如何在java netbeans中显示从ms访问jpanel的图像 [英] How to show an image from ms access to jpanel in java netbeans
本文介绍了如何在java netbeans中显示从ms访问jpanel的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的代码:
private void okActionPerformed(java.awt.event.ActionEvent evt)
{
try {
String Update = name.getText();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:odbc:NewPData");
PreparedStatement psmnt = connection.prepareStatement("SELECT Image FROM Table1 where Name='" + Update + "'");
ResultSet rs = psmnt.executeQuery();
Blob blob = rs.getBlob("Image");
int b;
InputStream bis = rs.getBinaryStream("Image");
FileOutputStream f = new FileOutputStream("Image.jpg");
while ((b = bis.read()) >= 0) {
f.write(b);
}
f.close();
bis.close();
icon = new ImageIcon(blob.getBytes(1L, (int) blob.length()));
lblImage.setIcon(icon);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
例外显示:
java.lang.UnsupportedOperationException
我首先以ms访问存储图像,现在我想在标签上显示它。请帮助。
I have first stored the image in ms access and now I want to show it on a label. Please help.
推荐答案
这部分代码没有意义。
Blob blob = rs.getBlob("Image");
int b;
InputStream bis = rs.getBinaryStream("Image");
FileOutputStream f = new FileOutputStream("Image.jpg");
while ((b = bis.read()) >= 0) {
f.write(b);
}
f.close();
bis.close();
icon = new ImageIcon(blob.getBytes(1L, (int) blob.length()));
您基本上从结果集中读取BLOB到文件,然后再次尝试读取它以构建您的图片。你可能已经耗尽了这条小溪。
You basically read the BLOB from result set to a file and then try and read it again to construct your image. It's possible that you've exhausted the stream.
为什么不只是阅读图片?
Why not just read the image?
icon = new ImageIcon("Image.jpg");
更好的是,为什么不利用 ImageIO
API并直接读取流,是否需要写出临时文件?
Better yet, why not take advantage of the ImageIO
API and read the stream directly, for-going the need to write out a temp file?
BufferedImage image = ImageIO.read(bis);
icon = image == null ? null : new ImageIcon(image);
这篇关于如何在java netbeans中显示从ms访问jpanel的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文