0

我正在尝试从数据库表中检索 blob 数据类型并在 imageView 中显示图像。这是我的选择 SQL 语句:

public boolean retrieve(){
boolean success = false;
ResultSet rs = null;
DBController db = new DBController();   
db.getConnection();     
String dbQuery = "SELECT * FROM sm_product WHERE productName ='" + name +"'";       
rs = db.readRequest(dbQuery);
try {
    if(rs.next()){
        desc = rs.getString("productDescription");
        price = rs.getInt("productPrice");
        quantity = rs.getInt("productQuantity");
        dateStr = rs.getString("dateOfCreation");
                    category = rs.getString("productCategory");     

                   Blob blob = rs.getBlob("productImage");
                   byte[] data = blob.getBytes(0, (int) blob.length());
                   image = ImageIO.read(new ByteArrayInputStream(data)); //Error here
                    success = true;
    }   
}
catch(Exception e){
    e.printStackTrace();
}   
db.terminate();
return success;
}

检索后,我想在图像视图中显示它。这是代码:

panel.getMyImageView().setImage(product.getImage());

但是,我收到了不兼容的类型错误消息。我知道 image = ImageIO.read(new ByteArrayInputStream(data)); 这条线应该存储为 BufferedImage ,然后从那里我慢慢转换为图像数据类型并显示在图像视图中。但是经过2个小时的研究,我没有运气。有人可以帮我吗?

提前致谢。

4

1 回答 1

0
Toolkit.getDefaultToolkit().createImage(data)

为从 blob 读取的字节数组调用此方法

于 2013-06-21T14:05:01.707 回答