1

I want to add a image in my JFrame from database. But this is not retrived. I use following code, is this right way to retrive image from database? What's the solution???

 Blob blob = rs.getBlob("image");
             int blobLength = (int) blob.length();  

             byte[] bytes = blob.getBytes(1, blobLength);
            //blob.free();
             BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));

             ImageIcon icon = new ImageIcon(bytes); // you can read straight from byte array
             l16 = new JLabel(icon);
             l16.setBounds(100, 100, 100, 50);
             add(l16);
  • At first read it looks reasonable to me. I would have used a custom `JComponent` (and painted the image myself) instead of a `JLabel`, and I would have used a proper layout manager. But anyway, what part of it is it that you don't think works? Have you made sure you're retrieving the expected data from the database? If you write the bytes to a file, can you open it in an image viewer? – aioobe Jun 07 '15 at 19:52
  • May this link help u: http://stackoverflow.com/questions/2150265/retrieve-an-image-stored-as-blob-on-a-mysql-db – Bacteria Jun 07 '15 at 19:53
  • What format of picture do you use? – vojta Jun 07 '15 at 19:57

0 Answers0