FileDocCategorySizeDatePackage
TestBFILE.javaAPI DocExample2451Wed Jul 11 10:50:54 BST 2001None

TestBFILE.java

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

public class TestBFILE {
  Connection conn;

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

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

  public void process() throws IOException, SQLException {
    int               rows      = 0;
    ResultSet         rslt      = null;
    Statement         stmt      = 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_picture " +
       "where  person_id = " + Long.toString( person_id ));
      
      rows = stmt.executeUpdate(
       "insert into person_picture " + 
       "( person_id, picture ) " + 
       "values " +
       "( " + Long.toString( person_id ) + ", bfilename( 'TESTBFILE', 'tim.gif' ) )");
      
      System.out.println(rows + " rows inserted");
      
      conn.commit();

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

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