|
Re: Storing and Retrieving PDFs in Oracle
|
Posted: Aug 30, 2006 10:53 AM
|
|
import java.sql.Connection; import java.sql.SQLException; import java.sql.DriverManager; import java.sql.CallableStatement; import oracle.sql.BLOB; // IO Imports import java.io.Writer; import java.io.File; import java.io.FileInputStream; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileNotFoundException;
public class TestBLOB {
// Variable for reading id value. static String id = null; // Variable to read the filename static String fileName = null; // Create connection object. static Connection conn = null; /* * Default Constuctor that takes no arguments. */ public TestBLOB() { } /* * Constuctor that takes in values of id, filename and streaming flag. */ public TestBLOB(String idn, String filename, String flag ) { id = idn; fileName = filename; }
public static void main (String args []) throws SQLException, FileNotFoundException {
TestBLOB testBlob = new TestBLOB(args[0], args[1]); testBlob.getConnection(); testBlob.callUpdate(id, fileName);
} }
/* * Method that gets database connection and returns the same. */ public void getConnection() throws SQLException{ // Load Oracle driver. DriverManager.registerDriver (new oracle.jdbc.OracleDriver()); try { // Connect to the database. conn = DriverManager.getConnection ("jdbc:oracle:thin:@lcw2k7:1531:or920", "vbasaval", "vbasaval"); } catch (SQLException sqlex) { System.out.println("SQLException while getting db connection: "+sqlex); if (conn != null) { conn.close(); } } catch (Exception ex) { System.out.println("Exception while getting db connection: "+ex); if (conn != null) { conn.close(); } } }
/* * Method that calls update stored procedure on the database. This will use * the getCLOB method() and oracle.sql.BLOB class to create the CLOB object * before passing it to the procedure. */ public void callUpdate(String id, String filename) throws SQLException {
CallableStatement cs = null; BLOB blob = null; String blobData = null; try { // Read the file whose content has to be updated in the BLOB column. String lineSep = System.getProperty("line.separator"); BufferedReader br = new BufferedReader(new FileReader(filename)); String nextLine = ""; StringBuffer sb = new StringBuffer(); while ((nextLine = br.readLine()) != null) { sb.append(nextLine); sb.append(lineSep); } // Convert the content to string. blobData = sb.toString();
// call Stored DB procedure for updating CLOB column. cs = (CallableStatement) conn.prepareCall( "begin updateStory(?,?); end;" );
// create the CLOB object blob = getCLOB(blobData);
// set id cs.setObject(1, id );
//set clob data cs.setObject(2, blob );
// Execute callable statement. cs.execute();
// Close the Statement object cs.close();
} catch ( SQLException ex ) { System.out.println("SQLException status : " + ex.getMessage( ) ); } catch ( Exception ex ) { System.out.println("some exception " + ex.getMessage( ) );
} finally { try { // Close Statement if ( cs != null ) { cs.close( ); }
// Free BLOB if ( blob != null ) { blob.freeTemporary(); } // Close connection if (conn !=null) { conn.close(); } } catch ( Exception ex ) { System.out.println("Some exception in callUpdate method of given " + "status : " + ex.getMessage( ) );
} } }
/* * This method creates BLOB object using temporary clob and returns the same. */ private BLOB getBLOB( String blobData ) throws Exception { CLOB tempClob = null; try { // create a new temporary BLOB tempBlob = BLOB.createTemporary( conn, true, BLOB.DURATION_SESSION ); // Open the temporary BLOB in readwrite mode to enable writing tempBlob.open( BLOB.MODE_READWRITE ); // Get the output stream to write Writer tempBlobWriter = tempBlob.getCharacterOutputStream( ); // Write the data into the temporary BLOB tempBlobWriter.write( blobData ); // Flush and close the stream tempBlobWriter.flush( ); tempBlobWriter.close( ); // Close the temporary BLOB tempBlob.close( ); } catch ( Exception exp ) { tempBlob.freeTemporary(); System.out.println("Exception thrown in getBLOB method " + "of given status : " + exp.getMessage( ) ); } return tempBlob; } } }
|
|