File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases 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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "saving image to database" Watch "saving image to database" New topic

saving image to database

Shashank Anand

Joined: Oct 30, 2008
Posts: 26

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

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()));
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.


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

Joined: Jan 10, 2002
Posts: 63548

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] [About Bear] [Books by Bear]
subject: saving image to database
It's not a secret anymore!