• Post Reply Bookmark Topic Watch Topic
  • New Topic

Save and retrive file from JSP into BLOB

 
Chuan Ren
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a table that stores records for a Course. If I need to attach a document to that record, what is the best way of doing so?

Has any one has an example how I could store a file and retrieve that file from a BLOB type of a database using JSP?

Thank you.
 
Ikram Soomro
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chuan Ren,

You can insert any binary containt into database using PreparedStatement like

PreparedStatement statement = connection.prepareStatement("INSERT INTO COURSE VALUES(?)");

FileInputStream in = new FileInputStream(file);
//file = the file u want to insert

statement.setBinaryStream(1 , in , in.available());
statement.execute();

where:
1 is col: number
in.available() mean mean number of bytes you want to insert into databae

You can retrieve file from database using

//plase query here
InputStream in = rs.getBinaryStream(1; // 1 is col number // rs is ResultSet


it's up to you how u use it in JSP file i.e. in scriplet or in seprate class.

Hope this is the answer of your question.

Ikram Soomro
(SCJP,SCWCD)
 
Chuan Ren
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Chuan Ren
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created the following file upload form and a upload.jsp.





I have got an error:
java.lang.NullPointerException
at java.io.File.<init>(File.java:175)
at java.io.FileInputStream.<init>(FileInputStream.java:54)
at org.apache.jsp.upload_jsp._jspService(upload_jsp.java:49)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)


How do I read the file and then put into the database BLOB? Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!