Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes JDBC and Relational Databases and the fly likes Storing an image to DB thru servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Storing an image to DB thru servlet" Watch "Storing an image to DB thru servlet" New topic

Storing an image to DB thru servlet

Raghav Sam
Ranch Hand

Joined: Apr 12, 2001
Posts: 412
I want to store an image(Jpeg/Gif) in the database thru servlet which the user selects using the browse option,so can any one help me out.

Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.<br />- Dr. Seuss
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi! Storing the image in the database? Do you mean that you want to save the name for the image in the database and then upload the image to the server.
If you do, check out for oreilly package. read the document carefully. This is for uploading you image. To update your database use your regular database connect. after that you shuld do an insert to the database.
insert into your_table ( image ) VALUES ( '"& image &"' )
hope this helps
Raghav Sam
Ranch Hand

Joined: Apr 12, 2001
Posts: 412
Hi Mathias P.W Nilsson,
My actual problem is how to get the(in what data type) image from the Form thru request in servlet.
Yogen Vadnere
Ranch Hand

Joined: Sep 20, 2001
Posts: 58
I m not getting ur prob, as per my understanding..u want to get image from client machine using <input type=File> tag and upload it to server and then store that image to ur database...
I would suggest first use multipart request from to upload the file to temporary folder on server and then use following code to save into the database.. hope this will help u..
File file = new File ("Logo.gif"); //use the actual file path
//from server
InputStream inStream = new FileInputStream ("Logo.gif");
PreparedStatement psmt = conn.prepareStatement
("INSERT INTO myTable (pk_field,img_field) VALUES
(?, ?)");
//mytable with two columns Varchar and Long raw
psmt.setString (1, "Logo");
psmt.setBinaryStream (3, inStream, (int)file.length ());
psmt.execute ();

Yogen Vadnere
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

If you are using oracle check out the example here: . If you are not using Oracle, It may shed some light as to what to do. The example "LOB Datatype" shows how to insert/retrieve/update an image to the database.
Hope it is useful,
I agree. Here's the link:
subject: Storing an image to DB thru servlet
It's not a secret anymore!