10 Overview of Supplied LOB APIs

There are APIs supplied to support LOBs.

Topics:

Programmatic Environments That Support LOBs

Table 10-1 lists the programmatic environments that support LOB functionality.

See Also:

APIs for supported LOB operations are described in detail in the following chapters:

Table 10-1 Programmatic Environments That Support LOBs

Language Precompiler or Interface Program Related Sections Related Books

PL/SQL

DBMS_LOB Package

"Using PL/SQL (DBMS_LOB Package) to Work With LOBs".

Oracle Database PL/SQL Packages and Types Reference

C

Oracle Call Interface for C (OCI)

"Using OCI to Work With LOBs".

Oracle Call Interface Programmer's Guide

C++

Oracle Call Interface for C++ (OCCI)

"Using C++ (OCCI) to Work With LOBs" .

Oracle C++ Call Interface Programmer's Guide

C/C++

Pro*C/C++ Precompiler

"Using C/C++ (Pro*C) to Work With LOBs".

Pro*C/C++ Programmer's Guide

COBOL

Pro*COBOL Precompiler

"Using COBOL (Pro*COBOL) to Work With LOBs".

Pro*COBOL Programmer's Guide

Java

JDBC Application Programmatic Interface (API)

"Using Java (JDBC) to Work With LOBs".

Oracle Database JDBC Developer’s Guide.

ADO/OLE DB

Oracle Provider for OLE DB (OraOLEDB).

"Oracle Provider for OLE DB (OraOLEDB)"

Oracle Provider for OLE DB Developer's Guide for Microsoft Windows

.NET

Oracle Data Provider for .NET (ODP.NET)

"Overview of Oracle Data Provider for .NET (ODP.NET) "

Oracle Data Provider for .NET Developer's Guide for Microsoft Windows

Comparing the LOB Interfaces

Table 10-2 and Table 10-3 compare the eight LOB programmatic interfaces by listing their functions and methods used to operate on LOBs. The tables are split in two simply to accommodate all eight interfaces. The functionality of the interfaces, with regards to LOBs, is described in the following sections.

Table 10-2 Comparing the LOB Interfaces, 1 of 2

PL/SQL: DBMS_LOB (dbmslob.sql) C (OCI)(ociap.h) C++ (OCCI) (occiData.h). Also for Clob and Bfile classes. Pro*C/C++ and Pro*COBOL

DBMS_LOB.COMPARE

N/A

N/A

N/A

DBMS_LOB.INSTR

N/A

N/A

N/A

DBMS_LOB.SUBSTR

N/A

N/A

N/A

DBMS_LOB.APPEND

OCILobAppend()

Blob.append()

APPEND

N/A (use PL/SQL assign operator)

OCILobAssign()

 

ASSIGN

N/A

OCILobCharSetForm()

Clob.getCharsetForm (CLOB only)

N/A

N/A

OCILobCharSetId()

Clob.getCharsetId()

(CLOB only)

N/A

DBMS_LOB.CLOSE

OCILobClose()

Blob.close()

CLOSE

N/A

N/A

Clob.closeStream()

N/A

DBMS_LOB.COPY

OCILobCopy2()

Blob.copy()

COPY

DBMS_LOB.ERASE

OCILobErase2()

N/A

ERASE

DBMS_LOB.FILECLOSE

OCILobFileClose()

Clob.close()

CLOSE

DBMS_LOB.FILECLOSEALL

OCILobFileCloseAll()

N/A

FILE CLOSE ALL

DBMS_LOB.FILEEXISTS

OCILobFileExist()

Bfile.fileExists()

DESCRIBE [FILEEXISTS]

DBMS_LOB.GETCHUNKSIZE

OCILobGetChunkSize()

Blob.getChunkSize()

DESCRIBE [CHUNKSIZE]

DBMS_LOB.GET_STORAGE_LIMIT

OCILobGetStorageLimit()

N/A

N/A

DBMS_LOB.GETOPTIONS

OCILobGetOptions()

Blob/Clob::getOptions

N/A

DBMS_LOB.FILEGETNAME

OCILobFileGetName()

Bfile.getFileName() and Bfile.getDirAlias()

DESCRIBE DIRECTORY, FILENAME

DBMS_LOB.FILEISOPEN

OCILobFileIsOpen()

Bfile.isOpen()

DESCRIBE ISOPEN

DBMS_LOB.FILEOPEN

OCILobFileOpen()

Bfile.open()

OPEN

N/A (use BFILENAME operator)

OCILobFileSetName()

Bfile.setName()

FILE SET

DBMS_LOB.GETLENGTH

OCILobGetLength2()

Blob.length()

DESCRIBE LENGTH

N/A

OCILobIsEqual()

Use operator = ( )=/!=

N/A

DBMS_LOB.ISOPEN

OCILobIsOpen()

Blob.isOpen()

DESCRIBE ISOPEN

DBMS_LOB.LOADFROMFILE

OCILobLoadFromFile2()

Use overloadedcopy()

LOAD FROM FILE

N/A

OCILobLocatorIsInit()

Clob.isinitialized()

N/A

DBMS_LOB.OPEN

OCILobOpen()

Blob.open

OPEN

DBMS_LOB.READ

OCILobRead()

Blob.read

READ

DBMS_LOB.SETOPTIONS

OCILobSetOptions()

Blob/Clob::setOptions

N/A

DBMS_LOB.TRIM

OCILobTrim2()

Blob.trim

TRIM

DBMS_LOB.WRITE

OCILobWrite2

Blob.write

WRITEORALOB.

DBMS_LOB.WRITEAPPEND

OCILobWriteAppend2()

N/A

WRITE APPEND

DBMS_LOB.CREATETEMPORARY

OCILobCreateTemporary()

N/A

N/A

DBMS_LOB.FREETEMPORARY

OCILobFreeTemporary()

N/A

N/A

DBMS_LOB.ISTEMPORARY

OCILobIsTemporary()

N/A

N/A

N/A

OCILobLocatorAssign()

use operator = ( ) or copy constructor

N/A

Table 10-3 Comparing the LOB Interfaces, 2 of 2

PL/SQL: DBMS_LOB (dbmslob.sql) Java (JDBC) ODP.NET

DBMS_LOB.COMPARE

Use DBMS_LOB.

OracleClob.Compare

DBMS_LOB.INSTR

position

OracleClob.Search

DBMS_LOB.SUBSTR

getBytes for BLOBs or BFILEsgetSubString for CLOBs

N/A

DBMS_LOB.APPEND

Use length and then putBytes() or PutString()

OracleClob.Append

OCILobAssign()

N/A [use equal sign]

OracleClob.Clone

OCILobCharSetForm()

N/A

N/A

OCILobCharSetId()

N/A

N/A

DBMS_LOB.CLOSE

use DBMS_LOB.

OracleClob.Close

DBMS_LOB.COPY

Use read and write

OracleClob.CopyTo

DBMS_LOB.ERASE

Use DBMS_LOB.

OracleClob.Erase

DBMS_LOB.FILECLOSE

closeFile

OracleBFile.CloseFile

DBMS_LOB.FILECLOSEALL

Use DBMS_LOB.

N/A

DBMS_LOB.FILEEXISTS

fileExists

OracleBFile.FileExists

DBMS_LOB.GETCHUNKSIZE

getChunkSize

OracleClob.OptimumChunkSize

DBMS_LOB.FILEGETNAME

getDirAlias

getName

OracleBFile.DirectoryNameOracle.BFile.FileName

DBMS_LOB.FILEISOPEN

Use DBMS_LOB.ISOPEN

OracleBFile.IsOpen

DBMS_LOB.FILEOPEN

openFile

OracleBFile.OpenFile

OCILobFileSetName()

Use BFILENAME

OracleBFile.DirectoryName

Oracle.BFile.FileName

OCILobFlushBuffer()

N/A

N/A

DBMS_LOB.GETLENGTH

length

OracleClob.Length

N/A

equals()

N/A

DBMS_LOB.ISOPEN

use DBMS_LOB.ISOPEN()

OracleClob.IsInChunkWriteMode

DBMS_LOB.LOADFROMFILE

Use read and then write

N/A

DBMS_LOB.OPEN

Use DBMS_LOB.OPEN()

OracleClob.BeginChunkWrite

DBMS_LOB.READ

BLOB or BFILE: getBytes() and getBinaryStream()

CLOB: getString() and getSubString() and getCharacterStream()

OracleClob.Read

DBMS_LOB.TRIM

Use DBMS_LOB.TRIM()

OracleClob.SetLength

DBMS_LOB.WRITE

BLOB: setBytes() and setBinaryStream()

CLOB: setString() and setCharacterStream()

OracleClob.Write

DBMS_LOB.WRITEAPPEND

Use length() and then putString() or putBytes()

OracleClob.Append

DBMS_LOB.CREATETEMPORARY

N/A

OracleClob constructors

DBMS_LOB.FREETEMPORARY

N/A

OracleClob.Dispose

DBMS_LOB.ISTEMPORARY

N/A

OracleClob.IsTemporary

Using PL/SQL (DBMS_LOB Package) to Work With LOBs

The PL/SQL DBMS_LOB package can be used for the following operations:

  • Internal persistent LOBs and Temporary LOBs: Read and modify operations, either entirely or in a piece-wise manner.

  • BFILEs: Read operations

    See Also:

    Oracle Database PL/SQL Packages and Types Reference for detailed documentation, including parameters, parameter types, return values, and example code.

Provide a LOB Locator Before Running the DBMS_LOB Routine

DBMS_LOB routines work based on LOB locators. For the successful completion of DBMS_LOB routines, you must provide an input locator representing a LOB that exists in the database tablespaces or external file system, before you call the routine.

  • Persistent LOBs: First use SQL to define tables that contain LOB columns, and subsequently you can use SQL to initialize or populate the locators in these LOB columns.

  • External LOBs: Define a DIRECTORY object that maps to a valid physical directory containing the external LOBs that you intend to access. These files must exist, and have READ permission for Oracle Server to process. If your operating system uses case-sensitive path names, then specify the directory in the correct case.

Once the LOBs are defined and created, you may then SELECT a LOB locator into a local PL/SQL LOB variable and use this variable as an input parameter to DBMS_LOB for access to the LOB value.

Examples provided with each DBMS_LOB routine illustrate this in the following sections.

See Also:

Directory Objects

Guidelines for Offset and Amount Parameters in DBMS_LOB Operations

The following guidelines apply to offset and amount parameters used in procedures in the DBMS_LOB PL/SQL package:

  • For character data—in all formats, fixed-width and varying-width—the amount and offset parameters are in characters. This applies to operations on CLOB and NCLOB data types.

  • For binary data, the offset and amount parameters are in bytes. This applies to operations on BLOB data types.

  • When using the following procedures:

    • DBMS_LOB.LOADFROMFILE

    • DBMS_LOB.LOADBLOBFROMFILE

    • DBMS_LOB.LOADCLOBFROMFILE

    you cannot specify an amount parameter with a value larger than the size of the BFILE you are loading from. To load the entire BFILE with these procedures, you must specify either the exact size of the BFILE, or the maximum allowable storage limit.

  • When using DBMS_LOB.READ, the amount parameter can be larger than the size of the data. The amount should be less than or equal to the size of the buffer. The buffer size is limited to 32K.

Determining Character Set ID

To determine the character set ID, you must know the character set name.

A user can select from the V$NLS_VALID_VALUES view, which lists the names of the character sets that are valid as database and national character sets. Then call the function NLS_CHARSET_ID with the desired character set name as the one string argument. The character set ID is returned as an integer. UTF16 does not work because it has no character set name. Use character set ID = 1000 for UTF16. Although UTF16 is not allowed as a database or national character set, the APIs in DBMS_LOB support it for database conversion purposes. DBMS_LOB.LOADCLOBFROMFILE and other procedures in DBMS_LOB take character set ID, not character set name, as an input.

See Also:

PL/SQL Functions and Procedures for LOBs

See Also:

PL/SQL functions and procedures that operate on BLOBs, CLOBs, NCLOBs, and BFILEs

PL/SQL Functions and Procedures to Modify LOB Values

Here is a table of DBMS_LOB procedures:

Table 10-4 PL/SQL: DBMS_LOB Procedures to Modify LOB Values

Function/Procedure Description

APPEND

Appends the LOB value to another LOB

CONVERTTOBLOB

Converts a CLOB to a BLOB

CONVERTTOCLOB

Converts a BLOB to a CLOB

COPY

Copies all or part of a LOB to another LOB

ERASE

Erases part of a LOB, starting at a specified offset

FRAGMENT_DELETE

Delete the data from the LOB at the given offset for the given length

FRAGMENT_INSERT

Insert the given data (< 32KBytes) into the LOB at the given offset

FRAGMENT_MOVE

Move the given amount of bytes from the given offset to the new given offset

FRAGMENT_REPLACE

Replace the data at the given offset with the given data (< 32kBytes)

LOADFROMFILE

Load BFILE data into a persistent LOB

LOADCLOBFROMFILE

Load character data from a file into a LOB

LOADBLOBFROMFILE

Load binary data from a file into a LOB

SETOPTIONS

Sets LOB features (deduplication and compression)

TRIM

Trims the LOB value to the specified shorter length

WRITE

Writes data to the LOB at a specified offset

WRITEAPPEND

Writes data to the end of the LOB

PL/SQL Functions and Procedures for Introspection of LOBs

Table 10-5 PL/SQL: DBMS_LOB Procedures to Read or Examine Internal and External LOB values

Function/Procedure Description

COMPARE

Compares the value of two LOBs

GETCHUNKSIZE

Gets the chunk size used when reading and writing. This only works on persistent LOBs and does not apply to external LOBs (BFILEs).

GETLENGTH

Gets the length of the LOB value.

GETOPTIONS

Returns options (deduplication, compression, encryption) for SecureFiles.

GET_STORAGE_LIMIT

Gets the LOB storage limit for the database configuration.

INSTR

Returns the matching position of the nth occurrence of the pattern in the LOB.

ISSECUREFILE

Returns TRUE if the BLOB or CLOB locator passed to it is for a SecureFiles or FALSE if it is not.

READ

Reads data from the LOB starting at the specified offset.

SETOPTIONS

Sets options (deduplication and compression) for a SecureFiles, overriding the default LOB column settings. Incurs a server round trip.

SUBSTR

Returns part of the LOB value starting at the specified offset.

PL/SQL Operations on Temporary LOBs

Table 10-6 PL/SQL: DBMS_LOB Procedures to Operate on Temporary LOBs

Function/Procedure Description

CREATETEMPORARY

Creates a temporary LOB

ISTEMPORARY

Checks if a LOB locator refers to a temporary LOB

FREETEMPORARY

Frees a temporary LOB

PL/SQL Read-Only Functions and Procedures for BFILEs

Table 10-7 PL/SQL: DBMS_LOB Read-Only Procedures for BFILEs

Function/Procedure Description

FILECLOSE

Closes the file. Use CLOSE() instead.

FILECLOSEALL

Closes all previously opened files

FILEEXISTS

Checks if the file exists on the server

FILEGETNAME

Gets the directory object name and file name

FILEISOPEN

Checks if the file was opened using the input BFILE locators. Use ISOPEN() instead.

FILEOPEN

Opens a file. Use OPEN() instead.

PL/SQL Functions and Procedures to Open and Close Internal and External LOBs

Table 10-8 PL/SQL: DBMS_LOB Procedures to Open and Close Internal and External LOBs

Function/Procedure Description

OPEN

Opens a LOB

ISOPEN

Sees if a LOB is open

CLOSE

Closes a LOB

See Also:

Opening Persistent LOBs with the OPEN and CLOSE Interfaces for detailed information about these procedures for specific LOB operations, such as, INSERT a row containing a LOB

Using OCI to Work With LOBs

Oracle Call Interface (OCI) LOB functions enable you to access and make changes to LOBs and to read data from BFILEs in C.

See Also:

Oracle Call Interface Programmer's Guide chapter "LOB and BFILE Operations" for the details of all topics discussed in this section.

Prefetching of LOB Data, Length, and Chunk Size

To improve OCI access of smaller LOBs, LOB data can be prefetched and cached while also fetching the locator. This applies to internal LOBs, temporary LOBs, and BFILEs.

Setting the CSID Parameter for OCI LOB APIs

If you want to read or write data in 2-byte Unicode format, then set the csid (character set ID) parameter in OCILobRead2() and OCILobWrite2() to OCI_UTF16ID.

The csid parameter indicates the character set id for the buffer parameter. You can set the csid parameter to any character set ID. If the csid parameter is set, then it overrides the NLS_LANG environment variable.

See Also:

Fixed-Width and Varying-Width Character Set Rules for OCI

In OCI, for fixed-width client-side character sets, the following rules apply:

  • CLOBs and NCLOBs: offset and amount parameters are always in characters

  • BLOBs and BFILEs: offset and amount parameters are always in bytes

The following rules apply only to varying-width client-side character sets:

  • Offset parameter:

    Regardless of whether the client-side character set is varying-width, the offset parameter is always as follows:

    • CLOBs and NCLOBs: in characters

    • BLOBs and BFILEs: in bytes

  • Amount parameter:

    The amount parameter is always as follows:

    • When referring to a server-side LOB: in characters

    • When referring to a client-side buffer: in bytes

  • OCILobFileGetLength():

    Regardless of whether the client-side character set is varying-width, the output length is as follows:

    • CLOBs and NCLOBs: in characters

    • BLOBs and BFILEs: in bytes

  • OCILobRead2():

    With client-side character set of varying-width, CLOBs and NCLOBs:

    • Input amount is in characters. Input amount refers to the number of characters to read from the server-side CLOB or NCLOB.

    • Output amount is in bytes. Output amount indicates how many bytes were read into the buffer bufp.

  • OCILobWrite2(): With client-side character set of varying-width, CLOBs and NCLOBs:

    • Input amount is in bytes. The input amount refers to the number of bytes of data in the input buffer bufp.

    • Output amount is in characters. The output amount refers to the number of characters written into the server-side CLOB or NCLOB.

Other Operations

For all other LOB operations, irrespective of the client-side character set, the amount parameter is in characters for CLOBs and NCLOBs. These include OCILobCopy2(), OCILobErase2(), OCILobLoadFromFile2(), and OCILobTrim2(). All these operations refer to the amount of LOB data on the server.

NCLOBs in OCI

NCLOBs are allowed as parameters in methods.

OCILobLoadFromFile2() Amount Parameter

When using OCILobLoadFromFile2() you cannot specify amount larger than the length of the BFILE. To load the entire BFILE, you can pass the value returned by OCILobGetStorageLimit().

OCILobRead2() Amount Parameter

To read to the end of a LOB using OCILobRead2(), you specify an amount equal to the value returned by OCILobGetStorageLimit().

OCILobLocator Pointer Assignment

Special care must be taken when assigning OCILobLocator pointers in an OCI program—using the "=" assignment operator. Pointer assignments create a shallow copy of the LOB. After the pointer assignment, the source and target LOBs point to the same copy of data.

These semantics are different from using LOB APIs, such as OCILobAssign() or OCILobLocatorAssign() to perform assignments. When the these APIs are used, the locators logically point to independent copies of data after assignment.

For temporary LOBs, before performing pointer assignments, you must ensure that any temporary LOB in the target LOB locator is freed by calling OCIFreeTemporary(). In contrast, when OCILobLocatorAssign() is used, the original temporary LOB in the target LOB locator variable, if any, is freed automatically before the assignment happens.

LOB Locators in Defines and Out-Bind Variables in OCI

Before you reuse a LOB locator in a define or an out-bind variable in a SQL statement, you must free any temporary LOB in the existing LOB locator buffer using OCIFreeTemporary().

OCI Functions That Operate on BLOBs, CLOBs, NCLOBs, and BFILEs

OCI functions that operate on BLOBs, CLOBs, NCLOBs, and BFILEs are as follows:

OCI Functions to Modify Persistent LOB (BLOB, CLOB, and NCLOB) Values

Table 10-9 OCI Functions to Modify Persistent LOB (BLOB, CLOB, and NCLOB) Values

Function/Procedure Description

OCILobAppend()

Appends LOB value to another LOB.

OCILobArrayWrite()

Writes data using multiple locators in one round trip.

OCILobCopy2()

Copies all or part of a LOB to another LOB.

OCILobErase2()

Erases part of a LOB, starting at a specified offset.

OCILobLoadFromFile2()

Loads BFILE data into a persistent LOB.

OCILobSetContentType()

Sets a content string in a SecureFiles.

OCILObSetOptions()

Enables option settings (deduplication and compression) for a SecureFiles.

OCILobTrim2()

Truncates a LOB.

OCILobWrite2()

Writes data from a buffer into a LOB, overwriting existing data.

OCILobWriteAppend2()

Writes data from a buffer to the end of the LOB.

OCI Functions to Read or Examine Persistent LOB and External LOB (BFILE) Values

Table 10-10 OCI Functions to Read or Examine persistent LOB and external LOB (BFILE) Values

Function/Procedure Description

OCILobArrayRead()

Reads data using multiple locators in one round trip.

OCILobGetChunkSize()

Gets the chunk size used when reading and writing. This works on persistent LOBs and does not apply to external LOBs (BFILEs).

OCILobGetContentType()

Gets the content string for a SecureFiles.

OCILobGetLength2()

Returns the length of a LOB or a BFILE.

OCILObGetOptions()

Obtains the enabled settings (deduplication, compression, encryption) for a given SecureFiles.

OCILobGetStorageLimit()

Gets the maximum length of an internal LOB.

OCILobRead2()

Reads a specified portion of a non-NULL LOB or a BFILE into a buffer.

OCI Functions for Temporary LOBs

Table 10-11 OCI Functions for Temporary LOBs

Function/Procedure Description

OCILobCreateTemporary()

Creates a temporary LOB.

OCILobIsTemporary()

Sees if a temporary LOB exists.

OCILobFreeTemporary()

Frees a temporary LOB.

OCI Read-Only Functions for BFILEs

Table 10-12 OCI Read-Only Functions for BFILES

Function/Procedure Description

OCILobFileClose()

Closes an open BFILE.

OCILobFileCloseAll()

Closes all open BFILEs.

OCILobFileExists()

Checks whether a BFILE exists.

OCILobFileGetName()

Returns the name of a BFILE.

OCILobFileIsOpen()

Checks whether a BFILE is open.

OCILobFileOpen()

Opens a BFILE.

OCI LOB Locator Functions

Table 10-13 OCI LOB-Locator Functions

Function/Procedure Description

OCILobAssign()

Assigns one LOB locator to another.

OCILobCharSetForm()

Returns the character set form of a LOB.

OCILobCharSetId()

Returns the character set ID of a LOB.

OCILobFileSetName()

Sets the name of a BFILE in a locator.

OCILobIsEqual()

Checks whether two LOB locators refer to the same LOB.

OCILobLocatorIsInit()

Checks whether a LOB locator is initialized.

OCI Functions to Open and Close Internal and External LOBs

Table 10-14 OCI Functions to Open and Close Internal and External LOBs

Function/Procedure Description

OCILobOpen()

Opens a LOB.

OCILobIsOpen()

Sees if a LOB is open.

OCILobClose()

Closes a LOB.

OCI LOB Examples

Further OCI examples are provided in:

See Also:

Oracle Call Interface Programmer's Guide for further OCI demonstration script listings

Further Information About OCI

See Also:

http://www.oracle.com/technology/ for more information about OCI features and frequently asked questions.

Using C++ (OCCI) to Work With LOBs

Oracle C++ Call Interface (OCCI) is a C++ API for manipulating data in an Oracle database. OCCI is organized as an easy-to-use set of C++ classes that enable a C++ program to connect to a database, run SQL statements, insert/update values in database tables, retrieve results of a query, run stored procedures in the database, and access metadata of database schema objects. OCCI also provides a seamless interface to manipulate objects of user-defined types as C++ class instances.

Oracle C++ Call Interface (OCCI) is designed so that you can use OCI and OCCI together to build applications.

The OCCI API provides the following advantages over JDBC and ODBC:

  • OCCI encompasses more Oracle functionality than JDBC. OCCI provides all the functionality of OCI that JDBC does not provide.

  • OCCI provides compiled performance. With compiled programs, the source code is written as close to the computer as possible. Because JDBC is an interpreted API, it cannot provide the performance of a compiled API. With an interpreted program, performance degrades as each line of code must be interpreted individually into code that is close to the computer.

  • OCCI provides memory management with smart pointers. You do not have to be concerned about managing memory for OCCI objects. This results in robust higher performance application code.

  • Navigational access of OCCI enables you to intuitively access objects and call methods. Changes to objects persist without writing corresponding SQL statements. If you use the client side cache, then the navigational interface performs better than the object interface.

  • With respect to ODBC, the OCCI API is simpler to use. Because ODBC is built on the C language, OCCI has all the advantages C++ provides over C. Moreover, ODBC has a reputation as being difficult to learn. The OCCI, by contrast, is designed for ease of use.

You can use OCCI to make changes to an entire persistent LOB, or to pieces of the beginning, middle, or end of it, as follows:

  • For reading from internal and external LOBs (BFILEs)

  • For writing to persistent LOBs

OCCI Classes for LOBs

OCCI provides these classes that allow you to use different types of LOB instances as objects in your C++ application:

  • Clob class to access and modify data stored in internal CLOBs and NCLOBs

  • Blob class to access and modify data stored in internal BLOBs

  • Bfile class to access and read data stored in external LOBs (BFILEs)

    See Also:

    Syntax information on these classes and details on OCCI in general is available in theOracle C++ Call Interface Programmer's Guide.

Clob Class

The Clob driver implements a CLOB object using an SQL LOB locator. This means that a CLOB object contains a logical pointer to the SQL CLOB data rather than the data itself.

The CLOB interface provides methods for getting the length of an SQL CLOB value, for materializing a CLOB value on the client, and getting a substring. Methods in the ResultSet and Statement interfaces such as getClob() and setClob() allow you to access SQL CLOB values.

See Also:

Oracle C++ Call Interface Programmer's Guide for detailed information on the Clob class.

Blob Class

Methods in the ResultSet and Statement interfaces, such as getBlob() and setBlob(), allow you to access SQL BLOB values. The Blob interface provides methods for getting the length of a SQL BLOB value, for materializing a BLOB value on the client, and for extracting a part of the BLOB.

See Also:

Bfile Class

The Bfile class enables you to instantiate a Bfile object in your C++ application. You must then use methods of the Bfile class, such as the setName() method, to initialize the Bfile object which associates the object properties with an object of type BFILE in a BFILE column of the database.

See Also:

Oracle C++ Call Interface Programmer's Guide for detailed information on the Bfile class methods and details on instantiating and initializing an Bfile object in your C++ application.

Fixed-Width Character Set Rules

In OCCI, for fixed-width client-side character sets, these rules apply:

  • Clob: offset and amount parameters are always in characters

  • Blob: offset and amount parameters are always in bytes

  • Bfile: offset and amount parameters are always in bytes

Varying-Width Character Set Rules

The following rules apply only to varying-width client-side character sets:

  • Offset parameter: Regardless of whether the client-side character set is varying-width, the offset parameter is always as follows:

    • Clob(): in characters

    • Blob(): in bytes

    • Bfile(): in bytes

  • Amount parameter: The amount parameter is always as indicated:

    • Clob: in characters, when referring to a server-side LOB

    • Blob: in bytes, when referring to a client-side buffer

    • Bfile: in bytes, when referring to a client-side buffer

  • length(): Regardless of whether the client-side character set is varying-width, the output length is as follows:

    • Clob.length(): in characters

    • Blob.length(): in bytes

    • Bfile.length(): in bytes

  • Clob.read() and Blob.read(): With client-side character set of varying-width, CLOBs and NCLOBs:

    • Input amount is in characters. Input amount refers to the number of characters to read from the server-side CLOB or NCLOB.

    • Output amount is in bytes. Output amount indicates how many bytes were read into the OCCI buffer parameter, buffer.

  • Clob.write() and Blob.write(): With client-side character set of varying-width, CLOBs and NCLOBs:

    • Input amount is in bytes. Input amount refers to the number of bytes of data in the OCCI input buffer, buffer.

    • Output amount is in characters. Output amount refers to the number of characters written into the server-side CLOB or NCLOB.

Offset and Amount Parameters for Other OCCI Operations

For all other OCCI LOB operations, irrespective of the client-side character set, the amount parameter is in characters for CLOBs and NCLOBs. These include the following:

  • Clob.copy()

  • Clob.erase()

  • Clob.trim()

  • For LoadFromFile functionality, overloaded Clob.copy()

All these operations refer to the amount of LOB data on the server.

NCLOBs in OCCI
  • NCLOB instances are allowed as parameters in methods

  • NCLOB instances are allowed as attributes in object types.

Amount Parameter for OCCI LOB copy() Methods

The copy() method on Clob and Blob enables you to load data from a BFILE. You can pass one of the following values for the amount parameter to this method:

  • An amount smaller than the size of the BFILE to load a portion of the data

  • An amount equal to the size of the BFILE to load all of the data

  • The UB8MAXVAL constant to load all of the BFILE data

You cannot specify an amount larger than the length of the BFILE.

Amount Parameter for OCCI read() Operations

The read() method on an Clob, Blob, or Bfile object, reads data from a BFILE. You can pass one of these values for the amount parameter to specify the amount of data to read:

  • An amount smaller than the size of the BFILE to load a portion of the data

  • An amount equal to the size of the BFILE to load all of the data

  • 0 (zero) to read until the end of the BFILE in streaming mode

You cannot specify an amount larger than the length of the BFILE.

Further Information About OCCI

See Also:

OCCI Methods That Operate on BLOBs, BLOBs, NCLOBs, and BFILEs

OCCI methods that operate on BLOBs, CLOBs, NCLOBs, and BFILEs are as follows:

OCCI Methods to Modify Persistent LOB (BLOB, CLOB, and NCLOB) Values

Table 10-15 OCCI Clob and Blob Methods to Modify Persistent LOB (BLOB, CLOB, and NCLOB) Values

Function/Procedure Description

Blob/Clob.append()

Appends CLOB or BLOB value to another LOB.

Blob/Clob.copy()

Copies all or part of a CLOB or BLOB to another LOB.

Blob/Clob.copy()

Loads BFILE data into a persistent LOB.

Blob/Clob.trim()

Truncates a CLOB or BLOB.

Blob/Clob.write()

Writes data from a buffer into a LOB, overwriting existing data.

OCCI Methods to Read or Examine Persistent LOB and BFILE Values

Table 10-16 OCCI Blob/Clob/Bfile Methods to Read or Examine persistent LOB and BFILE Values

Function/Procedure Description

Blob/Clob.getChunkSize()

Gets the chunk size used when reading and writing. This works on persistent LOBs and does not apply to external LOBs (BFILEs).

Blob/Clob.getOptions()

Obtains settings for existing and newly created LOBs.

Blob/Clob.length()

Returns the length of a LOB or a BFILE.

Blob/Clob.read()

Reads a specified portion of a non-NULL LOB or a BFILE into a buffer.

Blob/Clob.setOptions()

Enables LOB settings for existing and newly created LOBs.

OCCI Read-Only Methods for BFILEs

Table 10-17 OCCI Read-Only Methods for BFILES

Function/Procedure Description

Bfile.close()

Closes an open BFILE.

Bfile.fileExists()

Checks whether a BFILE exists.

Bfile.getFileName()

Returns the name of a BFILE.

Bfile.getDirAlias()

Gets the directory object name.

Bfile.isOpen()

Checks whether a BFILE is open.

Bfile.open()

Opens a BFILE.

Other OCCI LOB Methods

Table 10-18 Other OCCI LOB Methods

Methods Description

Clob/Blob/Bfile.operator=()

Assigns one LOB locator to another. Use = or the copy constructor.

Clob.getCharSetForm()

Returns the character set form of a LOB.

Clob.getCharSetId()

Returns the character set ID of a LOB.

Bfile.setName()

Sets the name of a BFILE.

Clob/Blob/Bfile.operator==()

Checks whether two LOB refer to the same LOB.

Clob/Blob/Bfile.isInitialized()

Checks whether a LOB is initialized.

OCCI Methods to Open and Close Internal and External LOBs

Table 10-19 OCCI Methods to Open and Close Internal and External LOBs

Function/Procedure Description

Clob/Blob/Bfile.Open()

Opens a LOB

Clob/Blob/Bfile.isOpen()

Sees if a LOB is open

Clob/Blob/Bfile.Close()

Closes a LOB

Using C/C++ (Pro*C) to Work With LOBs

You can make changes to an entire persistent LOB, or to pieces of the beginning, middle or end of a LOB by using embedded SQL. You can access both internal and external LOBs for read purposes, and you can write to persistent LOBs.

Embedded SQL statements allow you to access data stored in BLOBs, CLOBs, NCLOBs, and BFILEs. These statements are listed in the following tables, and are discussed in greater detail later in the chapter.

See Also:

Pro*C/C++ Programmer's Guidefor detailed documentation, including syntax, host variables, host variable types and example code.

Providing an Allocated Input Locator Pointer That Represents LOB

Unlike locators in PL/SQL, locators in Pro*C/C++ are mapped to locator pointers which are then used to refer to the LOB or BFILE value.

To successfully complete an embedded SQL LOB statement you must do the following:

  1. Provide an allocated input locator pointer that represents a LOB that exists in the database tablespaces or external file system before you run the statement.
  2. SELECT a LOB locator into a LOB locator pointer variable.
  3. Use this variable in the embedded SQL LOB statement to access and manipulate the LOB value.

    See Also:

    APIs for supported LOB operations are described in detail in:

Pro*C/C++ Statements That Operate on BLOBs, CLOBs, NCLOBs, and BFILEs

Pro*C/C++ statements that operate on BLOBs, CLOBs, and NCLOBs are listed in the following tables:

Pro*C/C++ Embedded SQL Statements to Modify Persistent LOB Values

Table 10-20 Pro*C/C++: Embedded SQL Statements to Modify Persistent LOB Values

Statement Description

APPEND

Appends a LOB value to another LOB.

COPY

Copies all or a part of a LOB into another LOB.

ERASE

Erases part of a LOB, starting at a specified offset.

LOAD FROM FILE

Loads BFILE data into a persistent LOB at a specified offset.

TRIM

Truncates a LOB.

WRITE

Writes data from a buffer into a LOB at a specified offset.

WRITE APPEND

Writes data from a buffer into a LOB at the end of the LOB.

Pro*C/C++ Embedded SQL Statements for Introspection of LOBs

Table 10-21 Pro*C/C++: Embedded SQL Statements for Introspection of LOBs

Statement Description

DESCRIBE [CHUNKSIZE]

Gets the chunk size used when writing. This works for persistent LOBs only. It does not apply to external LOBs (BFILEs).

DESCRIBE [LENGTH]

Returns the length of a LOB or a BFILE.

READ

reads a specified portion of a non-NULL LOB or a BFILE into a buffer.

Pro*C/C++ Embedded SQL Statements for Temporary LOBs

Table 10-22 Pro*C/C++: Embedded SQL Statements for Temporary LOBs

Statement Description

CREATE TEMPORARY

Creates a temporary LOB.

DESCRIBE [ISTEMPORARY]

Sees if a LOB locator refers to a temporary LOB.

FREE TEMPORARY

Frees a temporary LOB.

Pro*C/C++ Embedded SQL Statements for BFILEs

Table 10-23 Pro*C/C++: Embedded SQL Statements for BFILES

Statement Description

FILE CLOSE ALL

Closes all open BFILEs.

DESCRIBE [FILEEXISTS]

Checks whether a BFILE exists.

DESCRIBE [DIRECTORY,FILENAME]

Returns the directory object name and filename of a BFILE.

Pro*C/C++ Embedded SQL Statements for LOB Locators

Table 10-24 Pro*C/C++ Embedded SQL Statements for LOB Locators

Statement Description

ASSIGN

Assigns one LOB locator to another.

FILE SET

Sets the directory object name and filename of a BFILE in a locator.

Pro*C/C++ Embedded SQL Statements to Open and Close LOBs

Table 10-25 Pro*C/C++ Embedded SQL Statements to Open and Close Persistent LOBs and External LOBs (BFILEs)

Statement Description

OPEN

Opens a LOB or BFILE.

DESCRIBE [ISOPEN]

Sees if a LOB or BFILE is open.

CLOSE

Closes a LOB or BFILE.

Using COBOL (Pro*COBOL) to Work With LOBs

You can make changes to an entire persistent LOB, or to pieces of the beginning, middle or end of it by using embedded SQL. You can access both internal and external LOBs for read purposes, and you can also write to persistent LOBs.

Embedded SQL statements allow you to access data stored in BLOBs, CLOBs, NCLOBs, and BFILEs. These statements are listed in the following tables, and are discussed in greater detail later in the manual.

Providing an Allocated Input Locator Pointer That Represents LOB

Unlike locators in PL/SQL, locators in Pro*COBOL are mapped to locator pointers which are then used to refer to the LOB or BFILE value. For the successful completion of an embedded SQL LOB statement you must perform the following:

  1. Provide an allocated input locator pointer that represents a LOB that exists in the database tablespaces or external file system before you run the statement.
  2. SELECT a LOB locator into a LOB locator pointer variable
  3. Use this variable in an embedded SQL LOB statement to access and manipulate the LOB value.

    See Also:

    APIs for supported LOB operations are described in detail in:

Where the Pro*COBOL interface does not supply the required functionality, you can call OCI using C. Such an example is not provided here because such programs are operating system dependent.

See Also:

Pro*COBOL Programmer's Guidefor detailed documentation, including syntax, host variables, host variable types, and example code.

Pro*COBOL Statements That Operate on BLOBs, CLOBs, NCLOBs, and BFILEs

The following Pro*COBOL statements operate on BLOBs, CLOBs, NCLOBs, and BFILEs:

Pro*COBOL Embedded SQL Statements to Modify Persistent LOB Values

Table 10-26 Pro*COBOL Embedded SQL Statements to Modify LOB Values

Statement Description

APPEND

Appends a LOB value to another LOB.

COPY

Copies all or part of a LOB into another LOB.

ERASE

Erases part of a LOB, starting at a specified offset.

LOAD FROM FILE

Loads BFILE data into a persistent LOB at a specified offset.

TRIM

Truncates a LOB.

WRITE

Writes data from a buffer into a LOB at a specified offset

WRITE APPEND

Writes data from a buffer into a LOB at the end of the LOB.

Pro*COBOL Embedded SQL Statements for Introspection of LOBs

Table 10-27 Pro*COBOL Embedded SQL Statements for Introspection of LOBs

Statement Description

DESCRIBE [CHUNKSIZE]

Gets the Chunk size used when writing.

DESCRIBE [LENGTH]

Returns the length of a LOB or a BFILE.

READ

Reads a specified portion of a non-NULL LOB or a BFILE into a buffer.

Pro*COBOL Embedded SQL Statements for Temporary LOBs

Table 10-28 Pro*COBOL Embedded SQL Statements for Temporary LOBs

Statement Description

CREATE TEMPORARY

Creates a temporary LOB.

DESCRIBE [ISTEMPORARY]

Sees if a LOB locator refers to a temporary LOB.

FREE TEMPORARY

Frees a temporary LOB.

Pro*COBOL Embedded SQL Statements for BFILEs

Table 10-29 Pro*COBOL Embedded SQL Statements for BFILES

Statement Description

FILE CLOSE ALL

Closes all open BFILEs.

DESCRIBE [FILEEXISTS]

Checks whether a BFILE exists.

DESCRIBE [DIRECTORY, FILENAME]

Returns the directory object name and filename of a BFILE.

Pro*COBOL Embedded SQL Statements for LOB Locators

Table 10-30 Pro*COBOL Embedded SQL Statements for LOB Locator Statements

Statement Description

ASSIGN

Assigns one LOB locator to another.

FILE SET

Sets the directory object name and filename of a BFILE in a locator.

Pro*COBOL Embedded SQL Statements for Opening and Closing LOBs and BFILEs

Table 10-31 Pro*COBOL Embedded SQL Statements for Opening and Closing Persistent LOBs and BFILEs

Statement Description

OPEN

Opens a LOB or BFILE.

DESCRIBE [ISOPEN]

Sees if a LOB or BFILE is open.

CLOSE

Closes a LOB or BFILE.

Using Java (JDBC) to Work With LOBs

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

Modifying Internal Persistent LOBs Using Java

You can make changes to an entire persistent LOB, or to pieces of the beginning, middle, or end of a persistent LOB in Java by means of the JDBC API using the classes:

  • oracle.sql.BLOB

  • oracle.sql.CLOB

These classes implement java.sql.Blob and java.sql.Clob interfaces according to the JDBC 3.0 specification, which has methods for LOB modification. They also include legacy Oracle proprietary methods for LOB modification. These legacy methods are marked as deprecated.

Starting in Oracle Database Release 11.1, the minimum supported version of the JDK is JDK5. To use JDK5, place ojdbc5.jar in your CLASSPATH. To use JDK6, place ojdbc6.jar in your CLASSPATH. ojdbc5.jar supports the JDBC 3.0 specification and ojdbc6.jar supports the JDBC4.0 specification which is new with JDK6.

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
  • BLOB and CLOB Classes: In JDBC theses classes provide methods for performing operations on large objects in the database including BLOB and CLOB data types.

  • BFILE Class: In JDBC this class provides methods for performing operations on BFILE data in the database.

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.

Prefetching LOBs to Improve Performance

The number of server round trips can be reduced by prefetching part of the data and metadata (length and chunk size) along with the LOB locator during the fetch.

The SELECT parse, execution, and fetch occurs in one round trip. For large LOBs (larger than five times the prefetch size) less improvement is seen.

To configure the prefetch size, a connection property, oracle.jdbc.defaultLobPrefetchSize, defined as a constant in oracle.jdbc.OracleConnection can be used. Values can be -1 to disable prefetching, 0 to enable prefetching for metadata only, or any value greater than 0 which represents the number of bytes for BLOBs and characters for CLOBs, to be prefetched along with the locator during fetch operations.

You can change the prefetch size for a particular statement by using a method defined in oracle.jdbc.OracleStatement:

void setLobPrefetchSize(int size) throws SQLException;

The statement level setting overrides the setting at the connection level. This setting can also be overriden at the column level through the extended defineColumnType method, where the size represents the number of bytes (or characters for CLOB) to prefetch. The possible values are the same as for the connection property. The type must be set to OracleTypes.CLOB for a CLOB column and OracleTypes.BLOB for a BLOB column. This method throws SQLException if the value is less than -1. To complement the statement there is in oracle.jdbc.OracleStatement:

int getLobPrefetchSize();

Zero-Copy Input/Output for SecureFiles to Improve Performance

To improve the performance of SecureFiles, there is a Zero-copy Input/Output protocol on the server that is only available to network clients that support the new Net NS Data transfer protocol.

To determine if a LOB is a SecureFiles or not, use the method

public boolean isSecureFile() throws SQLException

If it is a SecureFiles, TRUE is returned.

Use this thin connection property to disable (by setting to FALSE) the Zero-copy Input/Output protocol:

oracle.net.useZeroCopyIO
Zero-Copy Input/Output on the Server

Oracle Net Services is now able to use data buffers provided by the users of Oracle Net Services without transferring the data into or out of its local buffers.

The network buffers (at the NS layer) are bypassed and internal lob buffers are directly written on the network. The same applies to buffer reads.

This feature is only available to network clients that support the new NS Data packet (this is negotiated during the NS handshake). The thin driver supports the new NS protocol so that the server can use the zero-copy protocol and JavaNet exposes the zero-copy IO mechanism to the upper layer so that data copies are no longer required in the thin driver code.

Zero-Copy Input/Output in the JDBC Thin Driver

When you call the BLOB.getBytes(long pos, int length, byte[] buffer) API, the buffer provided is used at the JavaNet layer to read the bytes from the socket.

The data is retrieved in one single round trip. Similarly, during a write operation, when you call BLOB.setBytes(long pos, byte[] bytes), the buffer is directly written on the network at the JavaNet layer. So the data is written in one single round trip. The user buffer is sent as a whole.

JDBC-OCI Driver Considerations

The JDBC-OCI driver supports Zero-copy Input/Output in the server and in the network layer.

Referencing LOBs Using Java (JDBC)

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

  • As a column of an OracleResultSet

  • As an OUT type PL/SQL parameter from an OraclePreparedStatement

Using OracleResultSet: BLOB and CLOB Objects Retrieved

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(), then the retrieved locator still refers to the original LOB row.

To retrieve the locator for the most current row, you must call getBLOB(), getCLOB(), or getBFILE() on the OracleResultSet each time a move operation is made depending on whether the instance is a BLOB, CLOB or BFILE.

JDBC Syntax References and Further Information

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

See Also:

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 10-32 JDBC oracle.sql.BLOB Methods To Modify BLOB Values

Method Description

int setBytes(long, byte[])

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

JDBC oracle.sql.BLOB Methods to Read or Examine BLOB Values

Table 10-33 JDBC 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 Streaming BLOB Data

Table 10-34 JDBC oracle.sql.BLOB Methods and Properties for Streaming BLOB Data

Method Description

public java.io.InputStream getBinaryStream())

Streams the LOB as a binary stream

public java.io.OutputStream setBinaryStream()

Retrieves a stream that can be used to write to the BLOB value that this Blob object represents

JDBC oracle.sql.CLOB Methods to Modify CLOB Values

Table 10-35 JDBC oracle.sql.CLOB Methods to Modify CLOB Values

Method Description

int setString(long, java.lang.String)

JDBC 3.0: Writes the given Java String to the CLOB value that this Clob object designates at the position pos.

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 10-36 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

long length()

Returns the length of the LOB

int getChunkSize()

Returns the ChunkSize of the LOB

JDBC oracle.sql.CLOB Methods and Properties for Streaming CLOB Data

Table 10-37 JDBC oracle.sql.CLOB Methods and Properties for Streaming CLOB Data

Method Description

java.io.InputStream getAsciiStream()

Implements the Clob interface method. Gets the CLOB value designated by this Clob object as a stream of ASCII bytes

java.io.OutputStream setAsciiStream(long pos)

JDBC 3.0: Retrieves a stream to be used to write ASCII characters to the CLOB value that this Clob object represents, starting at position pos

java.io.Reader getCharacterStream()

Reads the CLOB as a character stream

java.io.Writer setCharacterStream(long pos)

JDBC 3.0: Retrieves a stream to be used to write Unicode characters to the CLOB value that this Clob object represents, starting at position pos

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

Table 10-38 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

long length()

Returns the length of the BFILE

boolean fileExists()

Checks if the operating system file referenced by this BFILE exists

public void openFile()

Opens the operating system file referenced by this BFILE

public void closeFile()

Closes the operating system file referenced by this BFILE

public boolean isFileOpen()

Checks if this BFILE is open

public java.lang.String getDirAlias()

Gets the directory object name for this BFILE

public java.lang.String getName()

Gets the file name referenced by this BFILE

JDBC oracle.sql.BFILE Methods and Properties for Streaming BFILE Data

Table 10-39 JDBC oracle.sql.BFILE Methods and Properties for Streaming BFILE Data

Method Description

public java.io.InputStream getBinaryStream()

Reads the BFILE as a binary stream

JDBC Temporary LOB APIs

Oracle Database JDBC drivers contain APIs to create and close temporary LOBs. These APIs can replace workarounds that use the following procedures from the DBMS_LOB PL/SQL package in prior releases:

  • DBMS_LOB.createTemporary()

  • DBMS_LOB.isTemporary()

  • DBMS_LOB.freeTemporary()

Table 10-40 JDBC: Temporary BLOB APIs

Methods Description

public static BLOB createTemporary(Connection conn,

boolean cache, int duration) throws SQLException

Creates a temporary BLOB

public static boolean isTemporary(BLOB blob)

throws SQLException

Checks if the specified BLOB locator refers to a temporary BLOB

public boolean isTemporary() throws SQLException

Checks if the current BLOB locator refers to a temporary BLOB

public static void freeTemporary(BLOB temp_blob)

throws SQLException

Frees the specified temporary BLOB

public void freeTemporary() throws SQLException

Frees the temporary BLOB

Table 10-41 JDBC: Temporary CLOB APIs

Methods Description

public static CLOB createTemporary(Connection conn,

boolean cache, int duration) throws SQLException

Creates a temporary CLOB

public static boolean isTemporary(CLOB clob)

throws SQLException

