Hi,
I am hoping someone can help point me in the right direction for this. I have been spinning my tires for the last couple days trying to figure this out. I am essentially trying to store a pdf byte[] in the a database blob field. When I run the code below, it does execute without errors but when i attempt to the view the pdf in the database it says something along the lines of incorrect encoding or damaged file. I know the byte[] arrary represents a proper pdf because i can save the byte[] as a pdf file to my local system. Any help would greatly be appreciated. I think i need to use a OutputStream to write my byte[] but i am not sure what to set that outputStream equal to.
Assumptions:
1. I am odjbc14 driver for oracle in a 10g database.
2. Using RAD7 IDE, so the oracleResultSet is wrapped by the WebSphere ResultSetWrapper and i can't access the native oracle one.
public static int savePDF( long primaryKey, byte[] pdf )
{
Statement createEmptyBlob = null;
PreparedStatement getBlobInstance = null;
PreparedStatement savePdfAsBlob = null;
StringBuffer sql = new StringBuffer();
sql.append( "UPDATE A SET BLOB_COL = EMPTY_BLOB() where PRIMARY_KEY = " );
sql.append( primaryKey ); //passed in value
try
{
connection = getDataBaseConnection();
//First I create a empty blob in the BLOB column field where i want to insert my byte[]
createEmptyBlob = connection.createStatement();
rowsUpdated = createEmptyBlob.executeUpdate( sql.toString() );
//Obtain a "handle" to the BLOB column:
getBlobInstance = connection.
PrepareStatement( "Select BLOB_COL from A where PRIMARY_KEY =?" );
getBlob.setLong(1, primaryKey)
ResultSet rsBlob = getBlobInstance.executeQuery();
while ( rsBlob.next() )
{
blob = rsBlob.getBlob( "BLOB_COL" );
}
//write the byte[] to the BLOB
savePdfAsBlob = dbConnection
.prepareStatement( "UPDATE A SET BLOB_COL = ? WHERE PRIMARY_KEY = ?" );
savePdfAsBlob.setBytes( 1, pdf );
savePdfAsBlob.setLong( 2, primaryKey );
rowsUpdated = savePdfAsBlob.executeUpdate();
connection.commit();
}
catch( aWholeLottaCrap ){ }
}//end very code post
The above code executes without error but when i attempt to retrieve the blob it comes back null.
//core code to retrieve the blob byte[]
Blob blob = null;
byte[] pdfStream = null;
prepStmt = Conn.
prepareStatement( "Select BLOB_COL from A where PRIMARY_KEY =?" );
prepStmt.setLong( 1, primaryKey );
ResultSet resultSet = prepStmt.executeQuery();
while ( resultSet.next() )
{
blob = resultSet.getBlob( "BLOB_COL" );// Stays null
pdfStream = blob.getBytes( 1, ( int ) blob.length() );//stays null
}