FileDocCategorySizeDatePackage
TestBLOBPutBytes.javaAPI DocExample4120Sun Jul 08 18:12:12 BST 2001None

TestBLOBPutBytes

public class TestBLOBPutBytes extends Object

Fields Summary
Connection
conn
Constructors Summary
public TestBLOBPutBytes()

    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();
    }
  
Methods Summary
protected voidfinalize()

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

    new TestBLOBPutBytes().process();
  
public voidprocess()

    int               rows      = 0;
    FileInputStream   fin       = null;
    ResultSet         rslt      = null;
    Statement         stmt      = 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;
      
      // check to see if the row already exists
      rows = 0;
      rslt = stmt.executeQuery(
       "select photo " +
       "from   person_information " +
       "where  person_id = " + Long.toString( person_id ) + " " +
       "for update nowait");
      while (rslt.next()) {
        rows++;
        photo = rslt.getBlob(1);
      }
      rslt.close();
      rslt = null;
      
      // if it doesn't exist then insert
      // a row in the information table.
      // This creates the LOB locators
      if (rows == 0) {
        rows = stmt.executeUpdate(
         "insert into person_information " + 
         "( person_id, biography, photo ) " + 
         "values " +
         "( " + Long.toString( person_id ) + 
         ", empty_clob(), empty_blob() )");
        System.out.println(rows + " rows inserted");
          
        // Now retrieve the locator
        rows = 0;
        rslt = stmt.executeQuery(
         "select photo " +
         "from   person_information " +
         "where  person_id = " + Long.toString( person_id ) + " " +
         "for update nowait");
        rslt.next();
        photo = rslt.getBlob(1);
        rslt.close();
        rslt = null;
      }
      stmt.close();
      stmt = null;

      // Copy the entire contents of the file to a buffer
      File binaryFile = new File("tim.gif");
      long fileLength = binaryFile.length();
      byte[] buffer = new byte[(int)fileLength];
      fin = new FileInputStream(binaryFile);
      fin.read(buffer);
      fin.close();
      fin = null;
   
      // Write the buffer all at once
      int bytesWritten = ((oracle.sql.BLOB)photo).putBytes(1, buffer);

      if (bytesWritten == fileLength) 
        System.out.println(fileLength + " bytes written");
      else
        System.out.println("only " + bytesWritten + " bytes written");
    
      conn.commit();

    }
    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 (fin != null)  
        try { fin.close();  } catch (IOException  ignore) { }
    }