Oracle9i Application Developer's Guide - Large Objects (LOBs)
Release 1 (9.0.1)

Part Number A88879-01
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback

Go to previous page Go to beginning of chapter Go to next page

LOB Support in Different Programmatic Environments, 10 of 11


Using Java (JDBC) To Work with LOBs

You can perform the following tasks on LOBs with Java (JDBC):

Changing Internal Persistent LOBs Using Java

You can make changes to an entire internal LOB, or to pieces of the beginning, middle or end of an internal LOB in Java by means of the JDBC API via the objects:

These objects also implement java.sql.Blob and java.sql.Clob interfaces according to the JDBC 2.0 specification. With this implementation, an oracle.sql.BLOB can be used wherever a java.sql.Blob is expected and an oracle.sql.CLOB can be used wherever a java.sql.Clob is expected.

Reading Internal Persistent LOBs and External LOBs (BFILEs) with Java

With JDBC you can use Java to read both internal persistent LOBs and external LOBs (BFILEs).

BLOB, CLOB, and BFILE Classes

The BLOB, CLOB, and BFILE classes encapsulate LOB locators, so you do not deal with locators but instead use methods and properties provided to perform operations and get state information.

Calling DBMS_LOB Package From Java (JDBC)

Any LOB functionality not provided by these classes can be accessed by a call to the PL/SQL DBMS_LOB package. This technique is used repeatedly in the examples throughout this manual.

Referencing LOBs Using Java (JDBC)

You can get a reference to any of the above LOBs in the following two ways:

Using OracleResultSet: BLOB and CLOB Objects Retrieved Represent LOB Locators of Current Row

When BLOB and CLOB objects are retrieved as a part of an OracleResultSet, these objects represent LOB locators of the currently selected row.

If the current row changes due to a move operation, for example, rset.next(), the retrieved locator still refers to the original LOB row.

To retrieve the locator for the most current row, you must call getXXXX() on the OracleResultSet each time a move operation is made, where XXXX is a BLOB, CLOB or BFILE.

JDBC Syntax References and Further Information

For further JDBC syntax and information about using JDBC with LOBs:

See:

 

JDBC Methods for Operating on LOBs

The following JDBC methods operate on BLOBs, CLOBs, and BFILEs:

JDBC oracle.sql.blob Methods To Modify BLOB Values

Table 3-42 DBC oracle.sql.BLOB Methods To Modify BLOB Values
Method  Description 

int putBytes(long, byte[]) 

Inserts the byte array into the LOB, starting at the given offset  

JDBC oracle.sql.blob Methods To Read or Examine BLOB Values

Table 3-43 DBC oracle.sql.blob Methods to Read or Examine BLOB Values
Method  Description 

byte[] getBytes(long, int) 

Gets the contents of the LOB as an array of bytes, given an offset  

long position(byte[],long) 

Finds the given byte array within the LOB, given an offset  

long position(Blob,long) 

Finds the given BLOB within the LOB  

public boolean equals(java.lang.Object) 

Compares this LOB with another. Compares the LOB locators. 

public long length() 

Returns the length of the LOB  

public int getChunkSize() 

Returns the ChunkSize of the LOB  

JDBC oracle.sql.blob Methods and Properties for BLOB-Buffering

Table 3-44 JDBC oracle.sql.blob Methods and Properties for BLOB-Buffering

Method  Description 

public java.io.InputStream getBinaryStream()) 

Streams the LOB as a binary stream  

public java.io.OutputStream getBinaryOutputStream() 

Writes to LOB as a binary stream 

JDBC oracle.sql.clob Methods To Modify CLOB Values

Table 3-45 JDBC oracle.sql.clob Methods to Modify CLOB Values

Method  Description 

int putString(long, java.lang.String) 

Inserts the string into the LOB, starting at the given offset  

int putChars(long, char[]) 

Inserts the character array into the LOB, starting at the given offset 

JDBC oracle.sql.CLOB Methods To Read or Examine CLOB Value


Table 3-46 JDBC oracle.sql.CLOB Methods To Read or Examine CLOB Values  
Method  Description 

