jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes saving image to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "saving image to database" Watch "saving image to database" New topic
Author

saving image to database

Shashank Anand
Greenhorn

Joined: Oct 30, 2008
Posts: 26
Hi,

I have following code to save an image into a database (msql - datatype blob).

//b.jsp
<%
File f = new File("c:/myfiles/pictures/bird.jpg");
FileInputStream fis = new FileInputStream(f);
//connection code here...
PreparedStatement ps = con.prepareStatement("INSERT INTO mytable (image) VALUES(?)");
ps.setBinaryStream(1, (InputStream) fis, (int) (f.length()));
ps.executeUpdate();
%>
It works and I can retrieve the image as well.
I have another page a.jsp linked to b.jsp with <form> tag.
There is a text field (name=image) in a.jsp. If I type c:/myfiles/pictures/bird.jpg in the text field and try to retrieve it in b.jsp,
the code doesn't work. Anyone please tell me where it went wrong? The code I am using is:
String img=request.getParameter("image");
File f = new File("img");
//same code as above.



Regards,
Shashank


Mark Williams
Ranch Hand

Joined: Aug 01, 2008
Posts: 66


That is your problem right there... You want to pass the identifier img (without quotes) to the constructor.

BTW, this isn't Shashank that took CS classes at Loyola in New Orleans with me is it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

Please take the time to choose the correct forum for your posts. This forum is for questions on JSP.

For more information, please read this.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: saving image to database