FileDocCategorySizeDatePackage
TestSetBlob.javaAPI DocExample3630Mon Dec 11 18:55:48 GMT 2000None

TestSetBlob

public class TestSetBlob extends Object

Fields Summary
Connection
conn
Constructors Summary
public TestSetBlob()

  try {
   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
   conn = DriverManager.getConnection(
    "jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");
  }
  catch (SQLException e) {
   System.err.println(e.getMessage());
   e.printStackTrace();
  }
 
Methods Summary
protected voidfinalize()

  if (conn != null) { try { conn.close(); } catch (SQLException ignore) { } }
  super.finalize();
 
public static voidmain(java.lang.String[] args)

  new TestSetBlob().process();
 
public voidprocess()

  int               rows      = 0;
  FileInputStream   fin       = null;
  ResultSet         rslt      = null;
  Statement         stmt      = null;
  PreparedStatement pstmt     = null;
  Blob              photo     = null;
  long              person_id = 0;
  try {
   conn.setAutoCommit(false);

   // get Tim's person_id 
   stmt = conn.createStatement();
   rslt = stmt.executeQuery(
    "select person_id " + 
    "from   person " + 
    "where  last_name  = 'O''Reilly' " + 
    "and    first_name = 'Tim'");   
   while (rslt.next()) {
    rows++;
    person_id = rslt.getLong(1);
   }
   if (rows > 1) {
    System.err.println("Too many rows!");
    System.exit(1);
   }
   else if (rows == 0) {
    System.err.println("Not found!");
    System.exit(1);
   }
   rslt.close();
   rslt = null;

   // delete an existing row
   rows = stmt.executeUpdate(
    "delete person_information " +
    "where  person_id = " + Long.toString( person_id ));
   System.out.println(rows + " rows deleted");

   // get an empty blob
   rslt = stmt.executeQuery(
    "select empty_blob() " + 
    "from   sys.dual");
   rslt.next();
   photo = rslt.getBlob(1);

   rslt.close();
   rslt = null;

   // set up the update statement
   pstmt = conn.prepareStatement(
    "insert into person_information " + 
    "( person_id, biography, photo ) " + 
    "values " +
    "( ?, empty_clob(), ? )");

   // update the database
   pstmt.setLong(1, person_id);
   pstmt.setBlob(2, photo);
   rows = pstmt.executeUpdate();
   System.out.println(rows + " rows updated");

   // retrieve and lock the Blob locator for photo
   rslt = stmt.executeQuery(
    "select photo " +
    "from   person_information " +
    "where  person_id = " + Long.toString( person_id ) + " " + 
    "for update nowait");
   rslt.next();
   photo = rslt.getBlob(1);

   rslt.close();
   rslt = null;

   stmt.close();
   stmt = null;

   // copy the entire contents of the file to a buffer
   File binaryFile = new File("tim.gif");
   long fileLength = binaryFile.length();
   fin = new FileInputStream(binaryFile);
   byte[] buffer = new byte[(int)fileLength];
   fin.read(buffer);
   fin.close();
   
   // put the contents of the buffer in the empty locator
   ((oracle.sql.BLOB)photo).putBytes(1, buffer);

   conn.commit();

   pstmt.close();
   pstmt = null;
  }
  catch (SQLException e) {
   System.err.println("SQL Error: " + e.getMessage());
  }
  catch (IOException e) {
   System.err.println("IO Error: " + e.getMessage());
  }
  finally {
   if (rslt != null)  
    try { rslt.close();  } catch (SQLException ignore) { }
   if (stmt != null)  
    try { stmt.close();  } catch (SQLException ignore) { }
   if (pstmt != null) 
    try { pstmt.close(); } catch (SQLException ignore) { }
  }