java.lang.String getSubString(long, int) 

Returns a substring of the LOB as a string 

int getChars(long, int, char[]) 

Reads a subset of the LOB into a character array 

long position(java.lang.String, long) 

Finds the given String within the LOB, given an offset 

long position(oracle.jdbc2.Clob, long) 

Finds the given CLOB within the LOB, given an offset 

boolean equals(java.lang.Object) 

Compares this LOB with another 

long length() 

Returns the length of the LOB 

int getChunkSize() 

Returns the ChunkSize of the LOB 

JDBC oracle.sql.clob Methods and Properties for CLOB-Buffering


Table 3-47 JDBC oracle.sql.clob Methods and Properties for CLOB-Buffering
Method  Description 

java.io.InputStream getAsciiStream() 

Reads the LOB as an ASCII stream 

java.io.OutputStream getAsciiOutputStream() 

Writes to the LOB from an ASCII stream 

java.io.Reader getCharacterStream() 

Reads the LOB as a character stream 

java.io.Writer getCharacterOutputStream() 

Writes to LOB from a character stream 

JDBC oracle.sql.bfile Methods To Read or Examine External LOB (BFILE) Values


Table 3-48 JDBC oracle.sql.bfile Methods To Read or Examine External LOB (BFILE) Values
Method  Description 

byte[] getBytes(long, int) 

Gets the contents of the BFILE as an array of bytes, given an offset 

int getBytes(long, int, byte[]) 

Reads a subset of the BFILE into a byte array 

long position(oracle.sql.BFILE, long) 

Finds the first appearance of the given BFILE contents within the LOB, from the given offset 

long position(byte[], long) 

Finds the first appearance of the given byte array within the BFILE, from the given offset 

boolean equals(java.lang.Object) 

Compares this BFILE with another. Compares locator bytes. 

long length() 

Returns the length of the BFILE 

boolean fileExists() 

Checks if the operating system (OS) file referenced by this BFILE exists 

public void openFile() 

Opens the OS file referenced by this BFILE 

public void closeFile() 

Closes the OS file referenced by this BFILE 

public boolean isFileOpen() 

Checks if this BFILE is already open 

public java.lang.String getDirAlias() 

Gets the directory alias for this BFILE 

public java.lang.String getName() 

Gets the file name referenced by this BFILE  

JDBC oracle.sql.BFILE Methods and Properties for BFILE-Buffering


Table 3-49 JDBC oracle.sql.BFILE Methods and Properties for BFILE-Buffering
Method  Description 

public java.io.InputStream getBinaryStream() 

Reads the BFILE as a binary stream 

JDBC: OracleBlob and OracleClob Do Not Work in Oracle8i 8.1.x and Higher Releases

OracleBlob and OracleClob were Oracle specific functions used in JDBC 8.0.x drivers to access LOB data. In Oracle8i Releases 8.1.x and higher, OracleBlob and OracleClob are deprecated.

If you use OracleBlob or OracleClob to access LOB data, you will receive the following typical error message, for example, when attempting to manipulate LOBs with Oracle8i Release 8.1.5 JDBC Thin Driver:

"Dumping lobs java.sql.SQLException: ORA-03115: unsupported network datatype or 
representation etc."

See Oracle9i JDBC Developer's Guide and Reference for a description of these non-supported functions and alternative and improved JDBC methods.

For further ideas on working with LOBs with Java, refer to the LOB examples shipped with Oracle9i or get a LOB examplet from http://www.oracle.com/java/jdbc.

JDBC Temporary LOB APIs

Oracle9i JDBC drivers contain APIs to create and close temporary LOBs. These APIs can replace prior releases' workaround of using the following procedures from the DBMS_LOB PL/SQL package:

JDBC: Opening and Closing LOBs

oracle.sql.CLOB class is the Oracle JDBC driver's implementation of standard JDBC java.sql.Clob interface. Table 3-51 lists the new Oracle extension APIs in oracle.sql.CLOB for accessing temporary CLOBs.

Oracle9i JDBC drivers contain APIs to explicitly open and close LOBs. These APIs replace previous techniques that use DBMS_LOB.open() and DBMS_LOB.close().

JDBC: Opening and Closing BLOBs

oracle.sql.BLOB class is the Oracle JDBC driver's implementation of standard JDBC java.sql.Blob interface. Table 3-52 lists the Oracle extension APIs in oracle.sql.BLOB that open and close BLOBs. These are new for this release.


Table 3-52 JDBC: Opening and Closing BLOBs
Methods  Description 

public void open(int mode) throws SQLException 

Opens the BLOB 

public boolean isOpen() throws SQLException 

Sees if the BLOB is open 

public void close() throws SQLException 

Closes the BLOB 

Opening the BLOB

To open a BLOB, your JDBC application can use the open method as defined in oracle.sql.BLOB class as follows:

/** 
 * Open a BLOB in the indicated mode. Valid modes include MODE_READONLY, 
 * and MODE_READWRITE. It is an error to open the same LOB twice. 
 */ 
public void open (int mode) throws SQLException

Possible values of the mode parameter are:

public static final int MODE_READONLY 
public static final int MODE_READWRITE 

Each call to open opens the BLOB. For example:

BLOB blob = ... 
blob.open (BLOB.MODE_READWRITE);

Checking If the BLOB is Opened

To see if a BLOB is opened, your JDBC application can use the isOpen method defined in oracle.sql.BLOB. The return boolean value indicates whether the BLOB has been previously opened or not. The isOpen method is defined as follows:

/** 
 * Check whether the BLOB is opened. 
 * @return true if the LOB is opened. 
 */ 
 public boolean isOpen () throws SQLException

The usage example is:

BLOB blob = ... 
// See if the BLOB is opened 
boolean isOpen = blob.isOpen ();

Closing the BLOB

To close a BLOB, your JDBC application can use the close method defined in oracle.sql.BLOB. The close API is defined as follows:

/** 
  * Close a previously opened BLOB. 
  */ 
public void close () throws SQLException

The usage example is:

BLOB blob = ... 
// close the BLOB 
blob.close ();

JDBC: Opening and Closing CLOBs

Class, oracle.sql.clob, is the Oracle JDBC driver's implementation of the standard JDBC java.sql.clob interface. Table 3-53 lists the new Oracle extension APIs in oracle.sql.clob to open and close CLOBs.


Table 3-53 JDBC: Opening and Closing CLOBs
Methods  Description 

public void open(int mode) throws SQLException 

Open the CLOB 

public boolean isOpen() throws SQLExceptio 

See if the CLOB is opened 

public void close() throws SQLException 

Close the CLOB 

Opening the CLOB

To open a CLOB, your JDBC application can use the open method defined in oracle.sql.CLOB class as follows:

/** 
 * Open a CLOB in the indicated mode. Valid modes include MODE_READONLY, 
 * and MODE_READWRITE. It is an error to open the same LOB twice. 
 */ 
public void open (int mode) throws SQLException

The possible values of the mode parameter are:

public static final int MODE_READONLY 
public static final int MODE_READWRITE 

Each call to open opens the CLOB. For example,

CLOB clob = ... 
clob.open (CLOB.MODE_READWRITE);

Checking if the CLOB is Open

To see if a CLOB is opened, your JDBC application can use the isOpen method defined in oracle.sql.CLOB. The return boolean value indicates whether the CLOB has been previously opened or not. The isOpen method is defined as follows:

/** 
  * Check whether the CLOB is opened. 
  * @return true if the LOB is opened. 
  */ 
public boolean isOpen () throws SQLException

The usage example is:

CLOB clob = ... 
 // See if the CLOB is opened 
 boolean isOpen = clob.isOpen ();

Closing the CLOB

To close a CLOB, the JDBC application can use the close method defined in oracle.sql.CLOB. The close API is defined as follows:

/** 
* Close a previously opened CLOB. 
*/ 
public void close () throws SQLException

The usage example is:

CLOB clob = ... 
// close the CLOB 
clob.close ();

JDBC: Opening and Closing BFILEs

oracle.sql.BFILE class wraps the database BFILE object. Table 3-54 lists the new Oracle extension APIs in oracle.sql.BFILE for opening and closing BFILEs.


Table 3-54 JDBC API Extensions for Opening and Closing BFILEs
Methods  Description 

public void open() throws SQLException 

Opens the BFILE 

public void open(int mode) throws SQLException 

Opens the BFILE 

public boolean isOpen() throws SQLException 

Checks if the BFILE is open 

public void close() throws SQLException 

Closes the BFILE 

Opening BFILEs

To open a BFILE, your JDBC application can use the OPEN method defined in oracle.sql.BFILE class as follows:

/** 
 * Open a external LOB in the readonly mode. It is an error 
 * to open the same LOB twice. 
 */ 
public void open () throws SQLException 

/** 
 * Open a external LOB in the indicated mode. Valid modes include 
 * MODE_READONLY only. It is an error to open the same 
 * LOB twice. 
 */ 
public void open (int mode) throws SQLException

The only possible value of the mode parameter is:

public static final int MODE_READONLY 

Each call to open opens the BFILE. For example,

BFILE bfile = ... 
bfile.open ();

Checking if the BFILE is Open

To see if a BFILE is opened, your JDBC application can use the ISOPEN method defined in oracle.sql.BFILE. The return boolean value indicates whether the BFILE has been previously opened or not. The ISOPEN method is defined as follows:

/** 
 * Check whether the BFILE is opened. 
 * @return true if the LOB is opened. 
 */ 
public boolean isOpen () throws SQLException

The usage example is:

BFILE bfile = ... 
// See if the BFILE is opened 
boolean isOpen = bfile.isOpen ();

Closing the BFILE

To close a BFILE, your JDBC application can use the CLOSE method defined in oracle.sql.BFILE. The CLOSE API is defined as follows:

/** 
 * Close a previously opened BFILE. 
*/ 
public void close () throws SQLException

The usage example is --

BFILE bfile = ... 
// close the BFILE 
bfile.close ();

Usage Example (OpenCloseLob.java)


/* 
 * This sample shows how to open/close BLOB and CLOB. 
 */ 

// You need to import the java.sql package to use JDBC 
import java.sql.*; 

// You need to import the oracle.sql package to use oracle.sql.BLOB 
import oracle.sql.*; 

class OpenCloseLob 
{ 
  public static void main (String args []) 
       throws SQLException 
  { 
    // Load the Oracle JDBC driver 
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); 

    String url = "jdbc:oracle:oci8:@"; 
    try { 
      String url1 = System.getProperty("JDBC_URL"); 
      if (url1 != null) 
        url = url1; 
    } catch (Exception e) { 
      // If there is any security exception, ignore it 
      // and use the default 
    } 

    // Connect to the database 
    Connection conn = 
      DriverManager.getConnection (url, "scott", "tiger"); 
    // It's faster when auto commit is off 
    conn.setAutoCommit (false); 

    // Create a Statement 
    Statement stmt = conn.createStatement (); 

    try 
    { 
      stmt.execute ("drop table basic_lob_table"); 
    } 
    catch (SQLException e) 
    { 
      // An exception could be raised here if the table did not exist already. 
    } 

// Create a table containing a BLOB and a CLOB 
stmt.execute ("create table basic_lob_table (x varchar2 (30), b blob, c clob)"); 

// Populate the table 
stmt.execute ("insert into basic_lob_table values ('one', 
'010101010101010101010101010101', 'onetwothreefour')"); 

    // Select the lobs 
    ResultSet rset = stmt.executeQuery ("select * from basic_lob_table"); 
    while (rset.next ()) 
    { 
      // Get the lobs 
      BLOB blob = (BLOB) rset.getObject (2); 
      CLOB clob = (CLOB) rset.getObject (3); 

      // Open the lobs 
      System.out.println ("Open the lobs"); 
      blob.open (BLOB.MODE_READWRITE); 
      clob.open (CLOB.MODE_READWRITE); 

      // Check if the lobs are opened 
      System.out.println ("blob.isOpen()="+blob.isOpen()); 
      System.out.println ("clob.isOpen()="+clob.isOpen()); 

      // Close the lobs 
      System.out.println ("Close the lobs"); 
      blob.close (); 
      clob.close (); 

      // Check if the lobs are opened 
      System.out.println ("blob.isOpen()="+blob.isOpen()); 
      System.out.println ("clob.isOpen()="+clob.isOpen()); 
    } 

    // Close the ResultSet 
    rset.close (); 

    // Close the Statement 
    stmt.close (); 

    // Close the connection 
    conn.close (); 
  } 
} 

Trimming LOBs using JDBC

Oracle9i JDBC drivers contain APIs to trim internal LOBs. These APIs replace previous techniques that used DBMS_LOB.trim().

JDBC: Trimming BLOBs

oracle.sql.blob class is Oracle JDBC driver's implementation of the standard JDBC java.sql.blob interface. Table 3-55 lists the new Oracle extension API in oracle.sql.blob that trims BLOBs.


Table 3-55 JDBC: Trimming BLOBs
Methods  Description 

public void trim(long newlen) throws SQLException 

Trims the BLOB 

The trim API is defined as follows:

/** 
 * Trim the value of the BLOB to the length you specify in the newlen parameter. 
 * @param newlen the new length of the BLOB. 
 */ 
public void trim (long newlen) throws SQLException

The newlen parameter specifies the new length of the BLOB.

JDBC: Trimming CLOBs

oracle.sql.CLOB class is the Oracle JDBC driver's implementation of standard JDBC java.sql.Clob interface. Table 3-56 lists the new Oracle extension API in oracle.sql.CLOB that trims CLOBs.


Table 3-56 JDBC: Trimming CLOBs
Methods  Description 

public void trim(long newlen) throws SQLException 

Trims the CLOB 

The trim API is defined as follows:

/** 
 * Trim the value of the CLOB to the length you specify in the newlen parameter. 
 * @param newlen the new length of the CLOB. 
 */ 
public void trim (long newlen) throws SQLException

The newlen parameter specifies the new length of the CLOB.

See:

Chapter 10, "Internal Persistent LOBs" "Java (JDBC): Trimming LOB Data" , for an example. 

JDBC: New LOB Streaming APIs

Oracle9i JDBC drivers contains the new LOB streaming APIs to read from/write to a LOB at the requested position from a Java stream. In prior releases, LOB streaming APIs did not specify the offset.

New JDBC BLOB Streaming APIs

oracle.sql.BLOB class is the Oracle JDBC driver's implementation of standard JDBC java.sql.Blob interface. Table 3-57 lists the new Oracle extension APIs in oracle.sql.BLOB that manipulate the BLOB content from the requested position.


Table 3-57 JDBC: New BLOB Streaming APIs
Methods  Description 

public java.io.OutputStream getBinaryOutputStream (long pos) throws SQLException 

Writes to the BLOB from a stream 

public java.io.InputStream getBinaryStream(long pos) throws SQLException 

Reads from the BLOB as a stream 

These APIs are defined as follows:

/** 
 * Write to the BLOB from a stream at the requested position. 
 * 
 * @param pos is the position data to be put. 
 * @return a output stream to write data to the BLOB 
 */ 
public java.io.OutputStream getBinaryOutputStream(long pos) throws SQLException

/** 
 * Read from the BLOB as a stream at the requested position. 
 * 
 * @param pos is the position data to be read. 
 * @return a output stream to write data to the BLOB 
 */ 
public java.io.InputStream getBinaryStream(long pos) throws SQLException

New CLOB Streaming APIs

oracle.sql.CLOB class is the Oracle JDBC driver's implementation of standard JDBC java.sql.Clob interface. Table 3-58 lists the new Oracle extension APIs in oracle.sql.CLOB that manipulate the CLOB content from the requested position.


Table 3-58 JDBC: New CLOB Streaming APIs
Methods  Description 

public java.io.OutputStream getAsciiOutputStream (long pos) throws SQLException 

