从数据库中搜索图像但不在java jlable中显示 [英] Searching image from a database but not display in the java jlable
问题描述
我几乎尝试过所有事情......请帮助任何人。输出显示为:run:
BUILD SUCCESSFUL(总时间:8秒)
但是图像没有显示在jLable中
我尝试过:
CrimiID = this.txtCriID.getText();
字符串查询=SELECT * FROM criminal WHERE CriminalID ='+ CrimiID +';
DBConnection.DBConnection ();
尝试{
语句s = DBConnection.con.createStatement();
ResultSet rs = s.executeQuery(Query);
while(rs.next()){
java.sql.Blob FileNameBlob = rs。 getBlob( 照片);
byte [] content = FileNameBlob.getBytes(1L,(int)FileNameBlob.length());
ImageIcon ik = new ImageIcon(content);
final BufferedImage image = ImageIO.read(new ByteArrayInputStream(content));
Image img = ik.getImage();
Image newimg = img.getScaledInstance (this.jLabelPhoto.getWidth(),this.jLabelPhoto.getHeight(),java.awt.Image.SCALE_SMOOTH);
ik = new ImageIcon(newimg);
this.jLabelPhoto.setIcon(ik);
} catch(SQLException ex){
JOptionPane.showMessageDialog(null,ex.getMessage( ));
}
}
I have tried nearly everything... Please help anyone. The output is showing as: run:
BUILD SUCCESSFUL (total time: 8 seconds)
But the image is not displaying in the jLable
What I have tried:
CrimiID = this.txtCriID.getText();
String Query = "SELECT * FROM criminal WHERE CriminalID = '"+CrimiID+"'";
DBConnection.DBConnection();
try {
Statement s = DBConnection.con.createStatement();
ResultSet rs = s.executeQuery(Query);
while(rs.next()){
java.sql.Blob FileNameBlob = rs.getBlob("Photo");
byte[] content = FileNameBlob.getBytes(1L, (int) FileNameBlob.length());
ImageIcon ik = new ImageIcon(content);
final BufferedImage image = ImageIO.read(new ByteArrayInputStream(content));
Image img = ik.getImage();
Image newimg = img.getScaledInstance(this.jLabelPhoto.getWidth(), this.jLabelPhoto.getHeight(), java.awt.Image.SCALE_SMOOTH);
ik = new ImageIcon(newimg);
this.jLabelPhoto.setIcon(ik);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
在使用数据库时请使用正确准备好的语句和参数。
SQL注入在90年代后期被识别出来,不幸的是它仍然在# 12网站漏洞。
所以我看到的第一个错误是在你的SQL语句中
你可以学习如何正确地准备语句和使用Oracle网站上的参数:使用准备语句(The Java™Tutorials> JDBC(TM)数据库访问> JDBC基础知识) [ ^ ]
字符串查询=SELECT * FROM criminal WHERE CriminalID ='+ CrimiID +';
应替换为
字符串查询=SELECT * FROM criminal WHERE CriminalID =?;
通常情况下,我会给出完整的代码,但是我不会在我的开发机器上使用它。我使用了类似于Stack Overflow解决方案的代码:
java - 将数据库中的blob图像显示到jlabel中 - Stack Overflow [ ^ ]
Please use properly prepared statements and parameters when working with a database.
SQL Injection was identified back in the late 90s and unfortunately it is still in the #12 vulnerability in websites.
So the first error I see is in your SQL statement
You can learn how to properly prepare statements and use parameters on Oracle's website: Using Prepared Statements (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)[^]
String Query = "SELECT * FROM criminal WHERE CriminalID = '"+CrimiID+"'";
should be replaced with
String Query = "SELECT * FROM criminal WHERE CriminalID = ?";
Normally I would give the full code, but I am not on my development machine with it handy. I have used code similar to this Stack Overflow solution:
java - Display blob image from database into jlabel - Stack Overflow[^]
这篇关于从数据库中搜索图像但不在java jlable中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!