Checks if the specified CLOB locator refers to a temporary CLOB

public boolean isTemporary() throws SQLException

Checks if the current CLOB locator refers to a temporary CLOB

public static void freeTemporary(CLOB temp_clob)

throws SQLException

Frees the specified temporary CLOB

public void freeTemporary() throws SQLException

Frees the temporary CLOB

JDBC: Opening and Closing LOBs

oracle.sql.CLOB class is the Oracle JDBC driver implementation of standard JDBC java.sql.Clob interface. Table 10-41 lists the Oracle extension APIs in oracle.sql.CLOB for accessing temporary CLOBs.

Oracle Database 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 implementation of standard JDBC java.sql.Blob interface. Table 10-42 lists the Oracle extension APIs in oracle.sql.BLOB that open and close BLOBs.

Table 10-42 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 Using JDBC

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 Open Using JDBC

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 Using JDBC

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 implementation of the standard JDBC java.sql.Clob interface. Table 10-43 lists the Oracle extension APIs in oracle.sql.CLOB to open and close CLOBs.

Table 10-43 JDBC: Opening and Closing CLOBs

Methods Description

public void open(int mode) throws SQLException

Open the CLOB

public boolean isOpen() throws SQLException

See if the CLOB is opened

public void close() throws SQLException

Close the CLOB

Opening the CLOB Using JDBC

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 Using JDBC

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 Using JDBC

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 10-44 lists the Oracle extension APIs in oracle.sql.BFILE for opening and closing BFILEs.

Table 10-44 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 read-only 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 must import the java.sql package to use JDBC 
import java.sql.*; 

// You must 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", "password"); 
    // It is 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. 
    } 

// 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 (); 
  } 
} 

Truncating LOBs Using JDBC

Oracle Database JDBC drivers contain APIs to truncate persistent LOBs. These APIs replace previous techniques that used DBMS_LOB.trim().

JDBC: Truncating BLOBs

oracle.sql.BLOB class is Oracle JDBC driver implementation of the standard JDBC java.sql.Blob interface. Table 10-45 lists the Oracle extension API in oracle.sql.BLOB that truncates BLOBs.

Table 10-45 JDBC: Truncating BLOBs

Methods Description

public void truncate(long newlen) throws SQLException

Truncates the BLOB

The truncate API is defined as follows:

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

The newlen parameter specifies the new length of the BLOB.

JDBC: Truncating CLOBs

oracle.sql.CLOB class is the Oracle JDBC driver implementation of standard JDBC java.sql.Clob interface. Table 10-46 lists the Oracle extension API in oracle.sql.CLOB that truncates CLOBs.

Table 10-46 JDBC: Truncating CLOBs

Methods Description

public void truncate(long newlen) throws SQLException

Truncates the CLOB

The truncate API is defined as follows:

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

The newlen parameter specifies the new length of the CLOB.

See:

"About Trimming LOB Data", for an example.

JDBC BLOB Streaming APIs

The JDBC interface provided with the database includes LOB streaming APIs that enable you to read from or write to a LOB at the requested position from a Java stream.

The oracle.sql.BLOB class implements the standard JDBC java.sql.Blob interface. Table 10-47 lists BLOB Streaming APIs.

Table 10-47 JDBC: BLOB Streaming APIs

Methods Description

public java.io.OutputStream

setBinaryStream (long pos) throws SQLException

JDBC 3.0: Retrieves a stream that can be used to write to the BLOB value that this Blob object represents, starting at position pos

public java.io.InputStream

getBinaryStream() throws SQLException

JDBC 3.0: Retrieves a stream that can be used to read the BLOB value that this Blob object represents, starting at the beginning

public java.io.InputStream

getBinaryStream(long pos) throws SQLException

Oracle extension: Retrieves a stream that can be used to read the BLOB value that this Blob object represents, starting at position pos

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 setBinaryStream(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

JDBC CLOB Streaming APIs

The oracle.sql.CLOB class is the Oracle JDBC driver implementation of standard JDBC java.sql.Clob interface. Table 10-48 lists the CLOB streaming APIs.

Table 10-48 JDBC: CLOB Streaming APIs

Methods Description

public java.io.OutputStream

setAsciiStream (long pos) throws SQLException

JDBC 3.0: Retrieves a stream to be used to write ASCII characters to the CLOB value that this Clob object represents, starting at position pos

public java.io.Writer

setCharacterStream (long pos) throws SQLException

JDBC 3.0: Retrieves a stream to be used to write Unicode characters to the CLOB value that this Clob object represents, starting, at position pos

public java.io.InputStream

getAsciiStream() throws SQLException

JDBC 3.0: Retrieves a stream that can be used to read ASCII characters from the CLOB value that this Clob object represents, starting at the beginning

public java.io.InputStream

getAsciiStream(long pos) throws SQLException

Oracle extension: Retrieves a stream that can be used to read ASCII characters from the CLOB value that this Clob object represents, starting at position pos

public java.io.Reader

getCharacterStream() throws SQLException

JDBC 3.0: Retrieves a stream that can be used to read Unicode characters from the CLOB value that this Clob object represents, starting at the beginning

public java.io.Reader

getCharacterStream(long pos) throws SQLException

Oracle extension: Retrieves a stream that can be used to read Unicode characters from the CLOB value that this Clob object represents, starting at position pos

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 setAsciiStream(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 setCharacterStream(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

BFILE Streaming APIs

oracle.sql.BFILE class wraps the database BFILEs. Table 10-49 lists the Oracle extension APIs in oracle.sql.BFILE that reads BFILE content from the requested position.

Table 10-49 JDBC: 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)
/* 
 * This sample shows how to read/write BLOB and CLOB as streams. 
 */ 

import java.io.*; 

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

// You must 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", "password"); 
    // It is 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. 
    } 

    // 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.setCharacterStream(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.setBinaryStream(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 an 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:

  • set APIs of PreparedStatement

  • update APIs of updatable result set

  • attribute value of STRUCTs

  • element value of ARRAYs

    Note:

    Empty LOBs are special marker LOBs but not real LOB values.

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

Oracle Provider for OLE DB (OraOLEDB)

Oracle Provider for OLE DB (OraOLEDB) offers high performance and efficient access to Oracle data for OLE DB and ADO developers.

Developers programming with COM, C++, or any COM client can use OraOLEDB to access Oracle databases.

OraOLEDB is an OLE DB provider for Oracle. It offers high performance and efficient access to Oracle data including LOBs, and also allows updates to certain LOB types.

The following LOB types are supported by OraOLEDB:

Overview of Oracle Data Provider for .NET (ODP.NET)

Oracle Data Provider for .NET (ODP.NET) is an implementation of a data provider for the Oracle database.

ODP.NET uses Oracle native APIs to offer fast and reliable access to Oracle data and features from any .NET application. ODP.NET also uses and inherits classes and interfaces available in the Microsoft .NET Framework Class Library. The ODP.NET supports the following LOBs as native data types with .NET: BLOB, CLOB, NCLOB, and BFILE.

COM and .NET are complementary development technologies. Microsoft recommends that developers use the .NET Framework rather than COM for new development.