0

I am trying to load an image which is in a MySQL database as blob data format ([B@e96bf) but I can't load it to a JLabel as shown below. It shows the ByteArrayInputStream is empty.

byte[] bytesl = null;

ResultSet rs = DB.DB.search("select image from imageio where id = '2'");
while (rs.next()) {
    bytesl = rs.getBytes(1);               
}
BufferedImage imag = ImageIO.read(new ByteArrayInputStream(bytesl));

Image img = imag;

img = img.getScaledInstance(jLabel1.getWidth(), jLabel1.getHeight(),
                            Image.SCALE_SMOOTH);
jLabel2.setIcon(new ImageIcon(img));
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Door D Uthpala
  • 59
  • 1
  • 11
  • What type is `DB`? A good start would be putting a print statement in the while loop (or debug your code) to see if it ever goes in there. Shouldn't affect your result, but if the while loop is supposed to only execute at most once (I'm assuming...), you probably shouldn't be using a while loop (an if statement comes to mind as a better idea). – Bernhard Barker May 27 '15 at 17:36
  • have you verified the column and row have data? – MadConan May 27 '15 at 17:54
  • you i verified with an sout... [B@e96bf this is wht i got for bytesl do i have to decode it? – Door D Uthpala May 27 '15 at 17:59
  • `[B@e96bf` is (most likely) not what's actually stored in your byte array (if it is, that's a problem), that's just what Java prints out when you try to sout an array. Try `Arrays.toString` or `new String(bytes)` before passing it to `println`. [Relevant](http://stackoverflow.com/q/6684665). Also, you should probably show us the way you wrote data to the `image` column in your table, in case there's a problem there. If at all possible, you should always try to give us complete (but short) code we can just run instantly on our side to see the same result as what you are seeing. – Bernhard Barker May 27 '15 at 18:22
  • File f = jFileChooser1.getSelectedFile(); String path = f.getAbsolutePath(); System.out.println(path); newpath = path.replace("\\", "/"); File f2 = new File(newpath); Image img = ImageIO.read(f2); img = img.getScaledInstance(jLabel1.getWidth(), jLabel1.getHeight(), Image.SCALE_SMOOTH); jLabel1.setIcon(new ImageIcon(img)); fileContent = Files.readAllBytes(f2.toPath()); DB.DB.statement("insert into imageio (image) values ('" + fileContent + "')"); – Door D Uthpala May 27 '15 at 19:23

2 Answers2

0

I think you code doesn't go to while loop, because there's no records with such query. Most probably issue is the ' signs around 2. Usually id is a number, but in your request it looks like you compare it to string. Try to remove apostrophe signs around 2.

win_wave
  • 1,498
  • 11
  • 9
0
fileContent = Files.readAllBytes(f2.toPath());
DB.DB.statement("insert into imageio (image) values ('" + fileContent + "')");

You've just inserted the string [B@96bf into the database. ImageIO can't turn that into an image, so it returns null.

You should at least use a PreparedStatement and provide the data as an argument. You should really be using a Blob and an input stream here.

user207421
  • 305,947
  • 44
  • 307
  • 483