Problem in retrieving PDF file from database

Hello,

I am saving a PDF File into database using Blob.I want to retrieve and write it into PDF file,but I am getting blank page.

I have the code here:

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.InputStream;

import java.io.OutputStream;

import java.sql.Blob;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import java.sql.PreparedStatement;

publicclass PDFFile{

Connection con;

Statement st;

ResultSet rs;

PreparedStatement pst;

Blob blob;

InputStream input;

OutputStream output;

PDFFile(){

try{

Class.forName("net.sourceforge.jtds.jdbc.Driver");

con = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.1.20:1433/FlexiReports","sa","madmax");

}catch (ClassNotFoundException cnf){

cnf.printStackTrace();

}catch (java.sql.SQLException sql){

sql.printStackTrace();

}

}

publicvoid insertFile(){

FileReader fr;

File file;

try{

file=new File("c:/testPDFFile.pdf");

fr=new FileReader(file);

pst = con.prepareStatement("insert into Reports values(?)");

pst.setCharacterStream(1,fr,(int)file.length());

pst.executeUpdate();

}

catch (Exception e){

e.printStackTrace();

}

}

publicvoid openFile(){

try{

st = con.createStatement();

rs = st.executeQuery("select fileData from Reports");

int i = 1;

blob =null;

while (rs.next()){

blob = rs.getBlob("fileData");

}

input = blob.getBinaryStream();

output=new FileOutputStream("c:/FromDatabase.pdf");

byte by[]=blob.getBytes(0,(int)blob.length());

output.write(by);

}catch (Exception e){

e.printStackTrace();

}finally{

try{

con.close();

}catch (java.sql.SQLException sqle){

}

}

}

publicstaticvoid main(String args[]){

PDFFile obj=new PDFFile();

obj.insertFile();

obj.openFile();

}

}

You can take Pdf file as input and change the name in insertFile.Let me know if there is any other way of doing it.

I tried some other examples which have typecated result set to OracleResultSet and retrive and write to new file,but using that I get an error,because I am using jtds driver.

[4891 byte] By [sruthima] at [2007-11-26 18:41:10]
# 1

I don't know what "getting blank page" means there. Why do people think that "get" is a useful verb? It's so vague and imprecise.

Anyway, as soon as I scrolled down to your imports and saw you were using FileReaders, I saw at least one problem. Don't use Readers for binary files (like PDF). Use InputStreams. And don't use the setCharacterStream() method on your database call, use setBinaryStream().

That may not solve your "getting" problem but it will at least get one problem fixed.

DrClapa at 2007-7-9 6:15:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

>>>>I don't know what "getting blank page" means there

When I retieve data from database,I am writing it in a PDF File.File is getting created,but the contents are not written.The reason is that writing data into a PDF File cannot be like normal text file.So I was looking for any class in API which does it.

>>>>Don't use Readers for binary files (like PDF). Use InputStreams

I tried doing it at first and found out that,because I am using SqlServer as database and I have declared this column's data type to be "text".So data should be entered only with Character Stream Readers."Its giving me an error -incompatible type if I use InputStream.

sruthima at 2007-7-9 6:15:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> The reason is that writing data into a PDF File cannot be like normal text file.

That's what I said. PDF is not text.

So go back and fix your original error, which was to use a text column in the database to store PDF documents. Use some kind of column that accepts a large number of bytes.

DrClapa at 2007-7-9 6:15:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
>setCharacterStreamYou should use setBinaryStream ang getBinaryStream:)
zhaoyha at 2007-7-9 6:15:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...