aspose file tools*
The moose likes JDBC 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
Bookmark "Storing an image to DB thru servlet" Watch "Storing an image to DB thru servlet" New topic
Author

Storing an image to DB thru servlet

Raghav Sam
Ranch Hand

Joined: Apr 12, 2001
Posts: 412
Hi,
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.
Thanks,
Raghav.

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
Anonymous
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 www.servlets.com 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.
Thanks,
Raghav.
Yogen Vadnere
Ranch Hand

Joined: Sep 20, 2001
Posts: 58
Raghav,
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 www.servlets.com 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: http://web06-02.us.oracle.com/sample_code/tech/java/sqlj_jdbc/files/advanced/advanced.htm . 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,
Jamie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Storing an image to DB thru servlet
 
Similar Threads
updating server.xml from servlet
How to send gif image using java mail service
Binary to string conversion
Scored 75%
Servlet to display an image