-1

I am storing the image and other details of visitor into table tbl_visitor. The code is as follows,

    String string_op="F:\\POSTERS\\Roses\\TROPIC4.png"; 
    File imageFile = new File(string_op);
    FileInputStream fis = new FileInputStream(imageFile);

    String queryVis="insert into tbl_visitor(visitor_name,contact_no," +
            "job_profile,org_name,photo_id_proof,type_of_visitor,date," +
            "extra_people,image) values('"+
            name_of_visitor.getText()+"','"+
            contact_num.getText()+"','"+
            job_profile.getText()+"','"+
            org.getText()+"','"+
            photo_id_num.getText()+"','"+
            type_of_visitor.getSelectedItem().toString()+"','"+
            date_and_time.getText()+"','"+
            tf1.getText()+"','"+
            "fis,(int)imageFile.length()"+"')"; 

now I want to display the image on JFrame and to display the image am using the JLabel but I am unable to assign image to the JLabel. I have tried following code for display the image, but its giving me error.

Blob image_vis = rs1.getBlob(10);
image_cap.setIcon(image_vis);

Please help me.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Anjali
  • 269
  • 2
  • 6
  • 13
  • well, the parameter type of the SetIcon method must be of type ImageIcon. So I would try casting the Blob to a ImageIcon. Not 100% sure this will work though because I am not to familiar with the Blob class. – Travis Jun 03 '12 at 06:46
  • A single code line >400 chars wide?!? You must have a **very wide** monitor. For the sake of the rest of us, please force line breaks before SO introduces a scroll-bar. – Andrew Thompson Jun 03 '12 at 06:48
  • See also this possible duplicate: [conversion of byte array into image(blob) in java](http://stackoverflow.com/questions/10860210/conversion-of-byte-array-into-imageblob-in-java) – trashgod Jun 03 '12 at 07:50

1 Answers1

2

It's pretty simple if you take some time to read the API doc:

Blob has a getBinaryStream() which returns a stream of bytes containing the data stored in the blob.

ImageIcon, which implements Icon, has a constructor which takes a byte array as argument.

JLabel has a setIcon(Icon) method.

So, read everything from the Blob binary stream into a byte array, construct an ImageIcon using this byte array, and call the label setIcon method with this ImageIcon as argument.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for the solution. Can you please elaborate this statements by coding?? Thanks in advance. – Anjali Jun 03 '12 at 08:57
  • I won't do everything for you, no. Google for "Java IO tutorial" to understand how to read from an input stream to a byte array, or use Guava to do it for you. The rest is three lines of code. – JB Nizet Jun 03 '12 at 09:04
  • T tried this code but it does not display imageBlob image_vis = rs1.getBlob(10); ObjectInputStream ois = null; ois = new ObjectInputStream(image_vis.getBinaryStream()); ImageIcon image = (ImageIcon) ois.readObject(); – Anjali Jun 03 '12 at 12:09
  • 1
    My answer didn't tell you to do that at all. It said: read everything from the Blob binary stream **into a byte array**. Construct an ImageIcon **using this byte array** (and I even linked to the constructor you must use). Your code assumes you have a serialized ImageIcon object stored in the blob, which is not the case. Is it really so hard? Here's the whole code you need (with Guava): `label.setIcon(new ImageIcon(ByteStreams.toByteArray(blob.getBinaryStream())));` – JB Nizet Jun 03 '12 at 12:16