Click here to Skip to main content
13,764,268 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
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());

}
}
Posted 3 days ago
Updated 3 days ago

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.181113.4 | Last Updated 10 Nov 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100