Writes to the CLOB from an ASCII stream 

public java.io.Writer getCharacterOutputStream(long pos) throws SQLException 

Writes to the CLOB from a character stream 

public java.io.InputStream getAsciiStream(long pos) throws SQLException 

Reads from the CLOB as an ASCII stream 

public java.io.Reader getCharacterStream(long pos) throws SQLException 

Reads from the CLOB as a character stream 

These APIs are defined as follows:

/** 
  * Write to the CLOB from a stream at the requested position. 
  * @param pos is the position data to be put. 
  * @return a output stream to write data to the CLOB 
  */ 
public java.io.OutputStream getAsciiOutputStream(long pos) throws 
SQLException 

/** 
     * Write to the CLOB from a stream at the requested position. 
     * @param pos is the position data to be put. 
     * @return a output stream to write data to the CLOB 
     */ 
   public java.io.Writer getCharacterOutputStream(long pos) throws SQLException 

    /** 
     * Read from the CLOB as a stream at the requested position. 
     * @param pos is the position data to be put. 
     * @return a output stream to write data to the CLOB 
     */ 
  public java.io.InputStream getAsciiStream(long pos) throws SQLException 

   /** 
    * Read from the CLOB as a stream at the requested position. 
    * @param pos is the position data to be put. 
    * @return a output stream to write data to the CLOB 
    */ 
   public java.io.Reader getCharacterStream(long pos) throws SQLException

New BFILE Streaming APIs

oracle.sql.bfile class wraps the database BFILEs. Table 3-59 lists the new Oracle extension APIs in oracle.sql.bfile that reads BFILE content from the requested position.


Table 3-59 JDBC: New BFILE Streaming APIs
Methods  Description 

public java.io.InputStream getBinaryStream(long pos) throws SQLException 

Reads from the BFILE as a stream 

These APIs are defined as follows:

/** 
 * Read from the BLOB as a stream at the requested position. 
 * 
 * @param pos is the position data to be read. 
 * @return a output stream to write data to the BLOB 
 */ 
public java.io.InputStream getBinaryStream(long pos) throws SQLException

JDBC BFILE Streaming Example (NewStreamLob.java)


Note:

Some of the Java code strings (in quotes) in the example should appear on one line, but instead, they wrap to the next lines. For example, the stmt.execute lines. Be aware of this if you are using this code and ensure that the strings appear on one line. 



/* 
 * This sample shows how to read/write BLOB and CLOB as streams. 
 */ 

import java.io.*; 

// You need to import the java.sql package to use JDBC 
import java.sql.*; 

// You need to import the oracle.sql package to use oracle.sql.BLOB 
import oracle.sql.*; 

class NewStreamLob 
{ 
  public static void main (String args [])  throws Exception 
  { 
    // Load the Oracle JDBC driver 
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); 

    String url = "jdbc:oracle:oci8:@"; 
    try { 
      String url1 = System.getProperty("JDBC_URL"); 
      if (url1 != null) 
        url = url1; 
    } catch (Exception e) { 
      // If there is any security exception, ignore it 
      // and use the default 
    } 

    // Connect to the database 
    Connection conn = 
      DriverManager.getConnection (url, "scott", "tiger"); 
    // It's faster when auto commit is off 
    conn.setAutoCommit (false); 

    // Create a Statement 
    Statement stmt = conn.createStatement (); 

    try 
    { 
      stmt.execute ("drop table basic_lob_table"); 
    } 
    catch (SQLException e) 
    { 
      // An exception could be raised here if the table did not exist already. 
    } 

    // Create a table containing a BLOB and a CLOB 
    stmt.execute ("create table basic_lob_table (x varchar2 (30), b blob, c 
clob)"); 

    // Populate the table 
    stmt.execute ("insert into basic_lob_table values ('one', 
'010101010101010101010101010101', 'onetwothreefour')"); 
  
    System.out.println ("Dumping lobs"); 

