• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Storing an image to DB thru servlet

 
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ();
...
}
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic