Oracle8i JDBC Developer's Guide and Reference Release 8.1.5 A64685-01 |
|
This section contains the following subsections:
This section describes data access in oracle.sql.*
formats as opposed to Java formats. As discussed in the introduction to this chapter, the oracle.sql.*
formats are a key factor of the Oracle JDBC extensions, offering significant advantages in efficiency and precision in manipulating SQL data.
Using oracle.sql.*
formats involves casting your result sets and statements to OracleResultSet
, OracleStatement
, OraclePreparedStatement
, and OracleCallableStatement
objects as appropriate, and using the getOracleObject()
, setOracleObject()
, getXXX()
, and setXXX()
(where XXX
corresponds to the types in the oracle.sql
package) methods of these classes. Refer to the Javadoc for additional information about these classes and methods.
When JDBC programs retrieve SQL data into Java variables, the SQL data is converted to the Java datatypes of those variables. The Java datatypes can be represented as members of the oracle.sql
package instead of as members of the java.lang
or java.sql.Types
packages. In processing speed and effort, the oracle.sql.*
classes provide the most efficient way of representing SQL data. These classes store the usual representations of SQL data as byte arrays. They do not reformat the data or perform any character-set conversions (aside from the usual network conversions) on it. The data remains in SQL format; therefore, no information is lost. For SQL primitive types (such as NUMBER
, and CHAR
), the oracle.sql.*
classes simply wrap the SQL data. For SQL structured types (such as objects and arrays), the classes provide additional information such as conversion methods and structure details.
If you are moving data within the database, then you will probably want to keep your data in oracle.sql.*
format. If you are displaying the data, or performing calculations on it in a Java application running outside of the database, then you will probably want to represent the data as a member of java.sql.Types.*
or java.lang.*
. Similarly, if you are using a parser that expects the data to be in Java format, you must represent the data in one of the Java formats instead of as an oracle.sql.*
.
Java represents a SQL NULL
datum by the Java value null
. Java datatypes fall into two categories: the fixed set of scalar types (such as byte
, int
, float
) and object types (such as objects and arrays). The Java scalar types cannot represent null
. Instead, they store the null as the value zero (as defined by the JDBC specification). This can lead to ambiguity when you try to interpret your results.
In contrast, Java object types can represent null
. The Java language defines an object wrapper type corresponding to every scalar type (for example, Integer
for int
, Float
for float
) that can represent null
. The object wrapper types must be used as the targets for SQL data to detect SQL NULL
without ambiguity.
The JDBC Statement
object returns an OracleResultSet
object, typed as a java.sql.ResultSet
. If you want to apply only standard JDBC methods to the object, keep it as a ResultSet
type. However, if you want to use the Oracle extensions on the object, you must cast it to an OracleResultSet
type. The object is unchanged. The type by which the Java compiler will identify the object is changed.
When you execute a SELECT
statement in a Java application using a standard JDBC Statement
object, Oracle's JDBC drivers return a java.sql.ResultSet
object. You can use this standard ResultSet
object if all you need are standard JDBC ResultSet
methods, but to use Oracle extensions you must cast the result set to an OracleResultSet
object.
For example, assuming you have a standard Statement
object stmt
, do the following if you want to use only standard JDBC ResultSet
methods:
ResultSet rs = stmt.executeQuery("SELECT * FROM emp");
If you need the extended functionality provided by the Oracle extensions to JDBC, you can select the results into a standard ResultSet
object, as above, and then cast that object into an OracleResultSet
object later.
Similarly, when you want to execute a stored procedure using a callable statement, the JDBC drivers will return an OracleCallableStatement
object typed as a java.sql.CallableStatement
. If you want to apply only standard JDBC methods to the object, then keep it as a CallableStatement
type. However, if you want to use the Oracle extensions on the object, you must cast it to an OracleCallableStatement
type. The object is unchanged. The type by which the Java compiler identifies the object is changed.
You use the standard JDBC java.sql.Connection.prepareStatement()
method to create a PreparedStatement
object. If you want to apply only standard JDBC methods to the object, keep it as a PreparedStatement
type. However, if you want to use the Oracle extensions on the object, you must cast it to an OraclePreparedStatement
type. The object is unchanged. The type by which the Java compiler identifies the object is changed.
Key extensions to the result set and statement classes include getOracleObject()
and setOracleObject()
methods that you can use to access and manipulate data in oracle.sql.*
formats instead of standard Java formats. For more information see the next section: "Comparing get and set Methods for oracle.sql.* Format with Java Format".
This section describes get
and set
methods, particularly the JDBC standard getObject()
and setObject()
methods and the Oracle-specific getOracleObject()
and setOracleObject()
methods, and how to access data in oracle.sql.*
format compared with Java format.
Although there are specific getXXX()
methods for all of the Oracle SQL types (as described in "Other getXXX() Methods"), you can use the general get
methods for convenience or simplicity, or if you are not certain in advance what type of data you will receive.
The standard JDBC getObject()
method of a result set or callable statement returns data into a java.lang.Object
object. The format of the data returned is based on its original type, as follows:
getObject()
returns the default Java type corresponding to the column's SQL type, following the mapping specified in the JDBC specification.
ROWID
, discussed in "Additional Type Extensions"), getObject()
returns an object of the appropriate oracle.sql.*
class (such as oracle.sql.ROWID
).
getObject()
returns an object of the Java class specified in your type map. (Type maps specify the correlation between Java classes and database SQL types and are discussed in "Understanding Type Maps".) The getObject(
parameter_index
)
method uses the connection's default type map. The getObject(
parameter_index
,
map
)
enables you to pass in a type map. If the type map does not provide a mapping for a particular Oracle object, then getObject()
returns an oracle.sql.STRUCT
object.
For more information on getObject()
return types, see Table 4-3, "Summary of getObject() and getOracleObject() Return Types".
If you want to retrieve data from a result set or callable statement into an oracle.sql.*
object, then cast your result set to an OracleResultSet
type or your callable statement to an OracleCallableStatement
type and use the getOracleObject()
method.
When you use getOracleObject()
, the data will be of the appropriate oracle.sql.*
type and is returned into an Datum
object. The prototype for the method is:
public oracle.sql.Datum getOracleObject(int parameter_index
)
When you have retrieved data into a Datum
object, you can use the standard Java instanceOf()
operator to determine which oracle.sql.*
type it really is.
For more information on getOracleObject()
return types, see Table 4-3, "Summary of getObject() and getOracleObject() Return Types".
The following example creates a table that contains a column of character data (in this case, a row number) and a column containing a BFILE
locator. A SELECT
statement gets the contents of the table into a result set. The getOracleObject()
then retrieves the CHAR
data into the char_datum
variable and the BFILE
locator into the bfile_datum
variable. Note that because getOracleObject()
returns a Datum
object, the results must be cast to CHAR
and BFILE
respectively.
stmt.execute ("CREATE TABLE bfile_table (x varchar2 (30), b bfile)"); stmt.execute ("INSERT INTO bfile_table VALUES ('one', bfilename ('TEST_DIR', 'file1'))"); ResultSet rset = stmt.executeQuery ("SELECT * FROM string_table"); while (rset.next ()) { CHAR char_datum = (CHAR) ((OracleResultSet)rset).getOracleObject (1); BFILE bfile_datum = (BFILE) ((OracleResultSet)rset).getOracleObject (2); ... }
The following example prepares a call to the procedure myGetDate()
, which associates a character string (in this case a name) with a date. The program passes the string SCOTT
to the prepared call, and registers the DATE
type as an output parameter. After the call is executed, getOracleObject()
retrieves the date associated with the name SCOTT
. Note that since getOracleObject()
returns a Datum
object, the results are cast to a DATE
object.
OracleCallableStatement cstmt = (OracleCallableStatement)conn.prepareCall ("begin myGetDate (?, ?); end;"); cstmt.setString (1, "SCOTT"); cstmt.registerOutParameter (2, Types.DATE); cstmt.execute (); DATE date = (DATE) ((OracleCallableStatement)cstmt).getOracleObject (2); ...
Table 4-3 summarizes the information in the preceding sections, "Standard getObject() Method" and "Oracle getOracleObject() Method".
This table lists the underlying return types for each method for each Oracle SQL type, but keep in mind the signatures of the methods when you write your code:
getObject()
always returns data into a java.lang.Object
getOracleObject()
always returns data into an oracle.sql.Datum
You must cast the returned object to use any special functionality (see "Casting Your get Method Return Values").
For information on type compatibility between all SQL and Java types, see Table 8-1, "Valid SQL Datatype-Java Class Mappings".
Standard JDBC provides a getXXX()
for each standard Java type, such as getByte()
, getInt()
, getFloat()
, and so on. Each of these returns exactly what the method name implies (a byte
, an int
, a float
, and so on).
In addition, the OracleResultSet
and OracleCallableStatement
classes provide a full complement of getXXX()
methods corresponding to all of the oracle.sql.*
types. Each getXXX()
method returns an oracle.sql.XXX
. For example, getROWID()
returns an oracle.sql.ROWID
.
Some of these extensions are taken from the JDBC 2.0 specification. They return objects of type oracle.jdbc2.*
instead of oracle.sql.*
. For example, compare the prototypes:
oracle.jdbc2.Blob getBlob(int
parameter_index
)
which returns an oracle.jdbc2
type for BLOB
s, in contrast to:
oracle.sql.BLOB getBLOB(int
parameter_index
)
which returns an oracle.sql
type for BLOB
s.
Although there is no particular performance advantage in using the specific getXXX()
methods, they can save you the trouble of casting because they return specific object types.
Table 4-4 summarizes the underlying return types and the signature types for each getXXX()
method. You must cast to an OracleResultSet
or OracleCallableStatement
to use methods that are Oracle-specific.
As described in "Standard getObject() Method", Oracle's implementation of getObject()
always returns a java.lang.Object
and getOracleObject()
always returns an oracle.sql.Datum
. Usually, you would cast the returned object to the appropriate class so that you could use particular methods and functionality of that class.
In addition, you have the option of using a specific getXXX()
method instead of the generic getObject()
or getOracleObject()
methods. The getXXX()
methods enable you to avoid casting because the return type of getXXX()
corresponds to the type of object returned. For example, getCLOB()
returns an oracle.sql.CLOB
as opposed to a java.lang.Object
.
This example assumes that you have fetched data of type CHAR
into a result set (where it is in column 1). Because you want to manipulate the CHAR
data without losing precision, cast your result set to an OracleResultSet
ors
and use getOracleObject()
to return the CHAR
data. (If you do not cast your result set, you have to use getObject()
, which returns your character data into a Java String
and loses some of the precision of your SQL data.) By casting the result set, you can use getOracleObject()
and return data in oracle.sql.*
format.
The getOracleObject()
method returns an oracle.sql.CHAR
object into an oracle.sql.Datum
return variable unless you cast the output. Cast the getOracleObject()
output to oracle.sql.CHAR
if you want to use a CHAR
return variable and later use any special functionality of that class (such as the getCharacterSet()
method that returns the character set used to represent the characters).
CHAR char = (CHAR)ors.getOracleObject(1); CharacterSet cs = char.getCharacterSet();
Alternatively, return into a generic oracle.sql.Datum
return variable and cast this object later whenever you must use the CHAR
getCharacterSet()
method.
Datum rawdatum = ors.getOracleObject(1); ... CharacterSet cs = ((CHAR)rawdatum).getCharacterSet();
This uses the getCharacterSet()
method of oracle.sql.CHAR
. The getCharacterSet()
method is not defined on oracle.sql.Datum
and would not be reachable without the cast.
Just as there is a standard getObject()
and Oracle-specific getOracleObject()
in result sets and callable statements for retrieving data, there is also a standard setObject()
and an Oracle-specific setOracleObject()
in Oracle prepared statements and callable statements for updating data. The setOracleObject()
methods take oracle.sql.*
input parameters.
You can use the setObject()
method to bind standard Java types to a prepared statement or callable statement; it takes a java.lang.Object
as input. You can use the setOracleObject()
method to bind oracle.sql.*
types; it takes an oracle.sql.Datum
(or any subclass) as input. The setObject()
method supports some oracle.sql.*
types--see note below. For other oracle.sql.*
types, you must use setOracleObject()
.
To use setOracleObject()
, you must cast your prepared statement or callable statement to an OraclePreparedStatement
or OracleCallableStatement
object.
This example assumes that you have fetched character data into a standard result set (where it is in column 1), and you want to cast the results to an OracleResultSet
so that you can use Oracle-specific formats and methods. Since you want to use the data as oracle.sql.CHAR
format, cast the results of the getOracleObject()
(which returns type oracle.sql.Datum
) to CHAR
. Similarly, since you want to manipulate the data in column 2 as strings, cast the data to a Java String
type (since getObject()
returns data of type Object
). In this example, rs
represents the result set, charVal
represents the data from column 1 in oracle.sql.CHAR
format, and strVal
represents the data from column 2 in Java String
format.
CHAR charVal=(CHAR)((OracleResultSet)rs).getOracleObject(1); String strVal=(String)rs.getObject(2); ...
For some prepared statement ps
, the setOracleObject()
method binds the oracle.sql.CHAR
data represented by the charVal
variable to the prepared statement. To bind the oracle.sql.*
data, the prepared statement must be cast to an OraclePreparedStatement
. Similarly, the setObject()
method binds the Java String
data represented by the variable strVal
.
PreparedStatement ps= conn.prepareStatement("text_of_prepared_statement
");
((OraclePreparedStatement)ps).setOracleObject(1,charVal);
ps.setObject(2,strVal);
As with getXXX()
methods, there are several specific setXXX()
methods. Standard setXXX()
methods are provided for binding standard Java types, and Oracle-specific setXXX()
methods are provided for binding Oracle-specific types.
In addition, for compatibility with the JDBC 2.0 standard, OraclePreparedStatement
and OracleCallableStatement
classes provide setXXX()
methods that take oracle.jdbc2
input parameters for BLOB
s, CLOB
s, object references, and arrays. For example, there is a setBlob()
method that takes an oracle.jdbc2.Blob
input parameter, and a setBLOB()
method that takes an oracle.sql.BLOB
input parameter.
Similarly, there are two forms of the setNull()
method:
void setNull(int
parameterIndex
, int
sqlType
)
behaves in a similar way to the standard Java java.sql.PreparedStatement.setNull()
. This method takes a parameter index and a SQL type code defined by java.sql.Types
. You use this method to set an object (except for REF
s, ARRAY
s, or STRUCT
s) to NULL
.
void setNull(int
parameterIndex
, int
sqlType
, String
sql_type_name
)
takes a SQL type name in addition to a parameter index and a SQL type code. You use this method only when the SQL type code is REF
, ARRAY
, or STRUCT
.
Similarly, the OracleCallableStatement
.registerOutParameter()
method also has an overloaded method that you use when working with REF
s, ARRAY
s, or STRUCT
s.
void registerOutParameter(int
parameterIndex
, intsqlType
, Stringsql_type_name
)
There is no particular performance advantage in using the specific setXXX()
methods for binding Oracle-specific types over the methods for binding standard Java types.
Table 4-5 summarizes the input types for all of the setXXX()
methods. To use methods that are Oracle-specific, you must cast your statement to an OraclePreparedStatement
or OracleCallableStatement
.
For information on type compatibility between all SQL and Java types, see Table 8-1, "Valid SQL Datatype-Java Class Mappings".
Although the oracle.jdbc.driver.OracleResultSetMetaData
class does not implement the full JDBC 2.0 API for retrieving result set meta data, it does provide many methods to retrieve information about an Oracle result set.
The getColumnTypeName()
method takes a column number and returns the SQL type name for columns of type REF
, STRUCT
, or ARRAY
. In contrast, the getColumnType()
method takes a column number and returns the SQL type. If the column stores an Oracle object or collection, then it returns an OracleTypes.STRUCT
or an OracleTypes.ARRAY
. For a list of the key methods provided by OracleResultSetMetadata
, see "Class oracle.jdbc.driver.OracleResultSetMetaData".
The following example uses several of the methods in the OracleResultSetMetadata
class to retrieve the number of columns from the EMP
table, and each column's numerical type and SQL type name.
DatabaseMetaData dbmd = conn.getMetaData(); ResultSet rset = dbmd.getTables("", "SCOTT", "EMP", null); while (rset.next()) { OracleResultSetMetaData orsmd = ((OracleResultSet)rset).getMetaData(); int numColumns = orsmd.getColumnCount(); System.out.println("Num of columns = " + numColumns); for (int i=0; i<numColumns; i++) { System.out.print ("Column Name=" + orsmd.getColumnName (i+1)); System.out.print (" Type=" + orsmd.getColumnType (i + 1) ); System.out.println (" Type Name=" + orsmd.getColumnTypeName (i + 1)); } }
The program returns the following output:
Num of columns = 5 Column Name=TABLE_CAT Type=12 Type Name=VARCHAR2 Column Name=TABLE_SCHEM Type=12 Type Name=VARCHAR2 Column Name=TABLE_NAME Type=12 Type Name=VARCHAR2 Column Name=TABLE_TYPE Type=12 Type Name=VARCHAR2 Column Name=TABLE_REMARKS Type=12 Type Name=VARCHAR2