Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store a PDF/Word in a BLOB column and then retrieve it using java?

 
suresh dhanapal
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to store a PDF/Word in a BLOB column and then retrieve it using java?
Please help me on this I am new to this
Thanks
suresh
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use a PreparedStatement for saving. Then you'd use its setBlob method, to which you would pass a FileInputStream of the file in question.

Retrieving is just the other way around: ResultSet has a getBlob method, from which you can get the data as a byte[] or InputStream.
 
suresh dhanapal
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is my program, while opening PDF nothing has been return, please let me know what else is missing

String sql = "select document from corp_info_docs where CIFO_ID=154";
try {
getConnection();
pstmt = conn.prepareStatement(sql);
ResultSet resultSet = pstmt.executeQuery();
while (resultSet.next()) {
java.sql.Blob myBlob = resultSet.getBlob("DOCUMENT");
java.io.InputStream myInputStream = myBlob.getBinaryStream();
FileOutputStream fos = new FileOutputStream("C:\\BLOB_PDF4.pdf");
byte[] buffer = new byte[256];
int bytesRead = 0;
while ((bytesRead = myInputStream.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
System.out.println("Inside While:");
fos.close();
}
myInputStream.close();
}



} catch (Exception e) {
throw new DAOException("insertSchCorpDoc :" + e);
} finally {
release();

}
}
 
Paul Clapham
Sheriff
Posts: 21576
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try closing the output stream.
 
suresh dhanapal
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I did that , now I am getting below error while I opening pdf
Adobe Reader could not open 'BLOP_PDF.pdf' beacuse it is either not a supported file type or because the file has been damaged.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the bytes in the file you're writing identical to the bytes of the original document? Or, as a first step, has the blob in the DB verified to be correct? If so, how many bytes are you getting from the DB? Is it the same number of bytes the original document has?
 
suresh dhanapal
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all,
Now its working perfectly, its same size in original upload and DB file.
 
vani Maruthaiah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suresh,

What you did with that. want create Pdf from bytes[] ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic