• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();

}
}
 
Marshal
Posts: 28293
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try closing the output stream.
 
suresh dhanapal
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all,
Now its working perfectly, its same size in original upload and DB file.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suresh,

What you did with that. want create Pdf from bytes[] ?
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic