Skip Headers
Oracle® Database JDBC Developer's Guide and Reference,
11g Release 1 (11.1)

B31224-04
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
PDF · Mobi · ePub

14 Working with LOBs and BFILEs

This chapter describes how to use Java Database Connectivity (JDBC) and the oracle.sql.* classes to access and manipulate large object (LOB) and binary file (BFILE) locators and data. This chapter contains the following sections:

Notes:

Oracle Extensions for LOBs and BFILEs

LOBs are stored in a way that optimizes space and provides efficient access. The JDBC drivers provide support for three types of LOB: binary large object (BLOB), which is used for unstructured binary data, character large object (CLOB), which is used for character data, and national character large object (NCLOB), which is used for national character data. BLOB, CLOB, and NCLOB data is accessed and referenced by using a locator that is stored in the database table and points to the BLOB, CLOB, and NCLOB data, which is outside the table.

BFILEs are large binary data objects stored in operating system files outside of database tablespaces. These files use reference semantics. They can also be located on tertiary storage devices, such as hard disks, CD-ROMs, PhotoCDs, and DVDs. As with BLOB, CLOB, and NCLOBs, a BFILE is accessed and referenced by a locator which is stored in the database table and points to the BFILE data.

To work with LOB data, you must first obtain a LOB locator. Then you can read or write LOB data and perform data manipulation.

The JDBC drivers support the following oracle.sql.* classes for BLOBs, CLOBs, NCLOBs, and BFILEs:

The oracle.sql.BLOB, oracle.sql.CLOB, and oracle.sql.NCLOB classes implement the java.sql.Blob, java.sql.Clob, and java.sql.NClob interfaces, respectively. In contrast, BFILE is an Oracle extension, without a corresponding java.sql interface.

Instances of these classes contain only the locators for these data types, not the data. After accessing the locators, you must perform some additional steps to access the data.

Note:

If you want to create a new LOB, then use the factory methods from oracle.jdbc.OracleConnection interface.

Working with BLOBs, CLOBs and NCLOBs

This section describes how to read and write data to and from BLOBs, CLOBs, and NCLOBs in Oracle Database, using LOB locators. This section covers the following topics:

Getting and Passing BLOB, CLOB, and NCLOB Locators

Standard as well as Oracle-specific getter and setter methods are available for retrieving or passing LOB locators from or to the database. This section covers the following topics:

Retrieving BLOB, CLOB, and NCLOB Locators

Given a standard JDBC result set or callable statement that includes BLOB, CLOB, or NCLOB locators, you can access the locators by using standard getter methods. You can use the standard getBlob, getClob, and getNClob methods, which return java.sql.Blob, Clob, and NClob objects, respectively.

Note:

All the standard and Oracle-specific getter methods discussed here take either an int column index or a String column name as input.

If you retrieve or cast the result set or the callable statement to OracleResultSet or OracleCallableStatement, then you can use Oracle extensions, as follows:

  • You can use getBLOB, getCLOB, and getNCLOB, which return oracle.sql.BLOB, CLOB, and NCLOB objects, respectively.

  • You can also use the getOracleObject method, which returns an oracle.sql.Datum object, and cast the output appropriately.

Example: Getting BLOB, CLOB, and NCLOB Locators from a Result Set

Assume the database has a table called lob_table with a column for a BLOB locator, blob_col, a column for a CLOB locator, clob_col, and a column for a NCLOB locator, nclob_col. This example assumes that you have already created the Statement object, stmt.

First, select the LOB locators into a standard result set, then get the LOB data into appropriate Java classes:

// Select LOB locator into standard result set.
ResultSet rs =
   stmt.executeQuery ("SELECT blob_col, clob_col, nclob_col FROM lob_table");
while (rs.next())
{
   // Get LOB locators into Java container classes.
   java.sql.Blob blob = (java.sql.Blob)rs.getObject(1);
   java.sql.Clob clob = (java.sql.Clob)rs.getObject(2);
   java.sql.NClob nclob = (java.sql.NClob)rs.getObject(3);
   (...process...)
}

The output is cast to java.sql.Blob, java.sql.Clob, and java.sql.NClob. As an alternative, you can cast the output to oracle.sql.BLOB, oracle.sql.CLOB, and oracle.sql.NCLOB to take advantage of extended functionality offered by the oracle.sql.* classes. For example, you can rewrite the preceding code to get the LOB locators as:

   // Get LOB locators into Java container classes.
   oracle.sql.BLOB blob = (BLOB)rs.getObject(1);
   oracle.sql.CLOB clob = (CLOB)rs.getObject(2);
   oracle.sql.NCLOB nclob = (NCLOB)rs.getObject(3);
   (...process...)

Example: Getting a CLOB Locator from a Callable Statement

The callable statement methods for retrieving LOBs are identical to the result set methods.

For example, if you have an OracleCallableStatement instance, ocs, that calls a function func that has a CLOB output parameter, then set up the callable statement as in the following example.

This example registers OracleTypes.CLOB as the type code of the output parameter.

OracleCallableStatement ocs = 
   (OracleCallableStatement)conn.prepareCall("{? = call func()}");
ocs.registerOutParameter(1, OracleTypes.CLOB);
ocs.execute();
oracle.sql.CLOB clob = ocs.getCLOB(1);

Passing BLOB, CLOB and NCLOB Locators

Given a standard JDBC prepared statement or callable statement, you can use standard setter methods to pass LOB locators. These methods are defined as follows:

public void setBlob(int index, Blob value);
public void setClob(int index, Clob value);
public void setNClob(int index, NClob value);

Note:

If you pass a BLOB to a PL/SQL procedure, then the BLOB must be no bigger than 32K - 7. If you pass a BLOB that exceeds this limit, then you will receive a SQLException exception.

Given an Oracle-specific OraclePreparedStatement or OracleCallableStatement, then you can use Oracle extensions as follows:

  • Use setBLOB, setCLOB, and setNClob, which take oracle.sql.BLOB, CLOB and NCLOB locators as input, respectively.

  • Use the setOracleObject method, which simply specifies an oracle.sql.Datum input.

Example: Passing a BLOB Locator to a Prepared Statement

If you have an OraclePreparedStatement object ops and a BLOB named my_blob, then write the BLOB to the database as follows:

OraclePreparedStatement ops = (OraclePreparedStatement)conn.prepareStatement
                            ("INSERT INTO blob_table VALUES(?)"); 
ops.setBLOB(1, my_blob);
ops.execute();

Example: Passing a CLOB Locator to a Callable Statement

If you have an OracleCallableStatement object ocs and a CLOB named my_clob, then input the CLOB to the stored procedure proc as follows:

OracleCallableStatement ocs = 
   (OracleCallableStatement)conn.prepareCall("{call proc(?))}");
ocs.setClob(1, my_clob);
ocs.execute();

Example: Passing an NCLOB Locator to a Callable Statement

If you have an OracleCallableStatement object ocs and an NCLOB named my_nclob, then input the NCLOB to the stored procedure proc as follows:

OracleCallableStatement ocs = 
   (OracleCallableStatement)conn.prepareCall("{call proc(?))}");
ocs.setNClob(1, my_nclob);
ocs.execute();

Reading and Writing BLOB, CLOB and NCLOB Data

Once you have a LOB locator, you can use JDBC methods to read and write the LOB data. LOB data is materialized as a Java array or stream. Unlike LONG and LONG RAW data, you can access the LOB data at any time during the life of the connection.

To read and write the LOB data, use the methods in the java.sql.BLOB, java.sql.CLOB, and java.sql.NCLOB class, as appropriate. These classes provide functionality such as reading from the LOB into an input stream, writing from an output stream into a LOB, determining the length of a LOB, and closing a LOB.

Notes:

To write LOB data, the application must acquire a write lock on the LOB object. One way to accomplish this is through a SELECT FOR UPDATE. Also, you must disable auto-commit mode.

To read and write LOB data, you can use these methods:

  • To read from a BLOB, use the getBinaryStream method of an java.sql.BLOB object to retrieve the entire BLOB as an input stream. This returns a java.io.InputStream object.

    As with any InputStream object, use one of the overloaded read methods to read the LOB data and use the close method when you finish.

  • To write to a BLOB, use the setBinaryStream method of an java.sql.BLOB object to retrieve the BLOB as an output stream. This returns a java.io.OutputStream object to be written back to the BLOB.

    As with any OutputStream object, use one of the overloaded write methods to update the LOB data and use the close method when you finish.

  • To read from a CLOB, use the getAsciiStream or getCharacterStream method of an java.sql.CLOB object to retrieve the entire CLOB as an input stream. The getAsciiStream method returns an ASCII input stream in a java.io.InputStream object. The getCharacterStream method returns a Unicode input stream in a java.io.Reader object.

    As with any InputStream or Reader object, use one of the overloaded read methods to read the LOB data and use the close method when you finish.

    You can also use the getSubString method of java.sql.CLOB object to retrieve a subset of the CLOB as a character string of type java.lang.String.

  • To write to a CLOB, use the setAsciiStream or setCharacterStream method of an java.sql.CLOB object to retrieve the CLOB as an output stream to be written back to the CLOB. The setAsciiStream method returns an ASCII output stream in a java.io.OutputStream object. The setCharacterStream method returns a Unicode output stream in a java.io.Writer object.

    As with any Stream or Writer object, use one of the overloaded write methods to update the LOB data and use the flush and close methods when you finish.

  • To read from an NCLOB, use the getAsciiStream or getCharacterStream method of an java.sql.NCLOB object to retrieve the entire NCLOB as an input stream. The getAsciiStream method returns an ASCII input stream in a java.io.InputStream object. The getCharacterStream method returns a Unicode input stream in a java.io.Reader object.

    As with any InputStream or Reader object, use one of the overloaded read methods to read the LOB data and use the close method when you finish.

    You can also use the getSubString method of java.sql.NCLOB object to retrieve a subset of the NCLOB as a character string of type java.lang.String.

  • To write to an NCLOB, use the setAsciiStream or setCharacterStream method of an oracle.sql.NCLOB object to retrieve the NCLOB as an output stream to be written back to the NCLOB. The setAsciiStream method returns an ASCII output stream in a java.io.OutputStream object. The setCharacterStream method returns a Unicode output stream in a java.io.Writer object.

    As with any Stream or Writer object, use one of the overloaded write methods to update the LOB data and use the flush and close methods when you finish.

    Notes:

    • The stream write methods described in this section write directly to the database when you write to the output stream. You do not need to run an UPDATE to write the data. However, you need to call close or flush to ensure all changes are written. CLOBs and BLOBs are transaction controlled. After writing to either, you must commit the transaction for the changes to be permanent. BFILEs are not transaction controlled. Once you write to them the changes are permanent, even if the transaction is rolled back, unless the external file system does something else.

    • When writing to or reading from a CLOB or an NCLOB, the JDBC drivers perform all character set conversions for you.

    • When reading a LOB using any of the getXXX methods described in the section, the returned stream fetches blocks of data from the database needed. The entire LOB is not fetched all at once, which makes it practical to read very large LOBs.

Example: Reading BLOB Data

Use the getBinaryStream method of the oracle.sql.BLOB class to read BLOB data. The getBinaryStream method provides access to the BLOB data through a binary stream.

The following example uses the getBinaryStream method to read BLOB data through a byte stream and then reads the byte stream into a byte array, returning the number of bytes read, as well.

// Read BLOB data from BLOB locator.
InputStream byte_stream = my_blob.getBinaryStream(1L);
byte [] byte_array = new byte [10];
int bytes_read = byte_stream.read(byte_array);
...

Example: Reading CLOB Data

The following example uses the getCharacterStream method to read CLOB data into a Unicode character stream. It then reads the character stream into a character array, returning the number of characters read, as well.

// Read CLOB data from CLOB locator into Reader char stream.
Reader char_stream = my_clob.getCharacterStream(1L);
char [] char_array = new char [10];
int chars_read = char_stream.read (char_array, 0, 10);
...

Example: Reading NCLOB Data

The following example uses the getCharacterStream method to read NCLOB data into a Unicode character stream. It then reads the character stream into a character array, returning the number of characters read, as well.

// Read NCLOB data from NCLOB locator into Reader char stream.
Reader char_stream = my_nclob.getCharacterStream(1L);
char [] char_array = new char [10];
int chars_read = char_stream.read (char_array, 0, 10);
...

The next example uses the getAsciiStream method of the oracle.sql.NCLOB class to read NCLOB data through an ASCII character stream. It then reads the ASCII stream into a byte array, returning the number of bytes read, as well.

// Read NCLOB data from NCLOB locator into Input ASCII character stream
Inputstream asciiChar_stream = my_nclob.getAsciiStream(1L); 
byte[] asciiChar_array = new byte[10]; 
int asciiChar_read = asciiChar_stream.read(asciiChar_array,0,10);

Example: Writing BLOB Data

Use the setBinaryOutputStream method of an oracle.sql.BLOB object to write BLOB data.

The following example reads a vector of data into a byte array, then uses the setBinaryOutputStream method to write an array of character data to a BLOB.

java.io.OutputStream outstream;

// read data into a byte array 
byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

// write the array of binary data to a Blob
outstream = ((BLOB)my_blob).setBinaryOutputStream(1L);
outstream.write(data);
...

Example: Writing CLOB Data

Use the setCharacterStream method or the setAsciiStream method to write data to a CLOB. The setCharacterStream method returns a Unicode output stream. The setAsciiStream method returns an ASCII output stream.

The following example reads a vector of data into a character array, then uses the setCharacterStream method to write the array of character data to a CLOB.

java.io.Writer writer;

// read data into a character array
char[] data = {'0','1','2','3','4','5','6','7','8','9'};

// write the array of character data to a Clob
writer = ((CLOB)my_clob).setCharacterStream();
writer.write(data);
writer.flush();
writer.close();
...

The next example reads a vector of data into a byte array, then uses the setAsciiStream method to write the array of ASCII data to a CLOB.

java.io.OutputStream out;

// read data into a byte array
byte[] data = {'0','1','2','3','4','5','6','7','8','9'};

// write the array of ascii data to a CLOB 
out = clob.setAsciiStream();
out.write(data);
out.flush();
out.close();

Example: Writing NCLOB Data

Use the setCharacterStream method or the setAsciiStream method to write data to an NCLOB. The setCharacterStream method returns a Unicode output stream. The setAsciiStream method returns an ASCII output stream.

The following example reads a vector of data into a character array, then uses the setCharacterStream method to write the array of character data to an NCLOB.

java.io.Writer writer;

// read data into a character array
char[] data = {'0','1','2','3','4','5','6','7','8','9'};

// write the array of character data to an NClob
writer = ((NCLOB)my_nclob).setCharacterStream();
writer.write(data);
writer.flush();
writer.close();
...

The next example reads a vector of data into a byte array, then uses the setAsciiStream method to write the array of ASCII data to an NCLOB.

java.io.OutputStream out;

// read data into a byte array
byte[] data = {'0','1','2','3','4','5','6','7','8','9'};

// write the array of ascii data to a NClob 
out = nclob.setAsciiStream();
out.write(data);
out.flush();
out.close();

Creating and Populating a BLOB, CLOB or NCLOB Column

Create and populate aBLOB, CLOB, or NCLOB column in a table by using SQL statements.

Note:

You cannot construct a new BLOB, CLOB, or NCLOB locator in your application with a Java new statement. You must create the locator through a SQL operation, and then select it into your application or use the factory methods from oracle.jdbc.OracleConnection interface.

Create a BLOB, CLOB, or NCLOB column in a table with the SQL CREATE TABLE statement, then populate the LOB. This includes creating the LOB entry in the table, obtaining the LOB locator, and then copying the data into the LOB.

Creating a BLOB, CLOB, or NCLOB Column in a New Table

To create a BLOB, CLOB, or NCLOB column in a new table, run the SQL CREATE TABLE statement. The following example code creates a BLOB column in a new table. This example assumes that you have already created your Connection object conn and Statement object stmt:

String cmd = "CREATE TABLE my_blob_table (x VARCHAR2 (30), c BLOB)";
stmt.execute (cmd);

In this example, the VARCHAR2 column designates a row number, such as 1 or 2, and the BLOB column stores the locator of the BLOB data.

Populating a BLOB, CLOB, or NCLOB Column in a New Table

This example demonstrates how to populate a BLOB, CLOB, or NCLOB column by reading data from a stream. These steps assume that you have already created your Connection object conn and Statement object stmt. The table my_blob_table is the table that was created in the previous section.

The following example writes the john.gif file to a BLOB:

  1. Begin by using SQL statements to create the BLOB entry in the table. Use the empty_blob function to create the BLOB locator.

    stmt.execute ("INSERT INTO my_blob_table VALUES ('row1', empty_blob())");
    
  2. Get the BLOB locator from the table.

    BLOB blob;
    cmd = "SELECT * FROM my_blob_table WHERE X='row1' FOR UPDATE";
    ResultSet rset = stmt.executeQuery(cmd);
    rset.next();
    BLOB blob = ((OracleResultSet)rset).getBLOB(2);
    

    Note:

    You must disable auto-commit mode.
  3. Declare a file handler for the john.gif file, then print the length of the file. This value will be used later to ensure that the entire file is read into the BLOB. Next, create a FileInputStream object to read the contents of the file, and an OutputStream object to retrieve the BLOB as a stream.

    File binaryFile = new File("john.gif");
    System.out.println("john.gif length = " + binaryFile.length());
    FileInputStream instream = new FileInputStream(binaryFile);
    OutputStream outstream = blob.setBinaryStream(1L);
    
  4. Call getBufferSize to retrieve the ideal buffer size to use in writing to the BLOB, then create the buffer byte array.

    int size = blob.getBufferSize();
    byte[] buffer = new byte[size];
    int length = -1;
    
  5. Use the read method to read the file to the byte array buffer, then use the write method to write it to the BLOB. When you finish, close the input and output streams and commit the changes.

    while ((length = instream.read(buffer)) != -1)
       outstream.write(buffer, 0, length);
    instream.close();
    outstream.close();
    conn.commit();
    

Once your data is in the BLOB, CLOB, or NCLOB, you can manipulate the data.

Accessing and Manipulating BLOB, CLOB, or NCLOB Data

Once you have your BLOB, CLOB, or NCLOB locator in a table, you can access and manipulate the data to which it points. To access and manipulate the data, you first must select their locators from a result set or from a callable statement.

After you select the locators, you can retrieve the BLOB, CLOB, or NCLOB data. After retrieving the BLOB, CLOB, or NCLOB data, you can manipulate it however you want.

This example is a continuation of the example in the previous section. It uses the SQL SELECT statement to select the BLOB locator from the table my_blob_table into a result set. The result of the data manipulation is to print the length of the BLOB in bytes.

// Select the blob - what we are really doing here
// is getting the blob locator into a result set 
BLOB blob;
cmd = "SELECT *  FROM my_blob_table";
ResultSet rset = stmt.executeQuery (cmd);

// Get the blob data - cast to OracleResult set to 
// retrieve the data in oracle.sql format 
String index = ((OracleResultSet)rset).getString(1);
blob = ((OracleResultSet)rset).getBLOB(2);

// get the length of the blob
int length = blob.length();

// print the length of the blob
System.out.println("blob length" + length);

// read the blob into a byte array 
// then print the blob from the array
byte bytes[] = blob.getBytes(1, length);
blob.printBytes(bytes, length);

Data Interface for LOBs

The data interface for LOBs provides a streamlined mechanism for writing and reading the entire LOB contents. It is simpler to code and faster in many cases. It does not provide the random access capability or access beyond 2147483648 elements as do the standard java.sql.Blob and java.sql.Clob interfaces and the Oracle extensions, oracle.sql.BLOB, oracle.sql.BFILE, and oracle.sql.CLOB.

Input

In Oracle Database 11g release 1 (11.1), the setBytes, setBinaryStream, setString, setCharacterStream, and setAsciiStream methods of PreparedStatement are extended for BLOB and CLOB parameters.

For the JDBC Oracle Call Interface (OCI) and Thin drivers there is no limitation on the size of the byte array or String and no limit on the length specified for the stream functions except the limits imposed by the Java language, which is that array sizes are limited to positive Java int or 2147483648 elements.

For the server-side internal driver there is currently a limitation of 4000 bytes for operations on SQL statements, such as an INSERT statement. The limitation does not apply for PL/SQL statements. There is a simple workaround for an INSERT statement, which is to wrap it in a PL/SQL block, as follows:

BEGIN
 INSERT id, c INTO clob_tab VALUES(?,?);
END;

You must bear in mind the following automatic switching of the input mode for large data:

This will have impact on some programs, which formerly got ORA-17157 errors for attempts to use setString for String values larger than 32766 characters. Now, depending on the type of the target parameter an error may occur while the application is executed or the operation may succeed.

Another impact is that the automatic switching may result in additional server-side parsing to adapt to the change in the parameter type. This would result in a performance effect if the data sizes vary above and below the limit for repeated executions of the statement. Switching to the stream modes will effect batching as well.

Oracle Database 10g release 1 (10.1) has the SetBigStringTryClob connection property. Setting this property causes the standard setString method to switch to setStringForClob method for large data. This property is no longer used or needed. The setBytesForBlob and setStringForClob methods create temporary LOBs, which are automatically freed when the statement is executed or closed before execution.

However, when a PL/SQL procedure or function is embedded in a SQL statement, data less than 4 KB is bound as String, which is the standard. When data is greater than 4KB, the driver binds the data as a String as for any SQL statement. This will throw an error. The workaround is to use setClob or setCharacterStream instead of setString or setStringForClob. You can also create a callable statement.

Output

The getBytes, getBinaryStream, getSting, getCharacterStream, and getAsciiStream methods of ResultSet and CallableStatement are extended to work with BLOB, CLOB, and BFILE columns or OUT parameters. These methods will work for any LOB of length less than 2147483648. This operates entirely on the client-side and will work with any supported version of the database, that is, Oracle Database 8.1.7 and later.

BLOB, BFILE, or CLOB data can be read and written using the same streaming mechanism as for LONG RAW and LONG data. To read, use defineColumnType(nn, Types.LONGVARBINARY) or defineColumnType(nn,Types.LONGVARCHAR) on the column. This produces a direct stream on the data as if it were a LONG RAW or LONG column. This technique is limited to Oracle Database 10g release 1 (10.1) and later.

CallableSatement and IN OUT Parameter

It is a PL/SQL requirement that the Java types used as input and output for an IN OUT parameter must be the same. The automatic switching of types done by the extensions described in this chapter may cause problems with this.

Consider that you have an IN OUT CLOB parameter of a stored procedure and you wish to use setString for setting the value for this parameter. For any IN and OUT parameter, the binds must be of the same type. The automatic switching of the input mode will cause problems unless you are sure of the data sizes. For example, if it is known that neither the input nor output data will ever be larger than 32766 bytes, then you could use setString for the input parameter and register the OUT parameter as Types.VARCHAR and use getString for the output parameter.

A better solution is to change the stored procedure to have separate IN and OUT parameters. That is, if you have:

CREATE PROCEDURE clob_proc( c IN OUT CLOB );

then, change it to:

CREATE PROCEDURE clob_proc( c_in IN CLOB, c_out OUT CLOB );

Another workaround is to use a container block to make the call. The clob_proc procedure can be wrapped with a Java string to use for the prepareCall statement, as follows:

"DECLARE c_temp; BEGIN c_temp := ?; clob_proc( c_temp); ? := c_temp; END;"

In either case you may use setString on the first parameter and registerOutParameter with Types.CLOB on the second.

Size Limitations

Please be aware of the effect on the performance of the Java memory management system due to creation of very large byte array or String. Please read the information provided by your Java virtual machine (JVM) vendor about the impact of very large data elements on memory management, and consider using the stream interfaces instead.

Working With Temporary LOBs

You can use temporary LOBs to store transient data. The data is stored in temporary table space rather than regular table space. You should free temporary LOBs after you no longer need them. If you do not, then the space the LOB consumes in temporary table space will not be reclaimed.

You can insert temporary LOBs into a table. When you do this, a permanent copy of the LOB is created and stored. Inserting a temporary LOB may be preferable for some situations. For example, if the LOB data is relatively small so that the overhead of copying the data is less than the cost of a database round trip to retrieve the empty locator. Remember that the data is initially stored in the temporary table space on the server and then moved into permanent storage.

You create a temporary LOB with the static method createTemporary(Connection, boolean, int). This method is defined in both the oracle.sql.BLOB and oracle.sql.CLOB classes. You free a temporary LOB with the freeTemporary method.

public static BLOB createTemporary(Connection conn, boolean isCached, int duration);
public static CLOB createTemporary(Connection conn, boolean isCached, int duration);

The duration must be either DURATION_SESSION or DURATION_CALL as defined in the oracle.sql.BLOB or oracle.sql.CLOB class. In client applications, DURATION_SESSION is appropriate. In Java stored procedures, you can use either DURATION_SESSION or DURATION_CALL, which ever is appropriate.

You can test whether a LOB is temporary by calling the isTemporary method. If the LOB was created by calling the createTemporary method, then the isTemporary method returns true, else it returns false.

You can free a temporary LOB by calling the freeTemporary method. Free any temporary LOBs before ending the session or call. Otherwise, the storage used by the temporary LOB will not be reclaimed.

Notes:

  • Failure to free a temporary LOB will result in the storage used by that LOB in the database being unavailable. Frequent failure to free temporary LOBs will result in filling up temporary table space with unavailable LOB storage.

  • When fetching data from a ReultSet with columns that are temporary LOBs, use getClob or getBlob instead of getString or getBytes. Also invoke freeTemporary to free the temporary LOBs.

Creating Temporary NCLOBs in JDK 1.5

You create temporary national character large objects (NCLOBs) using a variant of the createTemporary method.

CLOB.createTemporary (Connection conn, boolean cache, int duration, short form);

The form argument specifies whether the created LOB is a CLOB or an NCLOB. If form equals oracle.jdbc.OraclePreparedStatement.FORM_NCHAR, then the method creates an NCLOB. If form equals oracle.jdbc.OraclePreparedStatement.FORM_CHAR, then the method creates a CLOB.

Creating Temporary NCLOBs in JDK 1.6

JDBC 4.0 supports NCLOBs directly. You can use the standard factory method of java.sql.Connection interface to create an NCLOB.

Using Open and Close With LOBs

You do not have to open and close your LOBs. You may choose to open and close them for performance reasons.

If you do not wrap LOB operations inside an Open/Close call operation, then each modification to the LOB will implicitly open and close the LOB, thereby firing any triggers on a domain index. Note that in this case, any domain indexes on the LOB will become updated as soon as LOB modifications are made. Therefore, domain LOB indexes are always valid and may be used at any time.

If you wrap your LOB operations inside the Open/Close call operation, then triggers will not be fired for each LOB modification. Instead, the trigger on domain indexes will be fired at the Close call. For example, you might design your application so that domain indexes are not be updated until you call the close method. However, this means that any domain indexes on the LOB will not be valid in-between the Open/Close calls.

You open a LOB by calling the open or open(int) method. You may then read and write the LOB without any triggers associated with that LOB firing. When you are done accessing the LOB, close the LOB by calling the close method. When you close the LOB, any triggers associated with the LOB will fire. You can see if a LOB is open or closed by calling the isOpen method. If you open the LOB by calling the open(int) method, the value of the argument must be either MODE_READONLY or MODE_READWRITE, as defined in the oracle.sql.BLOB and oracle.sql.CLOB classes. If you open the LOB with MODE_READONLY, any attempt to write to the LOB will result in a SQL exception.

Note:

An error occurs if you commit the transaction before closing all LOBs that were opened by the transaction. The openness of the open LOBs is discarded, but the transaction is successfully committed. Hence, all the changes made to the LOB and non-LOB data in the transaction are committed, but the triggers for domain indexing are not fixed.

Working with BFILEs

This section describes how to read data to BFILEs, using file locators. This section covers the following topics:

Getting and Passing BFILE Locators

Getter and setter methods are available for retrieving or passing BFILE locators from or to the database.

Retrieving BFILE Locators

Given a standard JDBC result set or callable statement object that includes BFILE locators, you can access the locators by using the standard result set getObject method. This method returns an oracle.sql.BFILE object.

You can also access the locators by casting your result set to OracleResultSet or your callable statement to OracleCallableStatement and using the getOracleObject or getBFILE method.

Notes:

  • In the OracleResultSet and OracleCallableStatement classes, getBFILE and getBfile both return oracle.sql.BFILE. There is no java.sql interface for BFILEs.

  • If using getObject or getOracleObject, remember to cast the output, as necessary.

Example: Getting a BFILE locator from a Result Set

Assume that the database has a table called bfile_table with a single column for the BFILE locator bfile_col. This example assumes that you have already created your Statement object stmt.

Select the BFILE locator into a standard result set. If you cast the result set to OracleResultSet, then you can use getBFILE to get the BFILE locator, as follows:

// Select the BFILE locator into a result set
ResultSet rs = stmt.executeQuery("SELECT bfile_col FROM bfile_table"); 
while (rs.next()) 
{
   oracle.sql.BFILE my_bfile = ((OracleResultSet)rs).getBFILE(1); 
}

Note that as an alternative, you can use getObject to return the BFILE locator. In this case, because getObject returns a java.lang.Object, cast the results to BFILE. For example:

oracle.sql.BFILE my_bfile = (BFILE)rs.getObject(1); 

Example: Getting a BFILE Locator from a Callable Statement

Assume you have an OracleCallableStatement object ocs that calls a function func that has a BFILE output parameter. The following code example sets up the callable statement, registers the output parameter as OracleTypes.BFILE, runs the statement, and retrieves the BFILE locator:

OracleCallableStatement ocs = 
   (OracleCallableStatement)conn.prepareCall("{? = call func()}");
ocs.registerOutParameter(1, OracleTypes.BFILE);
ocs.execute();
oracle.sql.BFILE bfile = ocs.getBFILE(1);

Passing BFILE Locators

To pass a BFILE locator to a prepared statement or callable statement, you can do one of the following:

  • Use the standard setObject method.

  • Cast the statement to OraclePreparedStatement or OracleCallableStatement, and use the setOracleObject or setBFILE method.

These methods take the parameter index and an oracle.sql.BFILE object as input.

Example: Passing a BFILE Locator to a Prepared Statement

Assume you want to insert a BFILE locator into a table, and you have an OraclePreparedStatement object ops to insert data into a table. The first column is a string, the second column is a BFILE, and you have a valid oracle.sql.BFILE object, bfile. Write the BFILE to the database, as follows:

OraclePreparedStatement ops = (OraclePreparedStatement)conn.prepareStatement
                            ("INSERT INTO my_bfile_table VALUES (?,?)");
ops.setString(1,"one");
ops.setBFILE(2, bfile);
ops.execute();

Example: Passing a BFILE Locator to a Callable Statement

Passing a BFILE locator to a callable statement is similar to passing it to a prepared statement. In this case, the BFILE locator is passed to the myGetFileLength procedure, which returns the BFILE length as a numeric value.

OracleCallableStatement cstmt = (OracleCallableStatement)conn.prepareCall
                               ("begin ? := myGetFileLength (?); end;");
try
{
   cstmt.registerOutParameter (1, Types.NUMERIC);
   cstmt.setBFILE (2, bfile);
   cstmt.execute ();
   return cstmt.getLong (1);
}

Reading BFILE Data

To read BFILE data, you must first get the BFILE locator. You can get the locator from either a callable statement or a result set. Once you obtain the locator, you can call a number of methods on the BFILE without opening it. For example, you can use the oracle.sql.BFILE methods fileExists() and isFileOpen() to determine whether the BFILE exists and if it is open. However, if you want to read and manipulate the data, then you must open and close the BFILE, as follows:

  • Use the openFile method of the oracle.sql.BFILE class to open a BFILE.

  • When you are done, use the closeFile method of the BFILE class.

BFILE data is through a Java stream. To read from a BFILE, use the getBinaryStream method of an oracle.sql.BFILE object to access the file as an input stream. This returns a java.io.InputStream object.

As with any InputStream object, use one of the overloaded read methods to read the file data and use the close method when you finish.

Notes:

  • BFILEs are read-only. They reside in the OS (operating system) File System and can be written to only using OS tools and commands.

  • You can create a BFILE. However, you cannot create an OS file that a BFILE would refer to. Those are created only externally.

Example: Reading BFILE Data

The following example uses the getBinaryStream method of an oracle.sql.BFILE object to read BFILE data into a byte stream and then read the byte stream into a byte array. The example assumes that the BFILE has already been opened.

// Read BFILE data from a BFILE locator
Inputstream in = bfile.getBinaryStream();
byte[] byte_array = new byte{10};
int byte_read = in.read(byte_array);

Creating and Populating a BFILE Column

This section discusses how to create a BFILE column in a table with SQL operations and specify the location where the BFILE resides. The examples in this section assume that you have already created your Connection object conn and Statement object stmt.

Creating a BFILE Column in a New Table

To work with BFILE data, create a BFILE column in a table, and specify the location of the BFILE. To specify the location of the BFILE, use the SQL CREATE DIRECTORY...AS statement to specify an alias for the directory where the BFILE resides. In this example, the directory alias is test_dir and the BFILE resides in the /home/work directory.

String cmd;
cmd = "CREATE DIRECTORY test_dir AS '/home/work'";
stmt.execute (cmd);

Use the SQL CREATE TABLE statement to create a table containing a BFILE column. In this example, the name of the table is my_bfile_table.

// Create a table containing a BFILE field 
cmd = "CREATE TABLE my_bfile_table (x varchar2 (30), b bfile)";
stmt.execute (cmd);

In this example, the VARCHAR2 column designates a row number and the BFILE column stores the locator of the BFILE data.

Populating a BFILE Column

Use the SQL INSERT INTO...VALUES statement to populate the VARCHAR2 and BFILE fields. The BFILE column is populated with the locator to the BFILE data. To populate the BFILE column, use the bfilename function to specify the directory alias and the name of the BFILE file.

cmd ="INSERT INTO my_bfile_table VALUES ('one', bfilename(test_dir, 
                                         'file1.data'))";
stmt.execute (cmd);
cmd ="INSERT INTO my_bfile_table VALUES ('two', bfilename(test_dir,  
                                         'jdbcTest.data'))";
stmt.execute (cmd);

In this example, the name of the directory alias is test_dir. The locator of the BFILE file1.data is loaded into the BFILE column on row one, and the locator of the BFILE jdbcTest.data is loaded into the bfile column on row two.

As an alternative, you may want to create the row for the row number and BFILE locator now, but wait until later to insert the locator. In this case, insert the row number into the table and null as a place holder for the BFILE locator.

cmd ="INSERT INTO my_bfile_table VALUES ('three', null)";
stmt.execute(cmd);

Here, three is inserted into the row number column and null is inserted as the place holder. Later in your program, insert the BFILE locator into the table by using a prepared statement.

First get a valid BFILE locator into the bfile object:

rs = stmt.executeQuery("SELECT b FROM my_bfile_table WHERE x='two'");
rs.next();
oracle.sql.BFILE bfile = ((OracleResultSet)rs).getBFILE(1);

Then, create your prepared statement. Note that because this example uses the setBFILE method to identify the BFILE, the prepared statement must be cast to OraclePreparedStatement:

OraclePreparedStatement ops = (OraclePreparedStatement)conn.prepareStatement
                      (UPDATE my_bfile_table SET b=? WHERE x = 'three');
ops.setBFILE(1, bfile);
ops.execute();

Now row two and row three contain the same BFILE.

Once you have the BFILE locators available in a table, you can access and manipulate the BFILE data.

Accessing and Manipulating BFILE Data

Once you have the BFILE locator in a table, you can access and manipulate the data to which it points. To access and manipulate the data, you must first select its locator from a result set or a callable statement.

The following code continues the example from the preceding section, getting the locator of the BFILE from row two of a table into a result set. The result set is cast to OracleResultSet so that oracle.sql.* methods can be used on it. Several of the methods applied to the BFILE, such as getDirAlias and getName, do not require you to open the BFILE. Methods that manipulate the BFILE data, such as reading, getting the length, and displaying, do require you to open the BFILE.

When you finish manipulating the BFILE data, you must close the BFILE.

// select the bfile locator
cmd = "SELECT *  FROM my_bfile_table WHERE x = 'two'";
rset = stmt.executeQuery (cmd);

if (rset.next ())
    BFILE bfile = ((OracleResultSet)rset).getBFILE (2);

// for these methods, you do not have to open the bfile
println("getDirAlias() = " + bfile.getDirAlias());
println("getName() = " + bfile.getName());
println("fileExists() = " + bfile.fileExists());
println("isFileOpen() = " + bfile.isFileOpen());

// now open the bfile to get the data
bfile.openFile();

// get the BFILE data as a binary stream
InputStream in = bfile.getBinaryStream();
int length ;

// read the bfile data in 6-byte chunks
byte[] buf = new byte[6];

while ((length = in.read(buf)) != -1)
{
    // append and display the bfile data in 6-byte chunks 
   StringBuffer sb = new StringBuffer(length);
   for (int i=0; i<length; i++)
      sb.append( (char)buf[i] );
   System.out.println(sb.toString());
}

// we are done working with the input stream. Close it.   
in.close();

// we are done working with the BFILE. Close it.  
bfile.closeFile();

Oracle SecureFiles

In Oracle Database 11g Release 1 (11.1), Oracle SecureFiles, a completely redesigned storage for LOBs, provide the following capabilities:

These features are implemented in the database and are transparenly available to JDBC programs through the existing APIs.

The new setLobOptions and getLobOptions APIs are described in the PL/SQL Packages and Types Reference, and may be accessed from JDBC through callable statements.