Whole document tree
8 New SQL TypesThe next two chapters discuss additions to the JDBC API that allow a Java application to access new SQL data types, such as binary large objects and structured types. JDBC drivers that do not support the new SQL types need not implement the methods and interfaces described in these chapters.8.1 Taxonomy of SQL TypesThe latest version of the ANSI/ISO SQL standard is commonly referred to as SQL99. The JDBC API incorporates a model of the new SQL99 types that includes only those properties that are essential to exchanging data between Java applications and databases. The JDBC API should not be affected if some details of the syntax and server-side semantics of the new SQL99 types are altered before the draft becomes an official standard.The SQL99 draft specifies these data types:
A The remainder of this chapter discusses the default mechanism provided by the JDBC API for accessing each of the new SQL types mentioned above. The JDBC API also provides a means for customizing the mapping of SQL distinct and structured types into Java classes. This mechanism is discussed in the Chapter 9. 8.2 Blobs and clobs8.2.1 Retrieving blobs and clobsThe binary large object (blob) and character large object (clob) data types are treated similarly to the existing, built-in types defined in the JDBC API. Values of these types can be retrieved by calling thegetBlob( ) and getClob( ) methods that appear on the
ResultSet and CallableStatement interfaces. For example,
retrieves a blob value from the first column of the result set and a clob value from the second column. TheBlob blob = rs.getBlob(1); Clob clob = rs.getClob(2); Blob interface contains operations for returning the length of the
blob, a specific range of bytes contained in the blob, etc. The Clob interface contains
corresponding operations that are character based. See the accompanying API documentation
for more details.
An application does not deal directly with the LOCATOR(blob) and LOCATOR(clob)
types that are defined in SQL. By default, a JDBC driver should implement the 8.2.2 Storing blobs and clobsABlob or Clob value can be passed as an input parameter to a PreparedStatement
object just like other data types by calling the setBlob() and setClob() methods respectively.
The setBinaryStream() , and setObject() methods may be used to input
a stream value as a blob. The setAsciiStream() , setUnicodeStream() , and setObject()
methods may be used to input a stream as a clob value.
8.2.3 Metadata additionsTwo new type codes,BLOB and CLOB, have been added to java.sql.Types. These
values are returned by methods such as DatabaseMetaData.getTypeInfo() and DatabaseMetaData.getColumns()
when a JDBC driver supports these data types.
8.3 Arrays8.3.1 Retrieving arraysData of type SQL array can be retrieved by calling thegetArray() method of the ResultSet
and CallableStatement interfaces. For example,
retrieves anArray a = rs.getArray(1); Array value from the first column of the result set. By default, a JDBC driver
should implement the Array interface using an SQL LOCATOR(array) internally.
Also, by default Array objects only remain valid during the transaction in which they
are created. These defaults may be changed as for the Blob and Clob types, but the
JDBC API does not specify how this is done.
The 8.3.2 Storing arraysThePreparedStatement.setArray() method may be called to pass an Array value
as an input parameter to a prepared statement. A Java array may be passed as an input
parameter by calling PreparedSatement.setObject() .
8.3.3 Metadata additionsA new type code,ARRAY, has been added to java.sql.Types. This value is returned
by methods such as DatabaseMetaData.getTypeInfo() and DatabaseMetaData.getColumns()
when a JDBC driver supports the Array data type.
8.4 Refs8.4.1 Retrieving refsAn SQL reference can be retrieved by calling thegetRef() method of the ResultSet
and CallableStatement interfaces. For example,
retrieves aRef ref = rs.getRef(1); Ref value from the first column of the result set. By default, retrieving a Ref
value does not materialize the data to which the Ref refers. Also, by default a Ref value
remains valid while the session or connection on which it is created is open. These defaults
may be overridden, but again the JDBC API does not specify how this is done.
The 8.4.2 Storing refsThePreparedStatement.setRef() method may be called to pass a Ref as an input
parameter to a prepared statement.
8.4.3 Metadata additionsA new type code,REF , has been added to java.sql.Types. This value is returned by
methods such as DatabaseMetaData.getTypeInfo() and DatabaseMetaData.getColumns()
when a JDBC driver supports the Ref data type.
8.5 Distinct types8.5.1 Retrieving distinct typesBy default, a datum of SQL type DISTINCT is retrieved by calling anygetXXX() method
that is appropriate to the underlying type that the distinct type is based on. For example,
given the following type declaration:
CREATE TYPE MONEY AS NUMERIC(10,2) a value of type MONEY could be retrieved as follows:
since the underlying SQL NUMERIC type is mapped to thejava.math.BigDecimal bd = rs.getBigDecimal(1); java.math.BigDecimal
type.
8.5.2 Storing distinct typesAnyPreparedStatement.setXXX() method that is appropriate to the underlying type
of an SQL DISTINCT type may be used to pass an input parameter of that distinct type
to a prepared statement. For example, given the definition of type MONEY above PreparedStatement.setBigDecimal()
would be used.
8.5.3 Metadata additionsA new type code,DISTINCT , has been added to java.sql.Types. This value is returned
by methods such as DatabaseMetaData.getTypeInfo() and DatabaseMetaData.getColumns()
when a JDBC driver supports this data type.
An SQL DISTINCT type must be defined as part of a particular database schema before
it is used in a schema table definition. Information on schema-specific user-defined
types-of which
returns descriptions of all the SQL DISTINCT types defined in the Each type description has the following columns:
Most of the columns above should be self-explanatory. The TYPE_NAME is the SQL type name given to the DISTINCT type-MONEY in the example above. This is the name used in a CREATE TABLE statement to specify a column of this type.
When DATA_TYPE is 8.6 Structured types8.6.1 Retrieving structured typesA value of an SQL structured type is always retrieved by calling methodgetObject() .
By default, getObject() returns a value of type Struct for a structured type. For example,
Struct struct = (Struct)rs.getObject(1);
retrieves a 8.6.2 Storing structured typesThePreparedStatement.setObject() method may be called to pass a Struct as an
input parameter to a prepared statement.
8.6.3 Metadata additionsA new type code,STRUCT , has been added to java.sql.Types. This value is returned
by methods such as DatabaseMetaData.getTypeInfo() and DatabaseMetaData.getColumns()
when a JDBC driver supports structured data types.
A structured SQL type must be defined as part of a particular database schema before
it can be used in a schema table definition. Information on schema-specific user-defined
types-of which
returns descriptions of all the structured SQL types defined in the
When the DATA_TYPE returned by
Contents | Prev | Next jdbc@eng.sun.com or jdbc-business@eng.sun.com Copyright © 1996-1999 Sun Microsystems, Inc. All rights reserved. |