Documentation

The Java™ Tutorials
Hide TOC
Using Advanced Data Types使用高级数据类型
Trail: JDBC Database Access
Lesson: JDBC Basics

Using Advanced Data Types使用高级数据类型

The advanced data types introduced in this section give a relational database more flexibility in what can be used as a value for a table column. 本节中介绍的高级数据类型为关系数据库提供了更大的灵活性,使其可以用作表列的值。For example, a column can be used to store BLOB (binary large object) values, which can store very large amounts of data as raw bytes. 例如,一列可用于存储BLOB(二进制大对象)值,该值可将大量数据存储为原始字节。A column can also be of type CLOB (character large object), which is capable of storing very large amounts of data in character format.列也可以是CLOB(字符大对象)类型,它能够以字符格式存储大量数据。

The latest version of the ANSI/ISO SQL standard is commonly referred to as SQL:2003. ANSI/ISO SQL标准的最新版本通常称为SQL:2003。This standard specifies the following data types:本标准规定了以下数据类型:

Mapping Advanced Data Types映射高级数据类型

The JDBC API provides default mappings for advanced data types specified by the SQL:2003 standard. JDBCAPI为SQL:2003标准指定的高级数据类型提供默认映射。The following list gives the data types and the interfaces or classes to which they are mapped:下表给出了数据类型及其映射到的接口或类:

Using Advanced Data Types使用高级数据类型

You retrieve, store, and update advanced data types the same way you handle other data types. 检索、存储和更新高级数据类型的方式与处理其他数据类型的方式相同。You use either ResultSet.getDataType or CallableStatement.getDataType methods to retrieve them, PreparedStatement.setDataType methods to store them, and ResultSet.updateDataType methods to update them. (The variable DataType is the name of a Java interface or class mapped to an advanced data type.) Probably 90 percent of the operations performed on advanced data types involve using the getDataType, setDataType, and updateDataType methods. The following table shows which methods to use:下表显示了要使用的方法:

Advanced Data Type高级数据类型 getDataType Method setDataType method updateDataType Method
BLOB getBlob setBlob updateBlob
CLOB getClob setClob updateClob
NCLOB getNClob setNClob updateNClob
ARRAY getArray setArray updateArray
XML getSQLXML setSQLXML updateSQLXML
Structured type getObject setObject updateObject
REF(structured type) getRef setRef updateRef
ROWID getRowId setRowId updateRowId
DISTINCT getBigDecimal setBigDecimal updateBigDecimal
DATALINK getURL setURL updateURL

Note: The DISTINCT data type behaves differently from other advanced SQL data types. Being a user-defined type that is based on an already existing built-in types, it has no interface as its mapping in the Java programming language. Consequently, you use the method that corresponds to the Java type on which the DISTINCT data type is based. See Using DISTINCT Data Type for more information.

For example, the following code fragment retrieves a SQL ARRAY value. For this example, suppose that the column SCORES in the table STUDENTS contains values of type ARRAY. The variable stmt is a Statement object.

ResultSet rs = stmt.executeQuery(
    "SELECT SCORES FROM STUDENTS " +
    "WHERE ID = 002238");
rs.next();
Array scores = rs.getArray("SCORES");

The variable scores is a logical pointer to the SQL ARRAY object stored in the table STUDENTS in the row for student 002238.变量scores是一个逻辑游标,指向存储在student 002238行中STUDENTS表中的SQLARRAY对象。

If you want to store a value in the database, you use the appropriate set method. 如果要在数据库中存储值,请使用适当的set方法。For example, the following code fragment, in which rs is a ResultSet object, stores a Clob object:例如,以下代码片段(rsResultSet对象)存储Clob对象:

Clob notes = rs.getClob("NOTES");
PreparedStatement pstmt =
    con.prepareStatement(
        "UPDATE MARKETS SET COMMENTS = ? " +
        "WHERE SALES < 1000000");
pstmt.setClob(1, notes);
pstmt.executeUpdate();

This code sets notes as the first parameter in the update statement being sent to the database. 此代码将notes设置为发送到数据库的update语句中的第一个参数。The Clob value designated by notes will be stored in the table MARKETS in column COMMENTS in every row where the value in the column SALES is less than one million.notes指定的Clob值将存储在表MARKETSSALES列值小于一百万的每一行的COMMENTS列中。


Previous page: Using WebRowSet Objects
Next page: Using Large Objects