下面的代码应该从数据库中检索图像并将其显示在 jlabel 中。经过大量尝试,我无法显示图像。在下面的代码中,我首先连接到 JavaDB,它是一个简单的表,只有一个包含图像的记录。然后我检索该记录并将其存储在现有客户对象中。然后我解码字节数组并将数组设置为 ImageIcon,然后将图标设置为标签。我遇到的问题是输出确认字节数组已存储在图标中,但它不会显示在标签中。请帮忙
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Customer existingCustomer = new Customer();
String dbURL = "jdbc:derby://localhost:1527/CustDB";
String uname = "mydb";
String upass = "mydb";
//Get a connection
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection conn = DriverManager.getConnection(dbURL, uname, upass);
System.out.println("Connection OK");
Statement stmt = conn.createStatement();
ResultSet results = stmt.executeQuery(
"select photo from APP.CUSTOMER where CUSTOMER_ID = 2");
while(results.next())
{
existingCustomer.setPhoto(results.getBytes(1));
}
results.close();
stmt.close();
byte[] imageData = Base64.base64Decode(existingCustomer.getPhoto());
System.out.println(imageData);
if(imageData != null)
{System.out.println("yes");}else{System.out.println("no");}
ImageIcon icon = new ImageIcon(imageData);
//BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageData));
if(icon != null){System.out.println("yes");}else{System.out.println("no");}
jLabelImage.setText(null);
jLabelImage.setIcon(icon);
} catch (SQLException ex) {
Logger.getLogger(custform.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(custform.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(custform.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(custform.class.getName()).log(Level.SEVERE, null, ex);
}