FileDocCategorySizeDatePackage
TestBlobSetBinaryStreamUpdate.javaAPI DocExample2967Mon Jul 09 18:58:14 BST 2001None

TestBlobSetBinaryStreamUpdate.java

import java.io.*;
import java.sql.*;
import java.text.*;

public class TestBlobSetBinaryStreamUpdate {
  Connection conn;

  public TestBlobSetBinaryStreamUpdate() {
    try {
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      conn = DriverManager.getConnection(
       "jdbc:oracle:oci8:@dssw2k01", "scott", "tiger");
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
      e.printStackTrace();
    }
  }

  public static void main(String[] args) 
   throws Exception, IOException {
    new TestBlobSetBinaryStreamUpdate().process();
  }

  public void process() throws IOException, SQLException {
    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;
      stmt.close();
      stmt = null;

      // set up the update statement
      pstmt = conn.prepareStatement(
       "update person_information " + 
       "set    photo = ? " + 
       "where  person_id = ?");

      // copy the entire contents of the file to a buffer
      File binaryFile = new File("tim2.gif");
      long fileLength = binaryFile.length();
      fin = new FileInputStream(binaryFile);

      pstmt.setBinaryStream(1, fin, (int)fileLength);

      pstmt.setLong(2, person_id);
      rows = pstmt.executeUpdate();
      fin.close();

      System.out.println(rows + " rows updated");

      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) { }
    }
  } 

  protected void finalize() 
   throws Throwable {
    if (conn != null) 
      try { conn.close(); } catch (SQLException ignore) { }
    super.finalize();
  }
}