PostgreSQL
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> PostgreSQL

Cập nhật JLabel qua SetIcon từ kiểu dữ liệu bytea trong postgres

Tôi không có sẵn bản cài đặt PostgreSQL, nhưng tôi nghĩ bạn nên viết / đọc định dạng hình ảnh chứ không phải BufferedImage dữ liệu.

Ví dụ:chữ viết có thể trông giống như ...

Connection con = ...;
BufferedImage img = ...;
try (PreparedStatement stmt = con.prepareStatement("insert into tableofimages (image) values (?)")) {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        ImageIO.write(img, "png", baos);
        try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
            stmt.setBinaryStream(1, bais);
            int rows = stmt.executeUpdate();
            System.out.println(rows + " rows updated");
        }
    }
} catch (SQLException | IOException exp) {
    exp.printStackTrace();
}

Và việc đọc có thể trông giống như ...

Connection con = ...;
try (PreparedStatement stmt = con.prepareStatement("select image from tableofimages")) {
    try (ResultSet rs = stmt.executeQuery()) {
        while (rs.next()) {
            try (InputStream is = rs.getBinaryStream(1)) {
                BufferedImage img = ImageIO.read(is);
            }
        }
    }
} catch (SQLException | IOException exp) {
    exp.printStackTrace();
}



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. CHỌN hoặc THỰC HIỆN trong một hàm PL / pgSQL

  2. Giám sát động các phiên bản PostgreSQL bằng pg_top

  3. Barman Cloud - Phần 2:Cloud Backup

  4. Sự phát triển của khả năng chịu lỗi trong PostgreSQL:Giai đoạn nhân rộng

  5. JDBCTemplate đặt POJO lồng nhau với BeanPropertyRowMapper