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.
Asked
Active
Viewed 1,539 times
1
-
1What have you tried? How comes that you can write a blob, but not read one? – JB Nizet Feb 20 '12 at 11:33
-
What code are you currently using? – Donal Fellows Feb 20 '12 at 11:36
3 Answers
1
Retrieve the BLOB from the database and create a BufferedImage
using ImageIO.read
See here for how to paint the image.
0
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);
}
}

zari
- 1,709
- 1
- 12
- 18
-
1What will the ImageIcon be if there is an IOException? The last line shuld be inside the try block. – JB Nizet Feb 20 '12 at 11:46
-
No. It should be in the try block. Else, you'll construct an ImageIcon with a null image, and that will cause a NullPointerException. – JB Nizet Feb 20 '12 at 12:02
0
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

Community
- 1
- 1

Mukesh Singh Rathaur
- 12,577
- 2
- 23
- 24
-
Depends on your app (like pretty much everything else). Depending on the number and size of images, and how they are used, either the filesystem or the database way could be better for performance. More often than not, the performance difference is negligible, and it's down to which way is more convenient to manage the data. Which, again, depends on your specific application. – Dmitri Mar 08 '12 at 01:00