FileDocCategorySizeDatePackage
TestSetBlob2.javaAPI DocExample2288Mon Dec 11 19:38:32 GMT 2000None

TestSetBlob2.java

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

public class TestSetBlob2 {
 Connection conn;
 public TestSetBlob2() {
  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 TestSetBlob2().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 = 1;
  try {
   conn.setAutoCommit(false);

   // get Tim's photo 
   stmt = conn.createStatement();
   rslt = stmt.executeQuery(
    "select photo " + 
    "from   person p, person_information i " + 
    "where  p.person_id = i.person_id " +
    "and    last_name   = 'O''Reilly' " + 
    "and    first_name  = 'Tim'");   
   rslt.next();
   photo = rslt.getBlob(1);

   rslt.close();
   rslt = null;

   stmt.close();
   stmt = 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");

   conn.commit();

   pstmt.close();
   pstmt = 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) { }
   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();
 }
}