    // Select the lobs 
    ResultSet rset = stmt.executeQuery ("select * from basic_lob_table"); 
    while (rset.next ()) 
    { 
      // Get the lobs 
      BLOB blob = (BLOB) rset.getObject (2); 
      CLOB clob = (CLOB) rset.getObject (3); 

      // Print the lob contents 
      dumpBlob (conn, blob, 1); 
      dumpClob (conn, clob, 1); 

      // Change the lob contents 
      fillClob (conn, clob, 11, 50); 
      fillBlob (conn, blob, 11, 50); 
    } 
    rset.close (); 

    System.out.println ("Dumping lobs again"); 

    rset = stmt.executeQuery ("select * from basic_lob_table"); 
    while (rset.next ()) 
    { 
      // Get the lobs 
      BLOB blob = (BLOB) rset.getObject (2); 
      CLOB clob = (CLOB) rset.getObject (3); 

      // Print the lobs contents 
      dumpBlob (conn, blob, 11); 
      dumpClob (conn, clob, 11); 
    } 
    // Close all resources 
    rset.close(); 
    stmt.close(); 
    conn.close(); 
  } 

  // Utility function to dump Clob contents 
  static void dumpClob (Connection conn, CLOB clob, long offset) 
    throws Exception 
  { 
    // get character stream to retrieve clob data 
    Reader instream = clob.getCharacterStream(offset); 

    // create temporary buffer for read 
    char[] buffer = new char[10]; 

    // length of characters read 
    int length = 0; 

    // fetch data 
    while ((length = instream.read(buffer)) != -1) 
    { 
      System.out.print("Read " + length + " chars: "); 

      for (int i=0; i<length; i++) 
        System.out.print(buffer[i]); 
      System.out.println(); 
    } 

    // Close input stream 
    instream.close(); 
  } 

  // Utility function to dump Blob contents 
  static void dumpBlob (Connection conn, BLOB blob, long offset) 
    throws Exception 
  { 
    // Get binary output stream to retrieve blob data 
    InputStream instream = blob.getBinaryStream(offset); 
    // Create temporary buffer for read 
    byte[] buffer = new byte[10]; 
    // length of bytes read 
    int length = 0; 
    // Fetch data 
    while ((length = instream.read(buffer)) != -1) 
    { 
      System.out.print("Read " + length + " bytes: "); 

      for (int i=0; i<length; i++) 
        System.out.print(buffer[i]+" "); 
      System.out.println(); 
    } 

    // Close input stream 
    instream.close(); 
  } 

  // Utility function to put data in a Clob 
  static void fillClob (Connection conn, CLOB clob, long offset, long length) 
    throws Exception 
  { 
    Writer outstream = clob.getCharacterOutputStream(offset); 

    int i = 0; 
    int chunk = 10; 

    while (i < length) 
    { 
      outstream.write("aaaaaaaaaa", 0, chunk); 

      i += chunk; 
      if (length - i < chunk) 
         chunk = (int) length - i; 
    } 
    outstream.close(); 
  } 

  // Utility function to put data in a Blob 
  static void fillBlob (Connection conn, BLOB blob, long offset, long length) 
    throws Exception 
  { 
    OutputStream outstream = blob.getBinaryOutputStream(offset); 

    int i = 0; 
    int chunk = 10; 

    byte [] data = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; 

    while (i < length) 
    { 
      outstream.write(data, 0, chunk); 

      i += chunk; 
      if (length - i < chunk) 
         chunk = (int) length - i; 
    } 
    outstream.close(); 
  } 
} 

JDBC and Empty LOBs

An empty BLOB can be created from the following API from oracle.sql.BLOB:

public static BLOB empty_lob () throws SQLException 

Similarly, the following API from oracle.sql.CLOB creates a empty CLOB:

public static CLOB empty_lob () throws SQLException 

Empty LOB instances are created by JDBC drivers without making database round trips. Empty LOBs can be used in the following cases:

JDBC applications cannot read or write to empty LOBs created from the above APIs. An ORA-17098 "Invalid empty lob operation" results if your application attempts to read/write to an empty LOB.


Go to previous page Go to beginning of chapter Go to next page
Oracle
Copyright © 1996-2001, Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback