Click here to Skip to main content
13,834,709 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+"'";


try {

Statement s = DBConnection.con.createStatement();
ResultSet rs = s.executeQuery(Query);
java.sql.Blob FileNameBlob = rs.getBlob("Photo");
byte[] content = FileNameBlob.getBytes(1L, (int) FileNameBlob.length());
ImageIcon ik = new ImageIcon(content);
final BufferedImage image = 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);
} catch (SQLException ex) {

JOptionPane.showMessageDialog(null, ex.getMessage());

Updated 10-Nov-18 12:25pm

1 solution

Rate this: bad
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[^]

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
Web02 | 2.8.190114.1 | Last Updated 10 Nov 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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