2013-06-21 36 views
0

我试图从数据库表中检索blob数据类型并在imageView中显示图像。这里是我选择的SQL语句:将BufferedImage转换为图像数据类型

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; 
} 

检索后,我想要在imageview中显示它。以下是代码:

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

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

在此先感谢。

回答

0
Toolkit.getDefaultToolkit().createImage(data) 

此电话为字节数组从团块

+0

感谢阅读。我用其他解决方案解决了它 –

相关问题