• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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
  • 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: 43016
76
  • 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();

}
}
 
Marshal
Posts: 25833
69
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: 43016
76
  • 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.
 
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[] ?
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic