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個小時的研究,我沒有運氣。有人可以幫我嗎?
在此先感謝。
感謝閱讀。我用其他解決方案解決了它 –