import java.io.*; import java.util.*; import java.sql.*; import oracle.jdbc.driver.*; import oracle.sql.*; public class Slob { public Connection getConnectionJDBC(String url, String username, String password) { Connection conn = null; try { Class.forName ("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection(url,username,password); } catch (SQLException se) { System.out.println("SQL Exception (Slob): " + se); } catch (Exception e) { System.out.println("Exception (Slob): " + e); } return conn; } //This procedure will read a bolb attribute and put its content into a file. public boolean read (String outputPath, //path where the new file will be stored String outputFileName, //name of the new file that stores the blob String column, //the blob column String table, //table being searched long id, //for identification purpose String id_column) { //column used in identification of the record // Declare local variables Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; Blob blob = null; FileOutputStream out = null; InputStream in = null; int length = 0; boolean readStatus = true; String absolutePath = null; String readStr = "select " + column + " from " + table + " where " + id_column + " = ?"; try { String url = "jdbc:oracle:thin:@D5T6B40B:1521:second"; String username = "scott"; String password = "tiger"; conn = getConnectionJDBC(url, username, password); conn.setAutoCommit(false); stmt = conn.prepareStatement(readStr); stmt.setLong(1, id); rs = stmt.executeQuery(); if (rs.next()) { blob = rs.getBlob(column); length = (int) blob.length(); byte [] buffer = new byte [1000]; absolutePath = outputPath + outputFileName; out = new FileOutputStream(new File(absolutePath).getCanonicalPath()); in = blob.getBinaryStream(); while ((length = in.read(buffer)) != -1) { out.write(buffer, 0, length); } conn.commit(); } else { readStatus = false; } } catch (SQLException se) { System.out.println("SQL Error on blob read (Slob): " + se); readStatus = false; } catch ( Exception e ) { System.out.println("Other Error on blob read (Slob): " + e); readStatus = false; } finally { try { if (out != null) out.close ( ); if (in != null) in.close ( ); if (rs != null) rs.close ( ); if (stmt != null) stmt.close ( ); if (conn != null) conn.close ( ); } catch (SQLException se2) { System.out.println("SQL Error releasing resources (Slob read) : " + se2); readStatus = false; } catch (Exception e2) { System.out.println("Other error releasing resources (Slob read) : " + e2); readStatus = false; } } return readStatus; } //end of read //This procedure will write the content of a file into a blob field public boolean write (String inputPath, //path where the file will be used String inputFileName, //name of the file that will be used to write to a blob attribute String column, //the blob column String table, //table being searched long id, //for identification purpose String id_column) { //column used in identification of the record // Declare local variables Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; BLOB blob = null; int length = 0; boolean readStatus = true; String semiAbsolutePath = null; File binaryFile = null; FileInputStream in = null; OutputStream out = null; String queryStr = "select * from " + table + " where " + id_column + " = ? for update"; try { String url = "jdbc:oracle:thin:@D5T6B40B:1521:second"; String username = "scott"; String password = "tiger"; conn = getConnectionJDBC(url, username, password); conn.setAutoCommit(false); stmt = conn.prepareStatement(queryStr); stmt.setLong(1, id); rs = stmt.executeQuery(); rs.next(); blob = ((OracleResultSet) rs).getBLOB(column); binaryFile = new File(inputPath + inputFileName); in = new FileInputStream(binaryFile); out = blob.getBinaryOutputStream(); int chunk = blob.getChunkSize(); byte[] buffer = new byte[chunk]; length = -1; while ((length = in.read(buffer)) != -1) { out.write ( buffer, 0, length ); } if (blob.length() != binaryFile.length()) out.flush(); } catch (SQLException se) { System.out.println("SQL Error on blob write (Slob): " + se); readStatus = false; } catch ( Exception e ) { System.out.println("Other Error on blob write (Slob): " + e); readStatus = false; } finally { try { if (out != null) out.close ( ); if (in != null) in.close ( ); if (rs != null) rs.close ( ); if (stmt != null) stmt.close ( ); if (conn != null) conn.close ( ); } catch (SQLException se2) { System.out.println("SQL Error releasing resources (Slob read) : " + se2); readStatus = false; } catch (Exception e2) { System.out.println("Other error releasing resources (Slob read) : " + e2); readStatus = false; } } return readStatus; } //end of write public static void main(String[] argv) { Slob s = new Slob(); for (int i = 0; i < argv.length; i++) { System.out.println(argv[i]); } //end of listing arguments if (argv[0].equals("Read")) s.read("C:/Classes/", argv[1], "Picture", "Emp", 7788, "empno"); else s.write("C:/Classes/", argv[1], "Picture", "Emp", 7788, "empno"); } //end of main } /* The following lines show the compilation and testing of the class: C:\Tomcat321\CS643>javac Slob.java C:\Tomcat321\CS643>java Slob "write" "BryanTBall.jpg" write BryanTBall.jpg C:\Tomcat321\CS643>java Slob "Read" "testread.jpg" Read testread.jpg C:\Tomcat321\CS643>java Slob "write" "new_logo.doc" write new_logo.doc C:\Tomcat321\CS643>java Slob "Read" "test_logo.doc" Read test_logo.doc C:\Tomcat321\CS643> */