hello....sir

i have to access image from mysql database using java program
and display it on the window

please any one can tell me solution for this problem

thanks in advance

Can you get the image in bytes in the same format and order as the bytes would be in a file on disk? If so, the ImageIO class could create an image from those bytes using an inputstream from a byte array.

i have the same problem too
so i have been planing to upload those images in to a folder and save those file names in database
it will be nice if some one posts the answer

thanks for replaying

please give me an example to understand and how can i create a byte array to image...

thanks

Read the bytes for the image into an array of bytes:

byte[] theimageBytes = new byte[SIZEOFIMAGE]; // read the image 'file' bytes here

Once the bytes are in the array, then wrap it in a ByteArray and use ImageIO to read from that byte array and create an image.

retriviing image from data dase....

In this im already store the buffered image in database and i trying to retrive

image from database using following code


ResultSet res1=con.st.executeQuery("SELECT * FROM tablename where user_id='mani'");

blob=res1.getBlob(12); /* retriving image*/
int n1=(int) blob.length();
imageByte= new byte[n1];
ByteArrayInputStream in = new ByteArrayInputStream(imageByte);
ImageInputStream iis=ImageIO.createImageInputStream(in);

You need to put the bytes from the blob object into the imageByte array.

blob=res1.getBlob(12);

int n1=(int) blob.length();

imageByte=blob.getBytes(1,n1);

ImageInputStream iis=ImageIO.createImageInputStream(new ByteArrayInputStream(imageByte));

BufferedImage imag=ImageIO.read(iis);

when i compile above code it show an error

cannot find ImageInputStream class

although i import the javax.imageio package

please help me

thanks for previous repalys

Use ImageIO's read() method:
static BufferedImage read(InputStream input)

Use this for the input stream:
new ByteArrayInputStream(byte[] buf)

hi sir,
thanks for replaying sir

as u said i type this below code..

but it gives null image

blob=res1.getBlob(12);
int n1=(int) blob.length();
imageByte=blob.getBytes(1,n1);
InputStream is=new ByteArrayInputStream(imageByte);
BufferedImage imag=ImageIO.read(is);

System.out.println("hai dis is image:"+imageByte+"\ndis is inputstreamobject:"+is+"\ndis is image object:"+imag);

when i compile above it gives following output:

hai dis is image:[B@269997
dis is inputstream object:java.io.ByteArrayInputStream@2db197
dis is image object:null

thanks in adavance

What is the contents of the blob object? Does it contain the bytes from an image file?
What does the blob.getBytes(1,n1) statement do? Why is first arg 1 vs 0?
Where is imageByte defined?

Blob blob=res1.getBlob(12);
int n1=(int) blob.length();
byte[] imageByte=blob.getBytes(1,n1);
InputStream is=new ByteArrayInputStream(imageByte);
BufferedImage imag=ImageIO.read(is);
 
System.out.println("hai dis is image:"+imageByte+"\ndis is inputstreamobject:"+is+"\ndis is image object:"+imag);

in the the blob object contains bufferedimage object that canbe resized image.
and the getBytes(1,n) takes bytes from blob object...

if my thinking is wrong...

please tell me correct way solve dis problem

thnks sir

getBytes(1,n) takes bytes from blob object

Its important that ALL of the bytes are taken. Is there at byte at location 0?

the blob object contains bufferedimage object

Sorry I don't know what a bufferedimage object looks like.
My ideas were for bytes that came from a image file like what would be on your hard drive.

If the data is an object, then you need to use the ObjectInputStream to read the bytes and construct an object.

thanks for reolaying sir

here,some information abot bufferedImage sir,

BufferedImage imageFind(URL url1)
{
BufferedImage bufferedImage1=null;
Image im;
try
{
Image im=ImageIO.read(url1);// here url1 mens my image path
int width=150;   /*image width and height*/
int height=150;
BufferedImage bufferedImage1=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = bufferedImage1.createGraphics();
                                           /*dis five lines of code can resize my image*/
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.drawImage(im,0,0,width, height, null);
graphics2D.dispose();
}
catch(Exception e)
{
	System.out.println("------------error----------"+e);
}
}

in the above code i taken a image from JFileChooser and it is passed to above function
and it returns the buffered image..
that buffered is assign to image object in another class

using that image object i directly insert image into blob field....

using that image object i directly insert image into blob

What method copies the image data from what object to the blob?

Image image=imageFind("file:/H:/Main.jpg"); /*dis function is expalined in 
previous replay and dis can be returned bufferedimage */

/* my table has instance of username :varchar(500) and image :blob 
         and i use mysql database */

String s11="INSERT INTO "+table+" "+"  VALUES(mani,'"+image+"')";
                                                  /*dis way i insert image into the table*/
st.executeUpdate(s11);

thanks for u r previous replay....:)

Can you write some debug code to display the first 100 bytes of what is the the blob in hex format? That might give a clue to what the blob contains.
byte[] bytes = ... get bytes from blob ...
for (int i=0; i < 100; i++) {
System.out.print(Integer.toHexString(bytes) + " "); // show bytes in hex
}

please tell me i follow correct method or not for inserting a image in database

im full confused.......:confused::confused:

please help me

Sorry, I don't know anything about databases.
In my original post, I asked if the bytes in the blob were the same as they would be if when stored in an image file. I have no idea how data is stored in a blob. If the bytes are the same as in a disk file, then the techniques mentioned above can create an image. If the bytes are stored some other way, then the technique won't work.

Try to print this:

String s11="INSERT INTO "+table+" "+"  VALUES(mani,'"+image+"')";

Before executing it. What happens is that when the String s11 is constructed this is trying to take value: "+image; It "converts" the image java instance to a String. Meaning this is called:

String s11="INSERT INTO "+table+" "+"  VALUES(mani,'"+image.toString()+"')";

And in the end you have the String representation of the Image java class instance . Not the contents of the image saved to your disc. You were trying to save a String: '<image.toStirng()>' not the actual image

Also what mani is? Is it a variable or the username. Because when you try to execute it this will execute:

INSERT INTO table_sth VALUES(mani,'some text')

mani is not an sql variable, it will not execute.
This is what you want:

INSERT INTO table_sth VALUES('mani',...)

And if you want to use BLOB, use a PreparedStatement instead. Then check the BLOB api. Specifically the class SerialBlob that implements it. It has a constructor that takes as argument a byte array. Read the image's bytes and save them into an array and create the BLOB object. Then call the PreparedStatement.

I have never done this before but that is what I would have tried.

please any one can how tell

how to insert byte array ( this array is consisting of bufferedimage data in bytes)

into blob field in mysql database ....:idea::idea:

please give any idea

thanks in advance.........

please any one can how tell

how to insert byte array ( this array is consisting of bufferedimage data in bytes)

into blob field in mysql database ....:idea::idea:

please give any idea

thanks in advance.........

Use PreparedStatement instead of Statement. And check the API for BLOB class. It has link for a class the implements it and has the constructor that you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.