java - 如何将图像从数据库显示到界面?(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
解决方案
推荐阅读
- concurrency - 如何在预览中构造一个 swiftui 类注释的 mainactor
- npm - 错误(“webpack-cli”ReferenceError: navigator is not defined),当运行`npm run prod or dev'
- angular - 如何使 Angular `routerLink` 片段导航滚动到我的 `id` 目标?
- excel - 如何使用bash将excel文件中的每张表转换为单独的csv文件
- c++ - Boost::qi 解析字符串
- python-3.x - 根据熊猫数据框中的选定列条件查找重复的行
- jquery - Jquery - resize() 上的单击事件无法正常工作
- javascript - 如何使用 Node Streams 和 Promise 避免内存泄漏
- dataset - 如何在 colab 中加载巨大的数据集以转换为 spacy JSON?
- c++ - 找不到重复案例时,“案例标签值已出现在此开关中”