FileDocCategorySizeDatePackage
TestBlobSetBinaryStream.javaAPI DocExample3212Mon Jul 09 17:21:20 BST 2001None

TestBlobSetBinaryStream.java

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

public class TestBlobSetBinaryStream {
  Connection conn;

  public TestBlobSetBinaryStream() {
    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 TestBlobSetBinaryStream().process();
  }

  public void process() throws IOException, SQLException {
    FileInputStream   fin       = null;
    int               rows      = 0;
    long              person_id = 0;
    PreparedStatement pstmt     = null;
    ResultSet         rslt      = null;
    Statement         stmt      = null;
    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 ));

      stmt.close();
      stmt = null;

      // Insert the data bypassing the locator using a stream
      // This Only works for oci8 driver 8.1.6 to database 8.1.6
      pstmt = conn.prepareStatement(
       "insert into person_information " + 
       "( person_id, biography, photo ) " + 
       "values " +
       "( ?, empty_clob(), ? )");

      // Open the input stream
      File binaryFile = new File("tim.gif");
      long fileLength = binaryFile.length();
      fin = new FileInputStream(binaryFile );

      // Set the parameter values
      pstmt.setLong(1, person_id);
      pstmt.setBinaryStream(2, fin, (int)fileLength);
      rows = pstmt.executeUpdate();
      fin.close();
      System.out.println(rows + " rows inserted");

      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();
  }
}