Oracle8i JDBC Developer's Guide and Reference Release 8.1.5 A64685-01 |
|
This section discusses the Java packages that support the Oracle JDBC extensions and the key classes that are included in these packages. This section has the following subsections:
You can refer to the Javadoc for more information about all of the classes mentioned in this section.
The oracle.jdbc2
package contains the Oracle implementation of the standard JDBC 2.0 interfaces. The JDBC 2.0 interfaces are part of the java.sql
package included in the JDK 1.2. However, since the drivers do not currently support JDK 1.2, these interfaces have been made available to the Oracle 1.0.2 and 1.1.x drivers as the oracle.jdbc2
package. This package contains the JDBC 2.0 features of the JDK 1.2 java.sql
package that the Oracle drivers support.
The following interfaces are implemented by oracle.sql.*
type classes for JDBC 2.0-compliant Oracle type extensions. These interfaces are equivalent to the interfaces published by Sun Microsystems; the oracle.jdbc2
versions add no new features.
oracle.jdbc2.Array
is implemented by oracle.sql.ARRAY
oracle.jdbc2.Struct
is implemented by oracle.sql.STRUCT
oracle.jdbc2.Ref
is implemented by oracle.sql.REF
oracle.jdbc2.Clob
is implemented by oracle.sql.CLOB
oracle.jdbc2.Blob
is implemented by oracle.sql.BLOB
In addition, Oracle includes the following standard JDBC 2.0 interfaces for users employing the JDBC-standard SQLData
interface to create Java classes that map to Oracle objects:
oracle.jdbc2.SQLData
implemented by classes that map to Oracle objects; users must provide this implementation
oracle.jdbc2.SQLInput
implemented by classes that read object data; Oracle provides a SQLInput
class that the JDBC drivers use
oracle.jdbc2.SQLOutput
implemented by classes that write object data; Oracle provides a SQLOutput
class that the JDBC drivers use
The SQLData
interface is one of the two features you can use to support Oracle objects in Java. The other feature is the Oracle CustomDatum
interface, contained in the oracle.sql
package. See "Understanding the SQLData Interface" for more information about SQLData
, SQLInput
, and SQLOutput
.
The oracle.sql
package supports direct access to data in SQL format and consists primarily of classes that map to the Oracle SQL datatypes.
These classes provide Java mappings for the Oracle SQL types and are wrapper classes for the raw SQL data. Because data in an oracle.sql.*
object remains in SQL format, no information is lost. For SQL primitive types, these classes simply wrap the SQL data. For SQL structured types (objects and arrays), they provide additional information such as conversion methods and details of structure.
Each of the Oracle datatype classes extends oracle.sql.Datum
, a superclass that encapsulates functionality common to all of the datatypes. Some of the classes are for JDBC 2.0-compliant datatypes. These classes, as Table 4-1 indicates, implement standard JDBC 2.0 interfaces in the oracle.jdbc2
package, as well as extending oracle.sql.Datum
.
Table 4-1 lists the oracle.sql
datatype classes and their corresponding Oracle SQL types.
The following sections describe each class listed in Table 4-1. Additional details about use of the Oracle extended types (STRUCT
, REF
, ARRAY
, BLOB
, CLOB
, BFILE
, and ROWID
) are described in "Working with LOBs", "Working with Oracle Object References", "Working with Arrays", and "Additional Type Extensions".
Notes:
|
In addition to the datatype classes, the oracle.sql
package includes these support classes and interfaces:
oracle.sql.ArrayDescriptor
class: used in constructing oracle.sql.ARRAY
objects; describes the SQL type of the array. See "Class oracle.sql.ARRAY" for more information.
oracle.sql.StructDescriptor
class: used in constructing oracle.sql.STRUCT
objects, which you can use as a default mapping to Oracle objects in the database. See "Class oracle.sql.STRUCT" for more information.
oracle.sql.CharacterSet
and oracle.sql.CharacterSetFactory
classes: used in constructing character set objects, which in turn are used in constructing oracle.sql.CHAR
objects. See "Class oracle.sql.CHAR" for more information.
oracle.sql.CustomDatum
and oracle.sql.CustomDatumFactory
interfaces: used in Java classes implementing the Oracle CustomDatum
scenario of Oracle object support. (The other possible scenario is the JDBC-standard SQLData
implementation. See "Understanding the CustomDatum Interface" for more information on CustomDatum
.)
Refer to the Javadoc for additional information about these classes. The rest of this section further describes the oracle.sql.*
classes.
Each of the Oracle datatype classes provides, among other things, the following:
getBytes()
method, which returns the SQL data as a byte array
toJdbc()
method that converts the data into an object of a corresponding Java class as defined in the JDBC specification
The JDBC driver does not convert Oracle-specific datatypes that are not part of the JDBC specification, such as ROWID
; the driver returns the object in the corresponding oracle.sql.*
format. For example, it returns an Oracle ROWID
as an oracle.sql.ROWID
.
stringValue()
or intValue()
method, where appropriate, to convert the SQL data to a String
or an int
get
, and set
methods as appropriate for the functionality of the datatype (such as methods in the LOB classes that get the data as a stream, and methods in the REF
class that get and set object data through the object reference)
Refer to the Javadoc for additional information about these classes.
For any given Oracle object type, if you do not specify a mapping to a Java class in your connection's type map, data from the object type will be materialized in Java in an instance of the oracle.sql.STRUCT
class.
The STRUCT
class implements the standard JDBC 2.0 oracle.jdbc2.Struct
class and extends oracle.sql.Datum
.
In the database, Oracle stores the raw bytes of object data in a linearized form. A STRUCT
object is a wrapper for the raw bytes of an Oracle object and contains a "values" array of oracle.sql.Datum
objects holding the attribute values in SQL format. The STRUCT
object also contains the SQL type name of the Oracle object.
In most cases you will probably want to create a custom Java type definition class to map to your Oracle object, although using the STRUCT
class may suffice in some cases (see "Using STRUCT Objects"). The attributes of a STRUCT
can be materialized as java.lang.Object[]
objects if you use the getAttributes()
method, or as oracle.sql.Datum[]
objects if you use the getOracleAttributes()
method. The oracle.sql.*
format gives you the same advantages as using oracle.sql.*
datatype classes in general:
STRUCT
class completely preserves data, because it maintains the data in SQL format. This is useful if you want to manipulate data but not necessarily display it.
In some cases you might want to manually create a STRUCT
object to pass it to a prepared statement or callable statement. To do this, you must also create a StructDescriptor
object. For more information on creating a STRUCT
object, see "Creating STRUCT Objects and Descriptors".
The STRUCT
class includes the following methods:
getAttributes()
: retrieves the values from the values array, using the type map (if one has been defined) to determine which Java classes to use in materializing the data. Conceptually, getAttributes()
returns a Java array containing the attribute values. The types of the attribute values are those that a call to getObject()
on the same underlying types will return. That is, they are the "default" JDBC types for the corresponding underlying types.
For example, assume that you have defined a SQL type PERSON
with a name attribute of type CHAR
and an age attribute of type NUMBER
. If you use getAttributes()
to get the object attributes of PERSON
, then it will return the name as a Java String
type and the age as a Java BigDecimal
type.
If you are calling getAttributes()
on a nested object, then you can optionally specify a type map (java.util.Map
object) if you do not want to use your connection's default type map.
getOracleAttributes()
: retrieves the values of the values array as oracle.sql.*
objects
getSQLTypeName()
: returns the fully qualified type name (schema.sql_type_name) of the Oracle object that this STRUCT
represents
getDescriptor()
: returns the StructDescriptor
object for this STRUCT
object (see "Creating STRUCT Objects and Descriptors" for information about the StructDescriptor
class)
getConnection()
: returns the current connection
getDescriptor()
: returns the OracleType
that identifies the Oracle object type
getMap()
: returns the current type map
isConvertibleTo(Class)
: determines if a datum object can be converted to a particular class
makeJdbcArray(int)
: returns a JDBC array representation of the datum
setDatumArray(Datum[])
: sets the Datum
array.
setDescriptor(StructDescriptor)
: sets the descriptor
stringValue()
: converts to a String representation of the datum object
toBytes()
: packs the bytes representing the attributes into the format that is actually used in the database
toClass(Class)
: applies the normal algorithms for converting a SQL structured type to a specific Java class
toJdbc()
: consults the current map to determine what class to convert to, and then uses toClass()
toJdbc(Dictionary)
: consults the map to determine what class to convert to, and then uses toClass()
toSTRUCT(Object, OracleConnection)
: returns the corresponding STRUCT
object from the input Java object
To create an oracle.sql.STRUCT
object, a STRUCT descriptor must first exist for the given Oracle object type. This descriptor is an object of the oracle.sql.StructDescriptor
class.
A StructDescriptor
describes a type of SQL structured object (Oracle object). Only one StructDescriptor
is necessary for each Oracle object type.
The driver caches STRUCT
descriptor objects to avoid recreating them if the type has already been encountered. The Oracle JDBC extensions provide a static createDescriptor()
method that will either construct a new StructDescriptor
object or return an existing one.
To create a StructDescriptor
object, pass in a Java string parameter with the SQL type name of the Oracle object type and a connection object to the StructDescriptor.createDescriptor()
method:
StructDescriptor structdesc = StructDescriptor.createDescriptor(sql_type_name, connection);
where sql_type_name
is a Java string containing the name of the Oracle object type (such as EMPLOYEE
) and connection
is your connection object.
You can also call the StructDescriptor
object if you need to create a new STRUCT
object. To construct a new StructDescriptor
object, pass in a Java string parameter with the SQL type name of the Oracle object type and your connection object:
StructDescriptor structdesc = new StructDescriptor(sql_type_name, connection);
To construct a STRUCT
object, pass in the StructDescriptor
, your connection object, and an array of Java objects containing the attributes you want the STRUCT
to contain.
STRUCT struct = new STRUCT(structdesc, connection, attributes);
where structdesc
is the StructDescriptor
created previously, connection
is your connection object, and attributes
is an array of type java.lang.Object[]
.
A STRUCT
descriptor can be referred to as a "type object." This means that it contains information about the type code and type name of the object type and how to convert to and from the given type. Remember, there should be only one StructDescriptor
object for any one Oracle object type. You can then use that descriptor to create as many STRUCT
objects as you need for that type.
The StructDescriptor
class includes the getName()
method to return the fully qualified SQL type name of the Oracle object (that is, in schema.sql_type_name format. For example, CORPORATE.EMPLOYEE
)
The JDBC driver seamlessly handles embedded objects (STRUCT
objects that are attributes of STRUCT
objects) in the same way that it normally handles objects. When the JDBC driver retrieves an attribute that is an object, it follows the same rules of conversion, using the type map if it is available, or else using default mapping.
The oracle.sql.REF
class is the generic class that supports Oracle object references. This class, as with all of the oracle.sql.*
datatype classes, is a subclass of oracle.sql.Datum
. It implements the standard JDBC 2.0 oracle.jdbc2.Ref
interface.
Selecting a REF
retrieves only a pointer to an object; it does not materialize the object. However, there are methods to accomplish this.
The oracle.sql.REF
class includes the following methods:
getValue()
: retrieves object attributes (using your type map as necessary)
setValue()
: sets object attributes (using your type map as necessary)
getBaseTypeName()
: retrieves the fully-qualified SQL structured type name of the referenced item
The setREF()
and setRef()
methods of the OraclePreparedStatement
and OracleCallableStatement
classes support passing a REF
object as an input parameter to a prepared statement. Similarly, the getREF()
and getRef()
methods of the OracleCallableStatement
and OracleResultSet
support passing a REF
object as an output parameter.
You cannot create REF
objects using JDBC.
For more information on how to use REF
objects, see "Working with Oracle Object References".
The oracle.sql.ARRAY
class supports Oracle collections, either varray
s or nested tables. If you select either a varray
or nested table from the database, then the JDBC driver materializes it as an object of the ARRAY
class; the structure of the data is equivalent in either case. The oracle.sql.ARRAY
class extends oracle.sql.Datum
(as do all of the oracle.sql.*
classes) and implements oracle.jdbc2.Array
, a standard JDBC 2.0 array interface.
You might want to manually create an ARRAY
object to pass it to a prepared statement or callable statement, perhaps to insert into the database. This involves the use of ArrayDescriptor
objects, which "Creating ARRAY Objects and Descriptors" describes.
The ARRAY
class includes the following methods:
getArray()
: retrieves the contents of the array in "default" JDBC types. If it retrieves an array of objects, then getArray()
uses the type map to determine the types
getOracleArray()
: identical to getArray()
, but retrieves the elements in oracle.sql.*
format
getArrayDescriptor()
: returns the ArrayDescriptor
object that pertains to this array (see "Creating ARRAY Objects and Descriptors" for information about the ArrayDescriptor
class)
getBaseType()
: returns the SQL type code for the array elements (see "Class oracle.jdbc.driver.OracleTypes" for information about type codes)
getSQLTypeName()
: returns the SQL type name of the array elements
getBaseTypeName()
: for named types (such as Oracle objects), returns the particular type name (for example, EMPLOYEE
)
getResultSet()
: materializes an array as a result set
The setARRAY()
method of the OraclePreparedStatement
or OracleCallableStatement
class supports passing an array as an input parameter to a prepared statement. You must first construct an array descriptor, which is an oracle.sql.ArrayDescriptor
object, and then you must construct the oracle.sql.ARRAY
object for the array you want to pass.
An ArrayDescriptor
object describes the SQL type of an array; however, you need only one array descriptor for any one SQL type. You can reuse the same descriptor object to create multiple instances of an oracle.sql.Array
object for the same array type.
Collections are strongly typed. Oracle supports only "named arrays", that is, an array given a SQL type name. For example, when you create an array with the CREATE TYPE
statement:
CREATE TYPE num_varray AS varray(22) OF NUMBER(5,2);
the SQL type name for the collection type is num_varray
.
To construct an ArrayDescriptor
object, pass the SQL type name of the collection type and your Connection
object (which JDBC uses to go to the database to gather meta data) to the constructor.
ArrayDescriptor arraydesc = ArrayDescriptor.createDescriptor(sql_type_name, connection);
where sql_type_name
is the type name of the array and connection
is your Connection
object.
To construct an ARRAY
object, pass in the array descriptor, your connection object, and a Java object containing the individual elements you want the array to contain.
ARRAY array = new ARRAY(arraydesc, connection, elements);
where arraydesc
is the array descriptor created previously, connection
is your connection object, and elements
is a Java array of objects. The two possibilities for the contents of elements
are:
int[]
.
xxx
[]
where xxx
represents the name of a Java object type.) For example, Integer[]
.
An array descriptor can be referred to as a type object, meaning it has information about the array's SQL type name, the type code of the array's elements and, if the array is a STRUCT
, the type name of the elements. The array descriptor also contains the information on how to convert to and from the given type. You need only one array descriptor object for any one type, then you can use that descriptor to create as many arrays of that type as you want.
The ArrayDescriptor
class has the following methods for retrieving an element's type code and type name:
getBaseType()
: returns the integer type code associated with this array descriptor (according to integer constants defined in the OracleTypes
class, which "Classes of the oracle.jdbc.driver Package" describes)
getBaseName()
: returns a string with the type name associated with this array element if it is a STRUCT
, REF
or collection
BLOB
s, CLOB
s, and BFILE
s, all referred to as LOBs, are for data items that are too large to store directly in the database table. Instead, the database table stores a locator that points to the location of the actual data.
The oracle.sql
package supports LOBs in several ways:
BLOB
s point to large unstructured binary data items and are supported by the oracle.sql.BLOB
class.
CLOB
s point to large fixed-width character data items (that is, characters that require a fixed number of bytes per character) and are supported by the oracle.sql.CLOB
class.
BFILE
s point to the content of external files (operating system files) and are supported by the oracle.sql.BFILE
class.
You can select a BLOB
, CLOB
, or BFILE
locator from the database using a standard SELECT
statement, but bear in mind that you are receiving only the locator, not the data itself. Additional steps are necessary to retrieve the data. This is described in "Working with LOBs".
The oracle.sql.BLOB
class includes the following methods:
getBinaryOutputStream()
: returns the BLOB
data
getBinaryStream()
: returns the BLOB
designated by this Blob
instance as a stream of bytes
getBytes()
: reads from the BLOB
data, starting at a specified point, into a supplied buffer
length()
: returns the length of the BLOB
in bytes
position()
: determines the byte position in the BLOB
where a given pattern begins
putBytes()
: writes BLOB
data, starting at a specified point, from a supplied buffer
The oracle.sql.CLOB
class includes the following methods:
getAsciiOutputStream()
: writes CLOB
data from an ASCII stream
getAsciiStream()
: returns the CLOB
value designated by the Clob
object as a stream of Ascii bytes
getCharacterOutputStream()
: writes CLOB
data from a Unicode stream
getCharacterStream()
: returns the CLOB
data as a stream of Unicode characters
getChars()
: retrieves characters from a specified point in the CLOB
data into a character array
length()
: returns the length of the CLOB
in characters
position()
: determines the character position in the CLOB
at which a given substring begins
putChars()
: writes characters from a character array to a specified point in the CLOB
data
getSubString()
: retrieves a substring from a specified point in the CLOB
data
putString()
: writes a string to a specified point in the CLOB
data
The oracle.sql.BFILE
class includes the following methods:
openFile()
: opens the external file
closeFile()
: closes the external file
getBinaryStream()
: returns the contents of the external file as a stream of bytes
getBytes()
: reads from the external file, starting at a specified point, into a supplied buffer
getName()
: gets the name of the external file
getDirAlias()
: gets the directory alias of the external file
length()
: returns the length of the BFILE
in bytes
position()
: determines the byte position at which the given byte pattern begins
The CHAR
class has special functionality for NLS conversion of character data. A key attribute of the CHAR
class, and a parameter always passed in when a CHAR
object is constructed, is the NLS character set used in presenting the character data. Without the character set being known, the bytes of data in the CHAR
object are meaningless.
CHAR
objects that the driver constructs and returns can be in the database character set, UTF-8
, or ISO-Latin-1 (WE8ISO8859P1
). CHAR
objects which are Oracle8 objects, are returned in the database character set.
JDBC constructs and populates CHAR
objects once character data has been read from the database. Additionally, you might want to construct a CHAR
object yourself (to pass in to a prepared statement, for example).
When you construct a CHAR
object, you must provide character set information to the CHAR
object by way of an instance of the oracle.sql.CharacterSet
class. Each instance of the CharacterSet
class represents one of the NLS character sets that Oracle supports. A CharacterSet
instance encapsulates methods and attributes of the character set, mainly involving functionality to convert to or from other character sets. You can find a complete list of the character sets that Oracle supports in the Oracle8i National Language Support Guide.
If you use a CHAR
object based on a character set that Oracle does not support, then the JDBC driver will not be able to perform character set conversions with it. For example, you will not be able to use the CHAR
object in an OraclePreparedStatement.setOracleObject()
call.
Follow these general steps to construct a CHAR
object:
CharacterSet
instance by calling the static CharacterSet.make()
method. This method is a factory for the character set class. It takes as input an integer OracleId
, which corresponds to a character set that Oracle supports. For example:
int oracleId = CharacterSet.JA16SJIS_CHARSET; // this is character set 832 ... CharacterSet mycharset = CharacterSet.make(OracleId);
Each character set that Oracle supports has a unique predefined OracleId
. If you enter an invalid OracleId
, an exception will not be thrown. Instead, when you try to use the character set, you will receive unpredictable results. For more information on character sets and character set IDs, see the Oracle8i National Language Support Guide.
CHAR
object. Pass to the constructor a string (or the bytes that represent the string) and the CharacterSet
object that indicates how to interpret the bytes based on the character set. For example:
String mystring = "teststring"; ... CHAR mychar = new CHAR(teststring, mycharset);
The CHAR
class has multiple constructors: they can take a string, a byte array, or an object as input along with the CharacterSet
object. In the case of a string, the string is converted to the character set indicated by the CharacterSet
object before being placed into the CHAR
object.
Refer to the CHAR
class Javadoc for more information.
The CHAR
class provides these methods for translating character data to strings:
getString()
: converts the sequence of characters represented by the CHAR
object to a string, returning a Java String
object. If the character set is not recognized (that is, if you entered an invalid OracleID
), then getString()
throws a SQLException
.
toString()
: identical to getString()
, but if the character set is not recognized (that is, if you entered an invalid OracleID
), then toString()
returns a hexadecimal representation of the CHAR
data and does not throw a SQLException
.
getStringWithReplacement()
: identical to getString()
, except a default replacement character replaces characters that have no Unicode representation in the character set of this CHAR
object. This default character varies from character set to character set, but is often a question mark.
The server (database) and the client (or application running on the client) can use different character sets. When you use the methods of this class to transfer data between the server and the client, the JDBC drivers must convert the data from the server character set to the client character set (or vice versa). To convert the data, the drivers use Oracle's National Language Support (NLS). For more information on how the JDBC drivers convert between character sets, see "Using NLS". For more information on NLS, see the Oracle8i National Language Support Guide.
These classes map to primitive SQL datatypes, which are a part of standard JDBC. These classes provide conversions to and from their corresponding JDBC Java types. For more information, see the Javadoc.
This class supports Oracle ROWID
s, which are unique identifiers for rows in database tables. You can select a ROWID
as you would select any column of data from the table. Note, however, that you cannot manually update ROWID
s; the Oracle database updates them automatically as appropriate.
The oracle.sql.ROWID
class does not implement any noteworthy functionality beyond what is in the oracle.sql.Datum
superclass. However, ROWID
does provide a stringValue()
method that overrides the stringValue()
method in the oracle.sql.Datum
class and returns the hexadecimal representation of the ROWID
bytes.
For information about accessing ROWID
data, see "Additional Oracle Extensions".
The oracle.jdbc.driver
package includes classes that add extended features to enable data access in oracle.sql
format. In addition, these classes provide Oracle-specific extensions to allow access to raw SQL format data by using oracle.sql.*
objects.
Table 4-2 lists key classes for connections, statements, and result sets in this package.
The oracle.jdbc.driver
package additionally includes:
The stream classes extend standard Java stream classes and read and write Oracle LOB
, LONG
, and LONG
RAW
data.
OracleTypes
defines integer constants, which identify SQL types. For standard types, it uses the same values as the standard java.sql.Types
. In addition, it adds constants for Oracle extended types.
The remainder of this section describes the classes of the oracle.jdbc.driver
package. For more information about using these classes to access Oracle type extensions, see "Data Access and Manipulation: Oracle Types vs. Java Types".
Use this class to register the Oracle JDBC drivers for use by your application. You can input a new instance of this class to the static registerDriver()
method of the java.sql.DriverManager
class so that your application can access and use the Oracle drivers. The registerDriver()
method takes as input a "driver" class; that is, a class that implements the java.sql.Driver
interface, as is the case with OracleDriver
.
Once you register the Oracle JDBC drivers, you can create your connection using the DriverManager
class. For more information on registering drivers and writing a connection string, see "First Steps in JDBC".
This class extends standard JDBC connection functionality to create and return Oracle statement objects, set flags and options for Oracle performance extensions, and support type maps for Oracle objects.
"Performance Extensions" describes the performance extensions, including row prefetching, update batching, and metadata TABLE_REMARKS
reporting.
Key methods include:
createStatement()
: allocates a new OracleStatement
object
prepareStatement()
: allocates a new OraclePreparedStatement
object
prepareCall()
: allocates a new OracleCallableStatement
object
getTransactionIsolation()
: gets this connection's current isolation mode
setTransactionIsolation()
: changes the transaction isolation level using one of the TRANSACTION_*
values
These oracle.jdbc.driver.OracleConnection
methods are Oracle-defined extensions:
getDefaultExecuteBatch()
: retrieves the default update-batching value for this connection
setDefaultExecuteBatch()
: sets the default update-batching value for this connection
getDefaultRowPrefetch()
: retrieves the default row-prefetch value for this connection
setDefaultRowPrefetch()
: sets the default row-prefetch value for this connection
getRemarksReporting()
: returns true if TABLE_REMARKS
reporting is enabled
setRemarksReporting()
: enables or disables TABLE_REMARKS
reporting
getTypeMap()
: retrieves the type map for this connection (for use in mapping Oracle object types to Java classes)
setTypeMap()
: initializes or updates the type map for this connection (for use in mapping Oracle object types to Java classes)
This class extends standard JDBC statement functionality and is the superclass of the OraclePreparedStatement
and OracleCallableStatement
classes. Extended functionality includes support for setting flags and options for Oracle performance extensions on a statement-by-statement basis, as opposed to the OracleConnection
class that sets these on a connection-wide basis.
"Performance Extensions" describes the performance extensions, including row prefetching and column type definitions.
Key methods include:
executeQuery()
: executes a database query and returns an OracleResultSet
object
getResultSet()
: retrieves an OracleResultSet
object
close()
: closes the current statement
These oracle.jdbc.driver.OracleStatement
methods are Oracle-defined extensions:
defineColumnType()
: defines the type you will use to retrieve data from a particular database table column
getRowPrefetch()
: retrieves the row-prefetch value for this statement
setRowPrefetch()
: sets the row-prefetch value for this statement
This class extends standard JDBC prepared statement functionality, is a subclass of the OracleStatement
class, and is the superclass of the OracleCallableStatement
class. Extended functionality consists of set
methods for binding oracle.sql.*
types and objects into prepared statements, and methods to support Oracle performance extensions on a statement-by-statement basis.
"Performance Extensions" describes the performance extensions, including database update batching.
Key methods include:
getExecuteBatch()
: retrieves the update-batching value for this statement
setExecuteBatch()
: sets the update-batching value for this statement
setOracleObject()
: a generic set
method for binding oracle.sql.*
data into a prepared statement as an oracle.sql.Datum
object
setXXX()
: set
methods, such setBLOB()
, for binding specific oracle.sql.*
types into prepared statements. For more information on all of the setXXX()
methods available for oracle.sql.*
types, see the Javadoc.
setCustomDatum()
: binds a CustomDatum
object (for use in mapping Oracle object types to Java) into a prepared statement
setNull()
: sets the value of the object specified by its SQL type name to NULL
. For setNull(param_index, type_code, sql_type_name
), if type_code
is REF
, ARRAY
, or STRUCT
, then sql_type_name
is the fully qualified name (schema.sql_type_name) of the SQL type.
close()
: closes the current statement
This class extends standard JDBC callable statement functionality and is a subclass of the OracleStatement
and OraclePreparedStatement
classes. Extended functionality includes set
methods for binding structured objects and oracle.sql
.* objects into prepared statements, and get
methods for retrieving data into oracle.sql.*
objects.
Key methods include:
getOracleObject()
: a generic get
method for retrieving data into an oracle.sql.Datum
object. It can be cast to the specific oracle.sql.*
type as necessary.
getXXX()
: get
methods, such as getCLOB()
, for retrieving data into specific oracle.sql.*
objects. For more information on all of the getXXX()
methods available for oracle.sql.*
types, see the Javadoc.
setOracleObject()
: a generic set
method for binding oracle.sql.*
data into a callable statement as an oracle.sql.Datum
object
setXXX()
: set
methods inherited from OraclePreparedStatement
, such as setBLOB()
, for binding specific oracle.sql.*
objects into callable statements. For more information on all of the setXXX()
methods available for oracle.sql.*
types, see the Javadoc.
setNull()
: sets the value of the object specified by its SQL type name to NULL
. For setNull(param_index, type_code, sql_type_name
), if type_code
is REF
, ARRAY
, or STRUCT
, then sql_type_name
is the fully qualified (schema.type) name of the SQL type.
registerOutParameter()
: registers the SQL type code of the statement's output parameter. JDBC requires this for any callable statement with an OUT
parameter. It takes an integer parameter index (the position of the output variable in the statement, relative to the other parameters) and an integer SQL type (the type constant defined in oracle.jdbc.driver.OracleTypes
).
This is an overloaded method. There is a version of this method that you use for named types only; that is, when the SQL type code is OracleTypes.REF
, STRUCT
, or ARRAY
. In this case, in addition to a parameter index and SQL type, the method also takes a String
SQL type name (the name of the Oracle object type in the database, such as EMPLOYEE
).
close()
: closes the current result set, if any, and the current statement
This class extends standard JDBC result set functionality, implementing get
methods for retrieving data into oracle.sql.*
objects.
Key methods include:
getOracleObject()
: a generic get
method for retrieving data into an oracle.sql.Datum
object. It can be cast to the specific oracle.sql.*
type as necessary.
getXXX()
: get
methods, such as getCLOB()
, for retrieving data into oracle.sql.*
objects
next()
: advances to the next row of the result set
This class extends standard JDBC result set metadata functionality to retrieve information about Oracle result set objects.
Key methods include the following:
getColumnCount()
: returns the number of columns in an Oracle result set
getColumnName()
: returns the name of a specified column in an Oracle result set
getColumnType()
: returns the SQL type of a specified column in an Oracle result set. If the column stores an Oracle object or collection, then this method returns OracleTypes.STRUCT
or OracleTypes.ARRAY
respectively.
getColumnTypeName()
: returns the SQL type name of the data stored in the column. If the column stores an array or collection, then this method returns its SQL type name. If the column stores REF
data, then this method returns the SQL type name of the objects to which the REF
points.
getTableName()
: returns the name of the table from which an Oracle result set column was selected
Oracle uses many stream classes that extend standard Java stream classes to provide special functionality, such as writing directly to an Oracle database. The JDBC drivers use these classes which are in the oracle.jdbc.driver
package but does not intend them for use by Java applications programmers. For more information on Java streams, see "Using Java Streams in JDBC".
The OracleTypes
class defines constants that JDBC uses to identify SQL types. Each variable in this class has a constant integer value. The oracle.jdbc.driver.OracleTypes
class contains a copy of the standard Java java.sql.Types
class and contains these additional Oracle type extensions:
OracleTypes.STRUCT
OracleTypes.REF
OracleTypes.ARRAY
OracleTypes.BLOB
OracleTypes.CLOB
OracleTypes.BFILE
OracleTypes.ROWID
As in java.sql.Types
, all of the variable names are in all-caps.
JDBC uses the SQL types identified by the elements of the OracleTypes
class in two main areas: registering output parameters and in the setNull()
method of the PreparedStatement
class.
The SQL types in the OracleTypes
class identify the SQL type of the output parameters in the registerOutParameter()
method of the java.sql.CallableStatement
and oracle.jdbc.driver.OracleCallableStatement
classes.
These are the forms that registerOutputParameter()
can take for CallableStatement
and OracleCallableStatement
:
CallableStatement.registerOutParameter(int index, int sqlType) CallableStatement.registerOutParameter(int index, int sqlType, int scale) OracleCallableStatement.registerOutParameter(int index, int sqlType, String sql_name)
In these prototypes, index
represents the parameter index, sqlType
represents the SQL datatype (one of the OracleTypes
, in this case), sql_name
represents the name given to the datatype (that is, the "named type"), and scale
represents the number of digits to the right of the decimal point when sqlType
is a NUMERIC
or DECIMAL
datatype.
Any output parameter datatype except STRUCT
, ARRAY
, or REF
can use the two forms of CallableStatement.registerOutParameter()
.
The OracleCallableStatement
form of registerOutParameter()
can be used only when the output parameter is of type STRUCT
, ARRAY
, or REF
and requires you to provide the name of the named type.
The following example uses a CallableStatement
to call a procedure named procout
, which returns a CHAR
datatype. Note the use of the OracleTypes.CHAR
SQL name in the registerOutParameter()
method.
CallableStatement procout = conn.prepareCall ("BEGIN procout (?); END;"); procout.registerOutParameter (1, OracleTypes.CHAR); procout.execute (); System.out.println ("Out argument is: " + procout.getString (1));
The next example uses a CallableStatement
to call procout
, which returns a STRUCT
datatype. The form of registerOutParameter()
requires you to specify the name of the SQL type, OracleTypes.STRUCT
, as well as the SQL type name (that is, the name of the named type) EMPLOYEE
.
The example assumes that no type mapping has been declared for the EMPLOYEE
type, so it is retrieved into a STRUCT
datatype. To retrieve the value of EMPLOYEE
into the default STRUCT
datatype, the statement object procout
is cast to an OracleCallableStatement
and the getSTRUCT()
is applied.
CallableStatement procout = conn.prepareCall ("BEGIN procout (?); END;"); procout.registerOutParameter (1, OracleTypes.STRUCT, "EMPLOYEE"); procout.execute (); // get the value into a STRUCT because it // is assumed that no type map has been defined STRUCT emp = ((OracleCallableStatement)procout).getSTRUCT (1);
The SQL types in the OracleTypes
class identify the object, which the setNull()
method sets to NULL
. The setNull()
method can be found in the java.sql.PreparedStatement
and oracle.jdbc.driver.OraclePreparedStatement
classes.
These are the forms that setNull()
can take for PreparedStatement
and OraclePreparedStatement
classes:
PreparedStatement.setNull(int index, int sqlType) OraclePreparedStatement.setNull(int index, int sqlType, String sql_name)
In these prototypes, index
represents the parameter index, sqlType
represents the SQL datatype (one of the OracleTypes
, in this case), and sql_name
represents the name given to the datatype (that is, the name of the "named type"). If you enter an invalid sqlType
, a "Parameter Type Conflict"
error is thrown.
You can use the PreparedStatement
form of setNull()
to set to NULL
the value of an object of any datatype, except STRUCT
, ARRAY
, or REF
.
You can use the OraclePreparedStatement
form of setNull()
only when you set to NULL
the value of an object of datatype STRUCT
, ARRAY
, or REF
.
The following example uses a PreparedStatement
to insert a NULL
numeric value into the database. Note the use of OracleTypes.NUMERIC
to identify the numeric object that is set to NULL
.
PreparedStatement pstmt = conn.prepareStatement ("INSERT INTO num_table VALUES (?)"); pstmt.setNull (1, OracleTypes.NUMERIC); pstmt.execute ();
In this example, the prepared statement inserts a NULL
STRUCT
object of type EMPLOYEE
into the database. Note that an OraclePreparedStatement
is required to set a STRUCT
object to NULL
. Thus, the prepared statement pstmt
must be cast to OraclePreparedStatement
.
PreparedStatement pstmt = conn.prepareStatement ("INSERT INTO employee_table VALUES (?)"); ((OraclePreparedStatement)pstmt).setNull(1, OracleTypes.STRUCT, "EMPLOYEE"); pstmt.execute ();