首页 > 解决方案 > 如何将图像从数据库显示到界面?(Java 和 MySQL 工作台 6.3)

问题描述

界面图片我试图将数据库中的图像(按 bookId 搜索)显示到界面中,需要放入 jLabel。

代码没有问题,没有显示错误但它仍然不起作用

到目前为止,这是我的代码

import java.sql.*;
import javax.swing.*;

Connection conn;
Statement stmt;
ResultSet rs;
String query;
PreparedStatement statement;
ImageIcon format;
Object image

   private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {  
      try{
        query="select Abstract from books where BookId= 2020010001";
        statement = conn.prepareStatement(query);
        rs= statement.executeQuery();
        if(rs.next()){
           byte[]imagedata=rs.getBytes("Abstract");
           format = new ImageIcon(imagedata);
           jLabel3.setIcon(format);
         }
       }
       catch(SQLException e){
          JOptionPane.showMessageDialog(this,"connection error 1");
       }
                   // TODO add your handling code here:
   }                                        
}

我已经更改了一些代码,这就是代码。

//the code for the button
    String f = "select Abstract from books where BookId= 2020010001";   
try{
PreparedStatement preparedstatement;
preparedstatement = conn.prepareStatement(f);
rs= preparedstatement.executeQuery();
jLabel3.setIcon(new ImageIcon(f));
}

 catch(SQLException e)
     {
         JOptionPane.showMessageDialog(this,"connection error 1");
     }

这次它显示错误

线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException

标签: javamysqldatabase

解决方案


推荐阅读