I'm able to save an image selected with a JFileChooser to a BLOB column in MySQL in phpMyAdmin, but how can I view that BLOB and load it into a JFrame for display from within Java? Any code would be helpful.
-
1What have you tried? How comes that you can write a blob, but not read one?JB Nizet– JB Nizet2012-02-20 11:33:50 +00:00Commented Feb 20, 2012 at 11:33
-
What code are you currently using?Donal Fellows– Donal Fellows2012-02-20 11:36:03 +00:00Commented Feb 20, 2012 at 11:36
3 Answers
Retrieve the BLOB from the database and create a BufferedImage using ImageIO.read
See here for how to paint the image.
Comments
The solution listed above by Dmitri will solve your problem, that is sure. I was use to do same think when i was novice programmer, but is not a good idea to store the image in Database at all in them of performance issues. Its better to store the image file location path into database and store the image in file system. This will save your lots of processing and improve your performance. For better understanding just read this wonderful discussion
1 Comment
This is a code segment that shows a picture in a JLabel object which is stored in DB in blob format .
Blob sqlphoto = (Blob) rs.getBlob("photo");
if (sqlphoto != null) {
InputStream photo = sqlphoto.getBinaryStream();
Image image = null;
try {
image = ImageIO.read(photo);
jLabel31.setIcon(new ImageIcon(image));
}
catch (IOException ex) {
Logger.getLogger(ModifyClerk.class.getName()).log(Level.SEVERE, null, ex);
}
}