This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Storing an image to DB thru servlet

 
Raghav Sam
Ranch Hand
Posts: 412
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 412
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ();
...
}
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic