FileDocCategorySizeDatePackage
TestLongRawSetBytesUpdate.javaAPI DocExample3053Mon Dec 18 19:20:16 GMT 2000None

TestLongRawSetBytesUpdate

public class TestLongRawSetBytesUpdate extends Object

Fields Summary
Connection
conn
Constructors Summary
TestLongRawSetBytesUpdate()

  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   conn = DriverManager.getConnection(
    "jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger" );
  }
  catch (ClassNotFoundException e) {
   System.err.println("Class Not Found Error: " + e.getMessage());
   System.exit(1);
  }
  catch (SQLException e) {
   System.err.println("SQL Error: " + e.getMessage());
   System.exit(1);
  }
 
Methods Summary
protected voidfinalize()

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

  new TestLongRawSetBytesUpdate().process(); 
 
private voidprocess()

  byte[]            photo     = null;
  FileInputStream   fin       = null;
  int               rows      = 0;
  long              person_id = 0;
  PreparedStatement pstmt     = null;
  Statement         stmt      = null;
  ResultSet         rslt      = 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;

   rslt = stmt.executeQuery(
    "select photo " + 
    "from   person_photo " + 
    "where  person_id = " + Long.toString(person_id) + " " + 
    "for update nowait");
   rslt.next();
   photo = rslt.getBytes(1);

   rslt.close();
   rslt = null;

   stmt.close();
   stmt = null;

   pstmt = conn.prepareStatement(
    "update person_photo " + 
    "set    photo = ? " +
    "where  person_id = ?");

   File fileName = new File("tim.gif");
   long fileLength = fileName.length();
   fin = new FileInputStream(fileName);
   byte[] buffer = new byte[(int)fileLength];
   int read = fin.read(buffer);
   System.out.println(read + " bytes read");

   pstmt.setBytes(1, buffer);
   pstmt.setLong(2, person_id);
   rows = pstmt.executeUpdate();
   System.out.println(rows + " rows updated");

   fin.close();
   fin = null;

   conn.commit();

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