Jdbc4.0 FR Spec
Jdbc4.0 FR Spec
0 Specification
JSR 221
Lance Andersen, Specification Lead
November 7, 2006
Version: 4.0
Please
Recycle
including all its required interfaces and functionality; (b) does not
modify, subset, superset or otherwise extend the Licensor Name Space, or
include any public or protected packages, classes, Java interfaces,
fields or methods within the Licensor Name Space other than those
required/authorized by the Specification or Specifications being
implemented; and (c) passes the Technology Compatibility Kit (including
satisfying the requirements of the applicable TCK Users Guide) for such
Specification ("Compliant Implementation"). In addition, the foregoing
license is expressly conditioned on your not acting outside its scope.
No license is granted hereunder for any other purpose (including, for
example, modifying the Specification, other than to the extent of your
fair use rights, or distributing the Specification to third parties).
Also, no right, title, or interest in or to any trademarks, service
marks, or trade names of Sun or Sun's licensors is granted hereunder.
Java, and Java-related logos, marks and names are trademarks or
registered trademarks of Sun Microsystems, Inc. in the U.S. and other
countries.
c Also with respect to any patent claims owned by Sun and covered by the
license granted under subparagraph 2 above, where the infringement of
such claims can be avoided in a technically feasible manner when
implementing the Specification such license, with respect to such
claims, shall terminate if You initiate a claim against Sun that its
making, having made, using, offering to sell, selling or importing a
Compliant Implementation infringes Your patent rights.
This Agreement will terminate immediately without notice from Sun if you
breach the Agreement or act outside the scope of the licenses granted above.
DISCLAIMER OF WARRANTIES
THE SPECIFICATION IS PROVIDED "AS IS". SUN MAKES NO REPRESENTATIONS OR
WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
NON-INFRINGEMENT (INCLUDING AS A CONSEQUENCE OF ANY PRACTICE OR
IMPLEMENTATION OF THE SPECIFICATION), OR THAT THE CONTENTS OF THE
SPECIFICATION ARE SUITABLE FOR ANY PURPOSE. This document does not
represent any commitment to release or implement any portion of the
Specification in any product. In addition, the Specification could
include technical inaccuracies or typographical errors.
LIMITATION OF LIABILITY
You will indemnify, hold harmless, and defend Sun and its licensors from
any claims arising or resulting from: (i) your use of the Specification;
(ii) the use or distribution of your Java application, applet and/or
implementation; and/or (iii) any claims that later versions or releases
of any Specification furnished to you are incompatible with the
Specification provided to you under this license.
GENERAL TERMS
Preface 1
Typographic Conventions 2
Submitting Feedback 2
1. Introduction 3
1.1 The JDBC API 3
1.2 Platforms 3
1.3 Target Audience 4
1.4 Acknowledgements 4
2. Goals 7
2.1 History 7
2.2 Overview of Goals 7
4. Overview 15
4.1 Establishing a Connection 15
4.2 Executing SQL Statements and Manipulating Results 16
4.2.1 Support for SQL Advanced Data Types 17
ix
4.3 Two-tier Model 17
4.4 Three-tier Model 18
4.5 JDBC in the Java EE Platform 20
6. Compliance 29
6.1 Definitions 29
6.2 Guidelines and Requirements 30
6.3 JDBC 4.0 API Compliance 31
6.4 Java EE JDBC Compliance 35
7. Database Metadata 37
7.1 Creating a DatabaseMetadata Object 38
7.2 Retrieving General Information 38
7.3 Determining Feature Support 39
7.4 Data Source Limits 39
7.5 SQL Objects and Their Attributes 40
7.6 Transaction Support 40
7.7 New Methods 41
7.8 Modified Methods 41
8. Exceptions 43
8.1 SQLException 43
8.1.1 Support for the Java SE Chained Execeptions 44
8.1.2 Navigating SQLExceptions 44
8.1.2.1 Using a For-Each Loop with SQLExceptions 45
8.2 SQLWarning 46
9. Connections 51
9.1 Types of Drivers 52
9.2 The Driver Interface 52
9.2.1 Loading a driver that implements java.sql.Driver 53
9.3 The DriverManager Class 53
9.3.1 The SQLPermission Class 55
9.4 The DataSource Interface 55
9.4.1 DataSource Properties 56
9.4.2 The JNDI API and Application Portability 57
9.4.3 Getting a Connection with a DataSource Object 58
9.4.4 Closing Connection Objects 59
10. Transactions 61
10.1 Transaction Boundaries and Auto-commit 61
10.1.1 Disabling Auto-commit Mode 62
10.2 Transaction Isolation Levels 62
10.2.1 Using the setTransactionIsolation Method 63
10.2.2 Performance Considerations 64
10.3 Savepoints 64
10.3.1 Setting and Rolling Back to a Savepoint 65
Contents xi
10.3.2 Releasing a Savepoint 65
13. Statements 93
13.1 The Statement Interface 93
13.1.1 Creating Statements 93
13.1.1.1 Setting ResultSet Characteristics 94
13.1.2 Executing Statement Objects 94
Contents xiii
13.3.3.2 Returning an Update Count 110
13.3.3.3 Returning Unknown or Multiple Results 110
13.4 Escape Syntax 111
13.4.1 Scalar Functions 112
13.4.2 Date and Time Literals 112
13.4.3 Outer Joins 113
13.4.4 Stored Procedures and Functions 114
13.4.5 LIKE Escape Characters 114
13.5 Performance Hints 115
13.6 Retrieving Auto Generated Values 115
Contents xv
16.5 Array Objects 149
16.5.1 Array Implementations 149
16.5.2 Creating Array Objects 150
16.5.3 Retrieving Array Objects 150
16.5.4 Storing Array Objects 151
16.5.5 Updating Array Objects 151
16.5.6 Releasing Array Resources 152
16.6 Ref Objects 152
16.6.1 Retrieving REF Values 152
16.6.2 Retrieving the Referenced Value 153
16.6.3 Storing Ref Objects 153
16.6.4 Storing the Referenced Value 153
16.6.5 Metadata 154
16.7 Distinct Types 154
16.7.1 Retrieving Distinct Types 154
16.7.2 Storing Distinct Types 155
16.7.3 Metadata 155
16.8 Structured Types 156
16.8.1 Creating Structured Objects 156
16.8.2 Retrieving Structured Types 157
16.8.3 Storing Structured Types 157
16.8.4 Metadata 157
16.9 Datalinks 158
16.9.1 Retrieving References to External Data 158
16.9.2 Storing References to External Data 158
16.9.3 Metadata 159
16.10 RowId Objects 159
16.10.1 Lifetime of RowId Validity 159
Contents xvii
B. Data Type Conversion Tables 191
This document introduces a range of new features for the JDBC API and is combined
with various specification improvements that focus on features introduced in or
before the JDBC 3.0 API. Where possible, any adjustment to the JDBC 3.0 API is
marked for easy identification - look for the JDBC 4.0 API demarcation for specific
features introduced in this revised and updated specification.
Readers can also download the API specification (JavadocTM API and comments) for
a complete and precise definition of JDBC classes and interfaces. This documentation
is available from the download page at
http://java.sun.com/products/jdbc
1
Typographic Conventions
Typeface Meaning Examples
AaBbCc123 Book titles, new words or Read Chapter 6 in the User’s Guide.
terms, words to be emphasized These are called class options.
You must be superuser to do this.
Submitting Feedback
Please send any comments and questions concerning this specification to:
[email protected]
Introduction
The JDBC API is based on the X/Open SQL CLI, which is also the basis for ODBC.
JDBC provides a natural and easy-to-use mapping from the Java programming
language to the abstractions and concepts defined in the X/Open CLI and SQL
standards.
Since its introduction in January 1997, the JDBC API has become widely accepted
and implemented. The flexibility of the API allows for a broad range of
implementations.
1.2 Platforms
The JDBC API is part of the Java platform, which includes the JavaTM Standard
Edition (JavaTM SE ) and the JavaTM Enterprise Edition (JavaTM EE). The JDBC 4.0 API
is divided into two packages: java.sql and javax.sql. Both packages are
included in the Java SE and Java EE platforms.
3
1.3 Target Audience
This specification is targeted primarily towards the vendors of these types of
products:
■ drivers that implement the JDBC API
■ application servers providing middle-tier services above the driver layer
■ tools that use the JDBC API to provide services such as application generation
1.4 Acknowledgements
The JDBC 4.0 specification work is being conducted as part of JSR-221 under the Java
Community Process. This specification is the result of the collaborative efforts of the
JDBC 4.0 Expert Group whose individual members contributed countess hours to
ensure the success of this specification. We would like to thank the following
members for their contributions:
Volker Berlin
Paul Bonfanti
Bruce Synder
Thanks also go to the many people behind the scenes who have helped and
supported this effort: Stephen Brodsky, Carla Carlson, Ian Evans , William Franklin,
Kyle Grucci, Amit Handa, Rick Hillegas, Eric Jendrock, Shreyas Kaushik, Fei Luo,
Ashish Mahahan, Magne Mahre, Josh Meckler, Ryan O’Connell, Kevin Osborn,
Craig Russell, David Van Couvering and Dag Wanvik.
Last, but not least, we would like to thank the previous JDBC specification leads for
their contributions to the success of JDBC: Graham Hamilton, Rick Cattell, Seth
White, Jon Ellis, Linda Ho and Jonathan Bruce.
Chapter 1 Introduction 5
6 JDBC 4.0 Specification • November 2006
CHAPTER 2
Goals
2.1 History
The JDBC API is a mature technology, having first been specified in January 1997. In
its initial release, the JDBC API focused on providing a basic call-level interface to
SQL databases. The JDBC 2.1 specification and the 2.0 Optional Package
specification then broadened the scope of the API to include support for more
advanced applications and for the features required by application servers to
manage use of the JDBC API on behalf of their applications.
The JDBC 3.0 specification operated with the stated goal to round out the API by
filling in smaller areas of missing functionality. With JDBC 4.0, our goals are two
fold: Improve the Ease-of-Development experience for all developers working with
SQL in the Java platform. Secondly, provide a range of enterprise level features to
expose JDBC to a richer set of tools and APIs to manage JDBC resources.
7
2. Be consistent with SQL:2003
The JDBC API provides programmatic access from applications written in the
Java programming language to standard SQL. JDBC 3.0 sought to ensure it’s
support for a subset of the SQL99 features that were likely to be widely
supported by the industry. Similarly for JDBC 4.0, support for SQL:2003 is
focused on the major components of this specification that we anticipate will
be supported for the foreseeable future.
6. Keep it simple
The JDBC API is intended to be a simple-to-use, straight forward interface
upon which more complex entities can be built. This goal is achieved by
defining many compact, single-purpose methods instead of a smaller number
of complex, multipurpose ones with control flag parameters.
Chapter 2 Goals 9
10 JDBC 4.0 Specification • November 2006
CHAPTER 3
11
■ SQL/XML and XML Support
SQL:2003 introduces the notion of how to express XML data in a SQL data
store. Additional APIs have been added to allow applications access to this
data.
■ Wrapper Pattern
Added the ability to unwrap implementation of JDBC classes to make use of
non-standard JDBC methods provided by vendor implementations.
■ SQLException Enhancements
Support for Java SE chained Exceptions has been added. SQLExceptions now
support the Iterable interface allowing for their use in a for-each loop. Three
categories of SQLExceptions have been added: SQLTransientException,
SQLNonTransientExeption and SQLRecoverableExeption. Each
category provides new SQLException subclasses that map to common
SQLState class values. Added the Exception SQLClientInfoException.
■ Connection Management
Additional enhancements to Connection and Statement interfaces to
permit improved connection state tracking, and greater flexibility when
managing Statement objects in pool environments.
■ New Scalar Funtions
Added the functions CHAR_LENGTH, CHARACTER_LENGTH, CURRENT_DATE,
CURRENT_TIME, CURRENT_TIMESTAMP, EXTRACT, OCTET_LENGTH,
POSITION.
■ JDBC API changes
The following changes were made to existing JDBC interfaces.
■ Array
Added the method free.
■ Connection
Added the methods createArrayOf,createBlob, createClob,
createNClob, createSQLXML,createStruct, isValid,
getClientInfo, setClientInfo.
■ CallableStatement
Added the methods getRowId, setRowId, getNClob, getNString,
getCharacterStream, getNCharacterStream, setNString,
setNCharacterStream, setNClob, getSQLXML, setSQLXML.
Overloaded the setAsciiStream,setBinaryStream,
setCharacterStream,setBlob and setClob methods.
■ DatabaseMetaData
See Chapter 5 “Classes and Interfaces” for a list of the classes and interfaces affected
by these changes.
Overview
The JDBC API provides a way for Java programs to access one or more sources of
data. In the majority of cases, the data source is a relational DBMS, and its data is
accessed using SQL. However, it is also possible for JDBC technology-enabled
drivers to be implemented on top of other data sources, including legacy file systems
and object-oriented systems. A primary motivation for the JDBC API is to provide a
standard API for applications to access a wide variety of data sources.
This chapter introduces some of the key concepts of the JDBC API. In addition, it
describes two common environments for JDBC applications, with a discussion of
how different functional roles are implemented in each one. The two-tier and three-
tier models are logical configurations that can be implemented on a variety of
physical configurations.
In a typical scenario, a JDBC application will connect to a target data source using
one of two mechanisms:
■ DriverManager — this fully implemented class was introduced in the original
JDBC 1.0 API. When an application first attempts to connect to a data source by
specifying a URL, DriverManager will automatically load any JDBC 4.0 drivers
found within the CLASSPATH (any drivers that are pre-JDBC 4.0 must be
explicitly loaded by the application).
■ DataSource — this interface was introduced in the JDBC 2.0 Optional Package
API. It is preferred over DriverManager because it allows details about the
underlying data source to be transparent to the application. A DataSource
object’s properties are set so that it represents a particular data source. When its
getConnection method is invoked, the DataSource instance will return a
15
connection to that data source. An application can be directed to a different data
source by simply changing the DataSource object’s properties; no change in
application code is needed. Likewise, a DataSource implementation can be
changed without changing the application code that uses it.
The JDBC API also defines two important extensions of the DataSource interface to
support enterprise applications. These extensions are the following two interfaces:
■ ConnectionPoolDataSource — supports caching and reusing of physical
connections, which improves application performance and scalability
■ XADataSource — provides connections that can participate in a distributed
transaction
The JDBC API extends the ResultSet interface with the RowSet interface, thereby
providing a container for tabular data that is much more versatile than a standard
result set. A RowSet object is a JavaBeansTM component, and it may operate without
being connected to its data source. For example, a RowSet implementation can be
serializable and therefore sent across a network, which is particularly useful for
small-footprint clients that want to operate on tabular data without incurring the
overhead of a JDBC driver and data source connection. Another feature of a RowSet
implementation is that it can include a custom reader for accessing any data in
tabular format, not just data in a relational database. Further, a RowSet object can
Application
JDBC Driver
data source
The client layer includes the application(s) and one or more JDBC drivers, with the
application handling these areas of responsibility:
Chapter 4 Overview 17
■ presentation logic
■ business logic
■ transaction management for multiple-statement transactions or distributed
transactions
■ resource management
In this model, the application interacts directly with the JDBC driver(s), including
establishing and managing the physical connection(s) and dealing with the details of
specific underlying data source implementations. The application may use its
knowledge of a specific implementation to take advantage of nonstandard features
or do performance tuning.
Application Application
JDBC
Driver data source
transaction
manager
1. Client tier — a thin layer implementing presentation logic for human interaction.
Java programs, web browsers and PDAs are typical client-tier implementations.
The client interacts with the middle-tier application and does not need to include
any knowledge of infrastructure or underlying data source functions.
Chapter 4 Overview 19
■ An application server to provide supporting infrastructure for a wide range of
applications. This can include management and pooling of physical
connections, transaction management, and the masking of differences between
different JDBC drivers. This last point makes it easier to write portable
applications. The application server role can be implemented by a Java EE
server. Application servers implement the higher-level abstractions used by
applications and interact directly with JDBC drivers.
■ JDBC driver(s) to provide connectivity to the underlying data sources. Each
driver implements the standard JDBC API on top of whatever features are
supported by its underlying data source. The driver layer may mask
differences between standard SQL:2003 syntax and the native dialect
supported by the data source. If the data source is not a relational DBMS, the
driver implements the relational layer used by the application server.
3. Underlying data source — the tier where the data resides. It can include relational
DBMSs, legacy file systems, object-oriented DBMSs, data warehouses,
spreadsheets, or other means of packaging and presenting data. The only
requirement is a corresponding driver that supports the JDBC API.
java.sql.Array
java.sql.BatchUpdateException
java.sql.Blob
java.sql.CallableStatement
java.sql.Clob
java.sql.ClientinfoStatus
java.sql.Connection
java.sql.DataTruncation
java.sql.DatabaseMetaData
java.sql.Date
java.sql.Driver
java.sql.DriverManager
java.sql.DriverPropertyInfo
java.sql.NClob
java.sql.ParameterMetaData
java.sql.PreparedStatement
21
java.sql.Ref
java.sql.ResultSet
java.sql.ResultSetMetaData
java.sql.RowId
java.sql.RowIdLifeTime
java.sql.Savepoint
java.sql.SQLClientInfoException
java.sql.SQLData
java.sql.SQLDataException
java.sql.SQLException
java.sql.SQLFeatureNotSupportedException
java.sql.SQLInput
java.sql.SQLIntegrityConstraintViolationException
java.sql.SQLInvalidAuthorizationSpecException
java.sql.SQLNonTransientConnectionException
java.sql.SQLNonTransientException
java.sql.SQLOutput
java.sql.SQLPermission
java.sql.SQLSyntaxErrorException
java.sql.SQLTimeoutException
java.sql.SQLTransactionRollbackException
java.sql.SQLTransientConnectionException
java.sql.SQLTransientException
java.sql.SQLXML
java.sql.SQLWarning
java.sql.Statement
java.sql.Struct
java.sql.Time
java.sql.Timestamp
java.sql.Types
java.sql.Wrapper
The following classes and interfaces are either new or updated in the JDBC 4.0 API.
New classes and interfaces are highlighted in bold.
java.sql.Blob
java.sql.CallableStatement
Connection
prepareStatement
pr
t
en
ep
em
are
tat
Ca
S
ll
ate
cr e
subclasses subclasses
Statement PreparedStatement CallableStatement
Input/Output of
Input to CallableStatement
PreparedStatement
exe
te
ltS
c
Data types
ute
esu
Qu
etR
ery
/g
reR ery
lts
executeQuery getXXX
u
esu
get cuteQ
Mo
exe
ResultSet
FIGURE 5-1 Relationships between major classes and interface in the java.sql package
javax.sql.CommonDataSource
javax.sql.ConnectionEvent
javax.sql.ConnectionEventListener
javax.sql.ConnectionPoolDataSource
javax.sql.DataSource
javax.sql.PooledConnection
javax.sql.RowSet
javax.sql.RowSetEvent
javax.sql.RowSetInternal
javax.sql.RowSetListener
javax.sql.RowSetMetaData
javax.sql.RowSetReader
javax.sql.RowSetWriter
javax.sql.StatementEvent
javax.sql.StatementEventListener
javax.sql.XAConnection
javax.sql.XADataSource
Note – The classes and interfaces in the javax.sql package were first made
available as the JDBC 2.0 Optional Package. This optional package was previously
separate from the java.sql package, which was part of J2SE 1.2. Both packages
(java.sql and javax.sql) are now part of Java SE as of J2SE 1.4.
FIGURE 5-2, FIGURE 5-3, FIGURE 5-4, and FIGURE 5-5 show the relationships between key
classes and interfaces in these areas of functionality: DataSource objects,
connection pooling, distributed transactions, and rowsets.
getConnection
DataSource Connection
java.sql javax.sql
ConnectionPoolDataSource
getConnection
getConnection
Connection PooledConnection
ConnectionEvent
ConnectionEventListener
PooledConnection XADataSource
subclasses getXAConnection
ConnectionEvent
ConnectionEventListener
ResultSet
subclasses
RowSet RowSetEvent RowSetEventListener
RowSetInternal
ResultSetMetaData
retrieves
metadata reads data writes data
subclasses
RowSetMetaData RowSetReader RowSetWriter
Compliance
This chapter identifies the features that a JDBC driver implementation is required to
support to claim compliance. Any features not identified are considered optional for
compliance.
6.1 Definitions
To avoid ambiguity, we will use these terms in our discussion of compliance:
■ JDBC driver implementation — a JDBC technology-enabled driver and its
underlying data source. The driver may provide support for features that are not
implemented by the underlying data source. It may also provide the mapping
between standard syntax/semantics and the native API implemented by the data
source.
■ Relevant specifications — this document, the API specification, and the relevant
SQL specification. This is also the order of precedence if a feature is described in
more than one of these documents. For the JDBC 4.0 API, it is SQL92 plus the
relevant sections of SQL:2003 and X/Open SQL CLI.
■ Supported feature — a feature for which the JDBC API implementation supports
standard syntax and semantics for that feature as defined in the relevant
specifications.
■ Partially Supported Feature—A feature for which some methods are
implemented via standard syntax and semantics and some required methods
throw SQLFeatureNotSupportedException to indicate that it is not
supported.
■ Extension — a feature that is not covered by any of the relevant specifications or
a non-standard implementation of a feature that is covered.
■ Fully implemented — a term applied to an interface that has all of its methods
implemented to support the semantics defined in the relevant specifications.
None of the methods may throw an exception because they are not implemented.
29
■ Must implement — an interface that must be implemented although some
methods on the interface are considered optional. Methods that are not
implemented must throw an SQLFeatureNotSupportedException to indicate
that the corresponding feature is not supported.
Chapter 6 Compliance 31
■ prepareStatement(String sql, int[] columnIndexes)
■ prepareStatement(String sql, String[] columnNames)
■ setSavePoint
■ rollback(java.sql.SavePoint savepoint)
■ releaseSavePoint
■ It must implement the Statement interface with the exception of the following
optional methods:
■ cancel
■ execute(String sql, Statement.RETURN_GENERATED_KEYS)
■ execute(String sql, int[] columnIndexes)
■ execute(String sql, String[] columnNames)
■ executeUpdate(String sql, Statement.RETURN_GENERATED_KEYS)
■ executeUpdate(String sql, int[] columnIndexes)
■ executeUpdate(String sql, String[] columnNames)
■ getGeneratedKeys
■ getMoreResults(Statement.KEEP_CURRENT_RESULT) unless
DatabasemetaData.supportsMultipleOpenResults() returns true.
■ getMoreResults(Statement.CLOSE_ALL_RESULTS) unless
DatabasemetaData.supportsMultipleOpenResults() returns true.
■ setCursorName
■ It must implement the PreparedStatement interface with the exception of the
following optional methods:
■ getMetaData
■ setArray, setBlob, setClob, setNClob, setNCharacterStream,
setNString, setRef, setRowId, setSQLXML and setURL unless the driver
supports the associated data type
■ setNull(int parameterIndex,int sqlType, String typeName)
unless the driver supports the associated data type
■ setUnicodeStream
■ setAsciiStream, setBinaryStream, setCharacterStream,
setNCharacterStream which do not take a length parameter
■ It must implement the CallableStatement interface if the method
DatabaseMetaData.supportsStoredProcedures() returns true with the
exception of the following optional methods:
■ All setXXX, getXXX and registerOutputParameter methods which
support named parameters
Chapter 6 Compliance 33
■ last
■ moveToCurrentRow
■ moveToInsertRow
■ previous
■ refreshRow
■ relative
■ rowDeleted
■ rowInserted
■ rowUpdated
■ updateRow
■ if a JDBC driver supports a ResultSet concurrency of CONCUR_UPDATABLE,
the following ResultSet interface methods must be implemented:
■ All updateXXX methods except for updateArray, updateBlob,
updateClob, updateNClob, updateNCharacterstream, updateNString,
updateRef, updateRowId, updateSQLXML and updateURL unless the
driver supports the associated data type and the updateBlob, updateClob,
updateNClob, updateAsciiStream, updateBinaryStream,
updateCharacterStream and updateNCharacterstream methods
which take a length parameter
■ cancelRowUpdates
■ deleteRow
■ rowDeleted
■ rowUpdated
■ updateRow
■ if a JDBC driver supports a ResultSet type of TYPE_SCROLL_SENSITIVE or
TYPE_SCROLL_INSENSITIVE, the following ResultSet interface methods
must be implemented:
■ absolute
■ afterLast
■ beforeFirst
■ first
■ isAfterLast
■ isBeforeFirst
■ isFirst
■ isLast
■ last
■ previous
Support for calling stored procedures using the execute method on the
Statement, PreparedStatement, and CallableStatement interfaces only
requires that a SQL statement being executed returns either an update count or a
single ResultSet object. This is due to the fact that some databases don’t support
returning more than a single ResultSet from a stored procedure. .
Chapter 6 Compliance 35
■ SUBSTRING
■ UCASE
Database Metadata
The DatabaseMetaData interface also contains over 40 fields, which are constants
used as return values for various DatabaseMetaData methods.
37
7.1 Creating a DatabaseMetadata Object
A DatabaseMetaData object is created with the Connection method
getMetaData. Once created, it can be used to dynamically discover information
about the underlying data source. CODE EXAMPLE 7-1 creates a DatabaseMetadata
object and uses it to determine the maximum number of characters allowed for a
table name.
Methods in this group return the limit as an int. A return value of zero means that
there is no limit or the limit is unknown.
The ResultSet objects that are returned from a DatabaseMetaData method have
a sensitivity of TYPE_FORWARD_ONLY and a concurrency of CONCUR_READ_ONLY.
ResultSet.getHoldability can be called to determine the holdability of the
returned ResultSet object as the default holdability is implementation defined.
A complete definition of these methods may be found in the JDBC 4.0 API
specification (javadoc).
The JDBC 4.0 API specification includes updated definitions of these methods.
Exceptions
The SQLException class and its subtypes provide information about errors and
warnings that occur while a data source is being accessed.
8.1 SQLException
An instance of SQLException is thrown when an error occurs during an interaction
with a data source. The exception contains the following information:
■ a textual description of the error. The String containing the description can be
retrieved by calling the method SQLException.getMessage.
■ a SQLState. The String containing the SQLState can be retrieved by calling the
method SQLException.getSQLState.
The value of the SQLState string will depend on the underlying data source
setting the value. Both X/Open and SQL:2003 define SQLState values and the
conditions in which they should be set. Although the sets of values overlap,
the values defined by SQL:2003 are not a superset of X/Open.
The DatabaseMetaData method getSQLStateType allows an application to
determine if the SQLStates being returned by a data source are X/Open or
SQL:2003.
■ an error code. This is an integer value identifying the error that caused the
SQLException to be thrown. Its value and meaning are implementation specific
and may be the actual error code returned by the underlying data source. The
error code can be retrieved using the SQLException.getErrorCode method.
■ a cause. This is another Throwable which caused this SQLException to occur.
43
■ a reference to any "chained" exceptions. If more than one error occurs the
exceptions are referenced via this chain. All chained exception can be recursively
retrieved by calling the SQLException.getNextException method on the
exception that was thrown. If no more exceptions are chained, the
getNextException method returns null.
There are multiple subclasses that extend SQLException. These subclasses are
described in the following sections.
Please refer to the JDBC 4.0 API specification for additional information.
The following code demonstrates how to use the For-Each Loop with
SQLExceptions.
catch(SQLException ex) {
for(Throwable e : ex ) {
System.out.println("Error encountered: " + e);
}
}
CODE EXAMPLE 8-2 Using SQLExceptions with a For-Each Loop
Chapter 8 Exceptions 45
8.2 SQLWarning
SQLWarning is a subclass of SQLException. The methods in the following
interfaces will generate an SQLWarning object if they encounter a database access
warning:
■ Connection
■ DataSet
■ Statement
■ ResultSet
When a method generates an SQLWarning object, the caller is not informed that a
data access warning has occurred. The method getWarnings must be called on the
appropriate object to retrieve the SQLWarning object. However, the
DataTruncation sub-class of SQLWarning may be thrown in some circumstances,
see Section 8.3 “DataTruncation” on page 8-46 for more details.
If multiple data access warnings occur, they are chained to the first one and can be
retrieved by recursively calling the SQLWarning.getNextWarning method. If
there are no more warnings in the chain, getNextWarning returns null.
Subsequent SQLWarning objects continue to be added to the chain until the next
statement is executed or, in the case of a ResultSet object, when the cursor is re-
positioned, at which point all SQLWarning objects in the chain are removed.
8.3 DataTruncation
The DataTruncation class, a sub-class of SQLWarning, provides information
when data is truncated. When data truncation occurs on a write to the data source, a
DataTruncation object is thrown. The data value that has been truncated may
have been written to the data source even if a warning has been generated. When
data truncation occurs on a read from the data source, a SQLWarning is reported.
If a limit has been set using setMaxFieldSize and there is an attempt to read data
that exceeds the limit, any truncation that occurs as a result of exceeding the set limit
will not be reported.
8.4 BatchUpdateException
A BatchUpdateException object provides information about errors that occur
while a batch of statements is being executed. This exception’s behavior is described
in Chapter 14 “Batch Updates”.
Chapter 8 Exceptions 47
8.5 Categorized SQLExceptions
JDBC 4.0 introduces new SQLException subclasses which provide a standard
mapping to common SQLStates class values and to common error states which are
not associated with a specific SQLState class value. The SQLState class values are
defined in the SQL:2003 specification. A JDBC driver may also throw a Categorized
SQLException for JDBC driver detected errors. The new SQLException subclasses
will provide a means for JDBC programmers to write more portable error-handling
code.
0A SQLFeatureNotSupportedException
08 SQLNonTransientConnectionException
22 SQLDataException
23 SQLIntegrityConstraintViolationException
28 SQLInvalidAuthorizationException
42 SQLSyntaxErrorException
08 SQLTransientConnectionException
40 SQLTransactionRollbackException
N/A SQLTimeoutException
8.5.3 SQLRecoverableException
A SQLRecoverableException would be thrown in situations where the failed
operation might succeed if the application performs some recovery steps and retries
the entire transaction or in the case of a distributed transaction, the transaction
branch. At a minimum, recovery includes closing the current connection and getting
a new one. After a SQLRecoverableException the application must assume that
the connection is no longer valid.
Chapter 8 Exceptions 49
8.6 SQLClientinfoException
A SQLClientInfoException is thrown by the Connection.setClientInfo
method when a failure occurs setting one or more of the specified client properties.
The SQLClientInfoException contains information indicating which client info
properties were not set.
Connections
From the JDBC driver perspective, a Connection object represents a client session.
It has associated state information such as user ID, a set of SQL statements and
result sets being used in that session, and what transaction semantics are in effect.
OR
■ a DataSource implementation
This chapter describes the various types of JDBC drivers and the use of the Driver
interface, the DriverManager class, and the basic DataSource interface.
DataSource implementations that support connection pooling and distributed
transactions are discussed in Chapter 11 “Connection Pooling” and Chapter 12
“Distributed Transactions”.
51
9.1 Types of Drivers
There are many possible implementations of JDBC drivers. These implementations
are categorized as follows:
■ Type 1 — drivers that implement the JDBC API as a mapping to another data
access API, such as ODBC. Drivers of this type are generally dependent on a
native library, which limits their portability. The JDBC-ODBC Bridge driver is an
example of a Type 1 driver.
■ Type 2 — drivers that are written partly in the Java programming language and
partly in native code. These drivers use a native client library specific to the data
source to which they connect. Again, because of the native code, their portability
is limited.
■ Type 3 — drivers that use a pure Java client and communicate with a middleware
server using a database-independent protocol. The middleware server then
communicates the client’s requests to the data source.
■ Type 4 — drivers that are pure Java often using a network protocol or File I/O to
communicate with a specific data source. The client connects directly to the data
source.
The DriverManager class invokes Driver methods when it wishes to interact with
a registered driver. The Driver interface also includes the method acceptsURL.
The DriverManager can use this method to determine which of its registered
drivers it should use for a given URL.
my.sql.Driver
CODE EXAMPLE 9-3 META-INF/services/java.sql.Driver file contents
Chapter 9 Connections 53
Key DriverManager methods include:
■ registerDriver — this method adds a driver to the set of available drivers and
is invoked implicitly when the driver is loaded. The registerDriver method is
typically called by the static initializer provided by each driver.
■ getConnection — the method the JDBC client invokes to establish a connection.
The invocation includes a JDBC URL, which the DriverManager passes to each
driver in its list until it finds one whose Driver.connect method recognizes the
URL. That driver returns a Connection object to the DriverManager, which in
turn passes it to the application.
■ jdbc:<subprotocol>:<subname>
where subprotocol defines the kind of database connectivity mechanism that may be
supported by one or more drivers. The contents and syntax of the subname will
depend on the subprotocol.
Note – A JDBC URL is not required to fully adhere to the URI syntax as defined in
RFC 3986, Uniform Resource Identifier (URI): Generic Syntax.
CODE EXAMPLE 9-4 illustrates how a JDBC client obtains a connection from the
DriverManager.
Chapter 9 Connections 55
The DataSource interface can be implemented so that it transparently provides the
following:
■ Increased performance and scalability through connection pooling
■ Support for distributed transactions through the XADataSource interface
The next three sections discuss (1) basic DataSource properties, (2) how logical
naming using the JNDI API improves an applications portability and makes it easier
to maintain, and (3) how to obtain a connection.
DataSource implementations must provide “getter” and “setter” methods for each
property they support. These properties typically are initialized when the
DataSource object is deployed, as in CODE EXAMPLE 9-5, in which a
VendorDataSource object implements the DataSource interface.
Using the JNDI API, applications can access a DataSource object by specifying its
logical name. A naming service using the JNDI API maps this logical name to a
corresponding data source. This scheme greatly enhances portability because any of
the DataSource properties, such as portNumber or serverName, can be changed
without impacting the JDBC client code. In fact, the application can be re-directed to
a different underlying data source in a completely transparent fashion. This is
particularly useful in the three-tier environment, where an application server hides
the details of accessing different data sources.
CODE EXAMPLE 9-6 illustrates the use of a JNDI-based naming service to deploy a
new VendorDataSource object.
Chapter 9 Connections 57
// Create a VendorDataSource object and set some properties
VendorDataSource vds = new VendorDataSource();
vds.setServerName("my_database_server");
vds.setDatabaseName("my_database");
vds.setDescription("data source for inventory and personnel");
Note – Java EE components use a special convention for naming their data sources
— see Chapter 5 "Naming" in the Java EE platform specification for more details.
Once a Connection has been closed, any attempt to access any of its methods with
the exception of the close, isClosed or isValid methods will result in a
SQLException being thrown.
Chapter 9 Connections 59
60 JDBC 4.0 Specification • November 2006
CHAPTER 10
Transactions
Transactions are used to provide data integrity, correct application semantics, and a
consistent view of data during concurrent access. All JDBC compliant drivers are
required to provide transaction support. Transaction management in the JDBC API
mirrors the SQL:2003 specification and includes these concepts:
■ Auto-commit mode
■ Transaction isolation levels
■ Savepoints
61
■ For Data Manipulation Language (DML) statements such as Insert, Update,
Delete, and DDL statements, the statement is complete as soon as it has finished
executing.
■ For Select statements, the statement is complete when the associated result set
is closed.
■ For CallableStatement objects or for statements that return multiple results,
the statement is complete when all of the associated result sets have been closed,
and all update counts and output parameters have been retrieved.
The default is for auto-commit mode to be enabled when the Connection object is
created. If the value of auto-commit is changed in the middle of a transaction, the
current transaction is committed. If setAutoCommit is called and the value for
auto-commit is not changed from its current value, it is treated as a no-op.
c. Transaction A reads the same row a second time and gets different results
■ phantom reads occur when:
Chapter 10 Transactions 63
The Connection method setTransactionIsolation is provided to allow JDBC
clients to change the transaction isolation level for a given Connection object. The
new isolation level remains in effect for the remainder of the session or until the next
invocation of the setTransactionIsolation method.
It is possible for a given JDBC driver to not support all four transaction isolation
levels (not counting TRANSACTION_NONE). If a driver does not support the isolation
level specified in an invocation of setTransactionIsolation, it is allowed to
substitute a higher, more restrictive transaction isolation level. If a driver is unable
to substitute a higher transaction level, it throws an SQLException. The
DatabaseMetaData method supportsTransactionIsolationLevel may be
used to determine whether or not the driver supports a given level.
10.3 Savepoints
Savepoints provide finer-grained control of transactions by marking intermediate
points within a transaction. Once a savepoint has been set, the transaction can be
rolled back to that savepoint without affecting preceding work.
CODE EXAMPLE 10-2 inserts a row into a table, sets the savepoint svpt1, and then
inserts a second row. When the transaction is later rolled back to svpt1, the second
insertion is undone, but the first insertion remains intact. In other words, when the
transaction is committed, only the row containing ’FIRST’ will be added to TAB1.
conn.createStatement();
int rows = stmt.executeUpdate("INSERT INTO TAB1 (COL1) VALUES " +
"(’FIRST’)");
// set savepoint
Savepoint svpt1 = conn.setSavepoint("SAVEPOINT_1");
rows = stmt.executeUpdate("INSERT INTO TAB1 (COL1) " +
"VALUES (’SECOND’)");
...
conn.rollback(svpt1);
...
conn.commit();
CODE EXAMPLE 10-2 Rolling back a transaction to a savepoint
Any savepoints that have been created in a transaction are automatically released
and become invalid when the transaction is committed or when the entire
transaction is rolled back.
Chapter 10 Transactions 65
66 JDBC 4.0 Specification • November 2006
CHAPTER 11
Connection Pooling
67
JDBC
Application
logical
DataSource API Connection
object
Application Server
Cache of
PooledConnection objects
physical
ConnectionPoolDataSource API PooledConnection
object
JDBC Driver
}
CODE EXAMPLE 11-2 The PooledConnection interface
When an error occurs, the JDBC driver notifies the listener by calling its
connectionErrorOccurred method and then throws an SQLException object to
the application to notify it of the same error. In the event of a fatal error, the bad
PooledConnection object is not returned to the pool. Instead, the connection pool
manager calls the PooledConnection.close method on the PooledConnection
object to close the physical connection.
A basic DataSource implementation, that is, one that does not implement
connection pooling, is typically provided by a JDBC driver vendor. In a basic
DataSource implementation, the following are true:
Connection Pool
Pool of
PreparedStatement
Objects
JDBC
PooledConnection
Application
data source
JDBC
PooledConnection
Application
Pool of
PreparedStatement
Objects
In FIGURE 11-2, the connection pool and statement pool are implemented by the
application server. However, this functionality could also be implemented by the
driver or underlying data source. This discussion of statement pooling is meant to
allow for any of these implementations.
An application may find out whether a data source supports statement pooling by
calling the DatabaseMetaData method supportsStatementPooling. If the
return value is true, the application can then choose to use PreparedStatement
objects knowing that they are being pooled.
When an error occurs that makes a PreparedStatement object invalid, the JDBC
driver notifies the listener by calling its statementErrorOccurred method and
then throws an SQLException object to the application to notify it of the same error.
maxStatements int The total number of statements that the pool should
keep open. 0 (zero) indicates that caching of
statements is disabled.
propertyCycle int The interval, in seconds, that the pool should wait
before enforcing the current policy defined by the
values of the above connection pool properties
Distributed Transactions
Up to this point, the discussion of transactions has focused on the local case—
transactions involving a single data source. This chapter introduces the distributed
case where a single transaction involves multiple connections to one or more
underlying data sources.
Transaction management in the JDBC API is designed to fit with the Java
Transaction APITM (JTATM) . The examples presented here are high-level; the JTA
specification should be consulted for a more substantial discussion.
12.1 Infrastructure
Distributed transactions require an infrastructure that provides these roles:
■ Transaction manager — controls transaction boundaries and manages the two-
phase commit protocol. This typically will be an implementation of JTA.
■ JDBC drivers that implement the XADataSource, XAConnection, and
XAResource interfaces. These are described in the next section.
■ An application-visible implementation of DataSource to “sit on top of” each
XADataSource object and interact with the transaction manager. The
DataSource implementation is typically provided by an application server.
81
■ Resource manager(s) to manage the underlying data. In the context of the JDBC
API, a resource manager is a DBMS server. The term “resource manager” is
borrowed from JTA to emphasize the point that distributed transactions using the
JDBC API follow the architecture specified in that document.
1. A client tier
2. A middle tier that includes applications, an EJB server working with an external
transaction manager, and a set of JDBC drivers
transaction
manager
Connection Connection XAResource
pool for A pool for B B1
physical physical
XAConnection XAConnection
A1 B1
XADataSource XADataSource
API API
JDBC JDBC
Driver A Driver B
resource resource
manager manager
12.3 XAResource
The XAResource interface is defined in the JTA specification and is the mapping in
the Java programming language of the X/Open Group XA interface. An
XAResource object is produced by calling the XAConnection.getXAResource
method and is used to associate an XAConnection object with a distributed
transaction. A given XAConnection object may be associated with at most one
See the JTA specification for a complete description of the XAResouce interface.
If a connection has auto-commit mode already enabled at the time it joins a global
transaction, the attribute will be ignored. The auto-commit behavior will resume
when the connection returns to local transaction mode.
1. The application server gets XAResource objects from two different connections:
// XAConA connects to resource manager A
javax.transaction.xa.XAResource resourceA = XAConA.getXAResource();
// XAConB connects to resource manager B
javax.transaction.xa.XAResource resourceB = XAConB.getXAResource();
CODE EXAMPLE 12-6 Getting the XAResource object from an XAConnection object
2. The application server passes the XAResource objects to the transaction manager.
The transaction manager does not access the associated XAConnection objects
directly.
3. The transaction manager uses the XAResource objects to assign a piece of the
transaction to each of the associated resource managers. The transaction is
identified by xid, which represents the identifier generated by the transaction
manager when the transaction is created.
4. The transaction manager initiates the two-phase commit protocol by asking each
participant to vote:
resourceA.prepare(xid);
resourceB.prepare(xid);
CODE EXAMPLE 12-8 Initiating two-phase commit
A participating resource manager can vote to roll back the transaction by throwing a
javax.transaction.xa.XAException.
5. If both participants vote to commit, the transaction manager tells each one to
commit its piece of the distributed transaction (the second parameter tells the
resource manager not to use a one phase commit protocol on behalf of the xid):
resourceA.commit(xid, false);
resourceB.commit(xid, false);
CODE EXAMPLE 12-9 Committing the distributed transaction
6. If either resource manager votes to roll back, the transaction manager tells each
one to roll back its piece of the transaction:
resourceA.rollback(xid);
resourceB.rollback(xid);
CODE EXAMPLE 12-10 Rolling back the distributed transaction
Note – Steps 1-6 also apply to the case where XAConA and XAConB are two physical
connections to the same resource manager.
Statements
Note – Any reference to the Statement interface within this specification includes
its subclasses PreparedStatement and CallableStatement unless stated
otherwise.
Each Connection object can create multiple Statement objects that may be used
concurrently by the program. This is demonstrated in CODE EXAMPLE 13-2.
93
// get a connection from the DataSource object ds
Connection conn = ds.getConnection(user, passwd);
// create two instances of Statement
Statement stmt1 = conn.createStatement();
Statement stmt2 = conn.createStatement();
CODE EXAMPLE 13-2 Creating multiple Statement objects from a single connection
CODE EXAMPLE 13-3 creates a Statement object that returns result sets that are
scrollable, that are insensitive to changes made while the ResultSet object is open,
that can be updated, and that do not close the ResultSet objects when commit is
called.
If the SQL string being executed does not return a ResultSet object, the method
executeQuery throws an SQLException.
The method execute returns true if the first result is a ResultSet object and false
if it is an update count.
Chapter 13 Statements 95
When the method execute returns true, the method getResultSet is called to
retrieve the ResultSet object. When execute returns false, the method
getUpdateCount returns an int. If this number is greater than or equal to zero, it
indicates the update count returned by the statement. If it is -1, it indicates that there
are no more results.
If multiple results are being returned, the method getMoreResults can be called
to get the next result. As with the method execute, getMoreResults will return
true if the next result is a ResultSet object and false if the next result is an
update count or no more result are available.
CODE EXAMPLE 13-6 shows how to retrieve all the results from a Statment object.
If the current result is an update count and not a ResultSet object, any parameter
passed to getMoreResults is ignored.
To determine whether a driver implements this feature, an application can call the
DatabaseMetaData method supportsMultipleOpenResults.
Note – Some JDBC driver implementations may also apply this limit to ResultSet
methods. Please consult your driver vendor documentation for details.
Chapter 13 Statements 97
Note – In the case of Statement batching, it is implementation defined as to
whether the time-out is applied to individual SQL commands added via the
addBatch method or to the entire batch of SQL commands invoked by the
executeBatch method.
Closing a Statement object will close and invalidate any instances of ResultSet
produced by that Statement object. The resources held by the ResultSet object
may not be released until garbage collection runs again, so it is a good practice to
explicitly close ResultSet objects when they are no longer needed.
Once a Statement has been closed, any attempt to access any of its methods with
the exception of the isClosed or close methods will result in a SQLException
being thrown.
For example, the method setString is used to specify a value for a parameter marker
that expects a string. Each of these setter methods takes at least two parameters. The
first is always an int equal to the ordinal position of the parameter to be set,
starting at 1. The second and any remaining parameters specify the value to be
assigned to the parameter.
Chapter 13 Statements 99
A value must be provided for each parameter marker in the PreparedStatement
object before it can be executed. The methods used to execute a PreparedStatement
object (executeQuery, executeUpdate and execute) will throw an SQLException if a
value is not supplied for a parameter marker.
The values set for the parameter markers of a PreparedStatement object are not
reset when it is executed. The method clearParameters can be called to explicitly
clear the values that have been set. Setting a parameter with a different value will
replace the previous value with the new one.
Note – If, in the execution of a PreparedStatement object, the JDBC driver reads
values set for the parameter markers by the methods setAsciiStream,
setBinaryStream, setCharacterStream, setNCharacterStream or
setUnicodeStream, those parameters must be reset prior to the next execution of
the PreparedStatement object otherwise a SQLException will be thrown. A
SQLException will also be thrown if the same stream is used by multiple
parameter markers within the same PreparedStatement object execution.
Note – For any given Statement, an application should not modify the value
argument passed to a setXXX method after the setXXX method is called and before
the subsequent execute, executeQuery, executeUpdate, executeBatch or
clearParameters method is called. An application may modify the value
argument after the execute, executeQuery, executeUpdate, executeBatch or
clearParameters method is called, if there is a subsequent setXXX method call
that overwrites the previous value or if the Statement is not reused. Failure to
conform to this restriction may result in unpredictable behavior.
SQL:2003 provides support for national character set types, which are described in
the SQL:2003 specification to be an implementation defined character set. The
following JDBC Types may be used to access national character set types: NCHAR,
NVARCHAR, LONGNVARCHAR, and NCLOB. These types are analogous to the types
CHAR, VARCHAR, LONGVARCHAR and CLOB, except that the values are encoded using
an alternate character set, the national character set. Since Java types encode
character data using UTF-16, there is no reason to use an alternate Java type to hold
these values. However, there may be an advantage to distinguishing CLOB from
NCLOB. The JDBC specification uses Strings to represent NCHAR, NVARCHAR, and
LONGNVARCHAR data, automatically converting between the Java character set and
the national character set. JDBC uses NClob to represent NCLOB values. There is no
automatic conversion between Clob and NClob values. Please refer to the Java API
docs for java.lang.Character for additional information on how the Java
Language uses Unicode.
For maximum portability, an application must indicate to the JDBC driver when a
particular value corresponds to a national character type. When specifying a value
for a parameter marker which is a national character type, the application would call
the setNString, setNCharacterStream, setNClob, or setObject method. If
the setObject method is used , the target data type must be specified as
Types.NCHAR, Types.NCLOB, Types.NVARCHAR, or Types.LONGNVARCHAR. In
the event that an application does not indicate that a parameter marker value
corresponds to a national character type, the JDBC driver may interpret the value
incorrectly, resulting in the possibility of a data conversion error. In situations where
the JDBC driver can detect that a data conversion error might occur, the call to the
setXXX method will result in a SQLException being thrown. It might not always
possible for the driver to detect that a data conversion error may occur.
If the driver does not support national character types, an attempt to invoke the
methods setNString, setNCharacterStream, setNClob or setObject,
specifying a target data type as a national character set, may result in a
SQLException being thrown.
Note – The method setObject will do custom mapping for SQL UDTs that have a
custom mapping. See Chapter 17 “Customized Type Mapping” for more
information.
ps.setNull(2, java.sql.Types.VARCHAR);
CODE EXAMPLE 13-13 Setting a String parameter to JDBC NULL
If a Java null is passed to any of the setter methods that take a Java object, the
parameter will be set to JDBC NULL.
Note – Not all databases allow for a non-typed Null to be sent to the underly data
source. For maximum portability, the setNull or the setObject(int
parameterIndex, Object x, int sqlType) method should be used instead
of setObject(int parameterIndex, Object x).
If the statement being executed does not return a ResultSet object an SQLException
is thrown by executeQuery.
CODE EXAMPLE 13-18 shows how to retrieve all the results from a
PreparedStatment object.
All the examples in this chapter use the escape syntax for calling stored procedures.
See “Stored Procedures and Functions” on page 114.
Named parameters can also be used to specify specific parameters. This is especially
useful when a procedure has many parameters with default values. Named
parameters can be used to specify only the values that have no default value. The
name of a parameter corresponds to the COLUMN_NAME field returned by
DatabaseMetaData.getProcedureColumns.
In CODE EXAMPLE 13-21, the procedure COMPLEX_PROC takes ten parameters, but
only the first and fifth parameters, PARAM_1 and PARAM_5, are required.
It is not possible to combine setting parameters with ordinals and with names in the
same statement. If ordinals and names are used for parameters in the same
statement, an SQLException is thrown.
Note – In some cases it may not be possible to provide only some of the parameters
for a procedure. For example, if the procedure name is overloaded, the data source
determines which procedure to call based on the number of parameters. Enough
parameters must be provided to allow the data source to resolve any ambiguity.
cstmt.setString(1, “October”);
cstmt.setDate(2, date);
CODE EXAMPLE 13-22 Setting IN parameters
The values of OUT parameters can be retrieved using the appropriate getter
methods defined in the CallableStatement interface. CODE EXAMPLE 13-23 shows
the execution of a stored procedure with two OUT parameters, a string and float,
and the retrieval of the OUT parameter values.
CODE EXAMPLE 13-24 shows the stored procedure calc, which takes one INOUT float
parameter.
CODE EXAMPLE 13-27 shows how to retrieve all the results from a
CallableStatment object.
Escape processing for a Statement object is turned on or off using the method
setEscapeProcessing, with the default being on. The RowSet interface also includes a
setEscapeProcessing method. The RowSet method applies to the SQL string
used to populate a RowSet object. The setEscapeProcessing method does not
work for a PreparedStatement object because its SQL string may have been
precompiled when the PreparedStatement object was created.
For example, the following code calls the function concat with two arguments to be
concatenated:
The following syntax gets the name of the current database user:
{fn user()}
Scalar functions may be supported by different data sources with slightly different
native syntax, and they may not be supported by all drivers. The driver will either
map the escaped function call into the native syntax or implement the function
directly.
Various DatabaseMetaData methods list the functions that are supported. For
example, the method getNumericFunctions returns a comma-separated list of the Open
Group CLI names of numeric functions, the method getStringFunctions returns string
functions, and so on.
The escape syntax for scalar functions must only be used to invoke the scalar
functions defined in Appendix C “Scalar Functions". The escape syntax is not
intended to be used to invoke user-defined or vendor specific scalar functions.
The driver will replace the escape clause with the equivalent native representation.
For example, the driver might replace {d ’1999-02-28’} with '28-FEB-99' if that is the
appropriate format for the underlying data source.
{t 'hh:mm:ss'}
Note – The leading zero may be omitted when specifying the mm or dd in date or
timestamp literals
{oj <outer-join>}
(Note that curly braces ({}) in the preceding line indicate that one of the items
between them must be used; they are not part of the syntax.) The following SELECT
statement uses the escape syntax for an outer join.
The JDBC API provides three DatabaseMetaData methods for determining the
kinds of outer joins a driver supports: supportsOuterJoins,
supportsFullOuterJoins, and supportsLimitedOuterJoins.
The square brackets indicate that the (argument-list) portion is optional. Input
arguments may be either literals or parameter markers. See “Setting Parameters” on
page 106 for information on parameters.
JDBC drivers may optionally provide support for invoking user-defined or vendor
defined functions using the escape syntax for stored procedures.
{escape '<escape-character>'}
For example, the following query uses the backslash as an escape character, and
finds identifier names that begin with an underscore. Note that the Java compiler
will not recognize the backslash as a character unless it is preceded by a backslash.
Hints provided to the driver via this interface may be ignored by the driver if they
are not appropriate.
The methods getFetchDirection and getFetchSize return the current value of the
hints. If either of these methods is called before the corresponding setter method has
been called, the value returned is implementation-defined.
Additional methods allow the ordinals or names of the columns that should be
returned to be specified as an array. If the columns are not specified, the JDBC driver
implementation will determine the columns or value to return.
In CODE EXAMPLE 13-29 the Statement method executeUpdate is called with two
parameters, the first is the SQL statement to be executed, the second is an array of
String containing the column name that should be returned when
getGeneratedKeys is called:
In some cases, such as in an insert select statement, more than one value may be
returned. The ResultSet object returned by getGeneratedKeys will contain a
row for each value that a statement generated. If no values are generated, an empty
result set will be returned.
Batch Updates
The batch update facility allows multiple SQL statements to be submitted to a data
source for processing at once. Submitting multiple SQL statements, instead of
individually, can greatly improve performance. Statement, PreparedStatement,
and CallableStatement objects can be used to submit batch updates.
14.1.1 Statements
The batch update facility allows a Statement object to submit a set of
heterogeneous SQL statements together as a single unit, or batch, to the underlying
data source.
Since the JDBC 2.0 API, a Statement object has had the ability to keep track of a list
of commands—or batch—that can be submitted together for execution. When a
Statement object is created, its associated batch is empty. An application adds
commands to a statement’s batch one at a time by calling the method
Statement.addBatch and providing it with the SQL statement to be added. Any
of the commands added to a batch must return a simple update count and must not
return a ResultSet.
In CODE EXAMPLE 14-1, all of the SQL statements required to insert a new employee
into a fictitious company database are submitted as a single batch.
119
// turn off autocommit
con.setAutoCommit(false);
Statement stmt = con.createStatement();
stmt.addBatch("INSERT INTO employees VALUES (1000, 'Joe Jones')");
stmt.addBatch("INSERT INTO departments VALUES (260, 'Shoe')");
stmt.addBatch("INSERT INTO emp_dept VALUES (1000, 260)");
The entries in the array are ordered according to the order in which the commands
were processed (which, again, is the same as the order in which the commands were
originally added to the batch). When all of the commands in a batch have been
executed successfully, an entry in the array of update counts may have the following
values :
■ 0 or greater — the command was processed successfully and the value is an
update count indicating the number of rows in the database that were affected by
the command’s execution
Calling the method executeBatch closes the calling Statement object’s current
result set if one is open. The statement’s batch is reset to empty once
executeBatch returns. The behavior of the methods executeQuery,
executeUpdate, and execute is implementation-defined when a statement’s
batch is non-empty.
Only DDL and DML commands that return a simple update count may be executed
as part of a batch. The method executeBatch throws a BatchUpdateException
if any of the commands in the batch fail to execute properly or if a command
attempts to return a result set. When a BatchUpdateException is thrown, an
application can call the BatchUpdateException.getUpdateCounts method to
obtain an integer array of update counts that describes the outcome of the batch
execution.
If a driver stops processing after the first failure, the array returned by the method
BatchUpdateException.getUpdateCounts will always contain fewer entries
than there were statements in the batch. Since statements are executed in the order
that they are added to the batch, if the array contains N elements, this means that the
first N elements in the batch were processed successfully when executeBatch was
called.
JDBC drivers that do not continue processing after a failure never return
Statement.EXECUTE_FAILED in an update count array. Drivers of this type
simply return a status array containing an entry for each command that was
processed successfully.
CODE EXAMPLE 14-2 inserts two new employee records into a database as a single
batch. The PreparedStatement interface setter methods are used to create each
parameter set, one for each employee. The PreparedStatement.addBatch
method adds a set of parameters to the current command.
stmt.setInt(1, 2000);
stmt.setString(2, "Kelly Kaufmann");
stmt.addBatch();
Stored procedures invoked using the batch update facility with a callable statement
must return a maximum of one update counts, if no update count is returned the
array element value will be Statement.SUCCESS_NO_INFO. Additionally, a
batchable stored procedure may not have OUT or INOUT parameters. The
CallableStatement.executeBatch method throws an SQLException, not a
sub-class of SQLException, if this restriction is violated. Error handling is
analogous to that for PreparedStatement objects.
Result Sets
The ResultSet interface provides methods for retrieving and manipulating the
results of executed queries.
1. TYPE_FORWARD_ONLY
■ The result set is not scrollable; its cursor moves forward only, from before the first
row to after the last row.
■ The rows contained in the result set depend on how the underlying database
materializes the results. That is, it contains the rows that satisfy the query at
either the time the query is executed or as the rows are retrieved.
2. TYPE_SCROLL_INSENSITIVE
■ The result set is scrollable; its cursor can move both forward and backward
relative to the current position, and it can move to an absolute position.
125
■ The result set is insensitive to changes made to the underlying data source while
it is open. It contains the rows that satisfy the query at either the time the query is
executed or as the rows are retrieved.
3. TYPE_SCROLL_SENSITIVE
■ The result set is scrollable; its cursor can move both forward and backward
relative to the current position, and it can move to an absolute position.
■ The result set reflects changes made to the underlying data source while the result
set remains open.
If the driver does not support the type supplied to the methods createStatement,
prepareStatement, or prepareCall, it generates an SQLWarning on the
Connection object that is creating the statement. When the statement is executed,
the driver returns a ResultSet object of a type that most closely matches the
requested type. An application can find out the type of a ResultSet object by
calling the method ResultSet.getType.
If the driver does not support the concurrency level supplied to the methods
createStatement, prepareStatement, or prepareCall, it generates an
SQLWarning on the Connection object that is creating the statement. An
application can find out the concurrency of a ResultSet object by calling the
method ResultSet.getConcurrency.
1. HOLD_CURSORS_OVER_COMMIT
■ ResultSet objects (cursors) are not closed; they are held open when the method
commit is called.
2. CLOSE_CURSORS_AT_COMMIT
■ ResultSet objects (cursors) are closed when commit is called. Closing cursors at
commit can result in better performance for some applications.
If the driver does not support the holdability level supplied to the methods
createStatement, prepareStatement, or prepareCall, it generates an
SQLWarning on the Connection object that is creating the statement. In the case of
a SQL CALL statement, the holdability of any returned ResultSets are determined
by the stored procedure, independent of the holdability level specified to the SQL
CALL statement.
For a ResultSet object that is of type TYPE_FORWARD_ONLY, the only valid cursor
movement method is next. All other cursor movement methods throw an
SQLException.
Two getter methods exist for each JDBC type: one that takes the column index as its
first parameter and one that takes the column label.
The columns are numbered from left to right, as they appear in the select list of the
query, starting at 1.
Column labels supplied to getter methods are case insensitive. If a select list contains
the same column more than once, the first instance of the column will be returned.
The index of the first instance of a column label can be retrieved using the method
findColumn. If the specified column is not found, the method findColumn throws
an SQLException.
ResultSet rs = stmt.executeQuery(sqlstring);
int colIdx = rs.findColumn(“ISBN”);
CODE EXAMPLE 15-3 Mapping a column label to a column index
ResultSet rs = stmt.executeQuery(sqlString);
ResultSetMetaData rsmd = rs.getMetaData();
int colType [] = new int[rsmd.getColumnCount()];
for (int idx = 0, int col = 1; idx < colType.length; idx++, col++)
colType[idx] = rsmd.getColumnType(col);
CODE EXAMPLE 15-4 Retrieving result set metadata
When the column value in the database is SQL NULL, it may be returned to the Java
application as null, 0, or false, depending on the type of the column value.
Column values that map to Java Object types are returned as a Java null; those
that map to numeric types are returned as 0; those that map to a Java boolean are
returned as false. Therefore, it may be necessary to call the wasNull method to
determine whether the last value retrieved was a SQL NULL.
The ResultSet interface contains two update methods for each JDBC type, one
specifying the column to be updated as an index and one specifying the column
label as it appears in the select list.
Column labels supplied to updater methods are case insensitive. If a select list
contains the same column more than once, the first instance of the column will be
updated.
The method updateRow is used to apply all column changes to the current row, and
to clear the record of changes made by prior update methods. The changes are not
made to the row until updateRow has been called. If updateRow is called on a
ResultSet whose concurrency level is ResultSet.CONCUR_READ_ONLY, then a
SQLException must be thrown. If the concurrency level is
ResultSet.CONCUR_UPDATABLE and updateRow is called without changes being
made to the row, the call will be considered a no-op.
The method cancelRowUpdates can be used to back out changes made to the row
before the method updateRow is called. CODE EXAMPLE 15-5 shows the current row
being updated to change the value of the column “author” to “Zamyatin, Evgenii
Ivanovich”:
A ResultSet object may be able to use the method rowUpdated to detect rows
that have had the method updateRow called on them. The method
DatabaseMetaData.updatesAreDetected(int type) returns true if a
ResultSet object of the specified type can determine if a row is updated using the
method rowUpdated and false otherwise.
For any given ResultSet, an application should not modify the value argument
passed to a updateXXX method after the updateXXX method is called and before the
subsequent updateRow or cancelRowUpdates method is called. An application
may modify the value argument after the updateRow or cancelRowUpdates
method is called, if there is updateXXX that overwrites the previous value or if the
update row is not reused. Failure to conform to this restriction may result in
unpredictable behavior.
rs.absolute(4);
rs.deleteRow();
CODE EXAMPLE 15-6 Deleting a row in a ResultSet object
After the method deleteRow has been called, the current row is deleted in the
underlying data source. This deletion is visible as a change in the open ResultSet
object if the row is either removed or replaced by an empty or invalid row.
if (dbmd.ownDeletesAreVisible(ResultSet.TYPE_SCROLL_INSENSITIVE) &&
dbmd.deletesAreDetected(ResultSet.TYPE_SCROLL_INSENSITIVE)) {
while (rs.next) {
if (rs.rowDeleted()) {
continue;
} else {
// process row
...
}
}
} else {
// if up-to-date data is needed, it is better to close this
// ResultSet object and reexecute the query to get an updated
// ResultSet object
...
rs.close();
break;
}
Note – CODE EXAMPLE 15-7 does not cover the case where ownDeletesAreVisible
returns true and deletesAreDetected returns false. This will cause an
SQLException to be thrown when the cursor is positioned on a deleted row, so an
implementation with these characteristics requires that an application handle the
exception. Such an implementation does not appear to be a very likely.
After the method deleteRow has been called, the cursor will be positioned before
the next valid row. If the deleted row is the last row, the cursor will be positioned
after the last row.
2. Set the values for the columns of the row using the ResultSet interface update
methods
CODE EXAMPLE 15-8 shows the steps necessary to insert a new row into the table
booklist.
Each column in the insert row that does not allow null as a value and does not
have a default value must be given a value using the appropriate update method. If
this is not the case, the method insertRow will throw an SQLException.
If the ResultSet objects of the specified type can identify newly inserted rows, the
method DatabaseMetaData.insertsAreDetected(int type) will return
true. This indicates that the inserted rows are visible to the ResultSet object.
For any given ResultSet, an application should not modify the value argument
passed to a updateXXX method after the updateXXX method is called and before the
subsequent insertRow method is called. An application may modify the value
argument after the insertRow method is called, if there is updateXXX that
overwrites the previous value or if the insert row is not reused. Failure to conform to
this restriction may result in unpredictable behavior.
Note – Some JDBC driver implementations may also implicitly close the
ResultSet when the ResultSet type is TYPE_FORWARD_ONLY and the next
method of ResultSet returns false.
Once a ResultSet has been closed, any attempt to access any of its methods with
the exception of the isClosed or close methods will result in a SQLException
being thrown. ResultSetMetaData instances that were created by a ResultSet
that has been closed are still accessible.
139
■ New built-in types — new types added by SQL:2003
■ XML — a XML Object
■ User Defined Types
■ Structured type — a user-defined type; for example:
CREATE TYPE PLANE_POINT AS (X FLOAT, Y FLOAT) NOT FINAL
■ DISTINCT type — a user-defined type based on a built-in type; for example:
CREATE TYPE MONEY AS NUMERIC(10,2) FINAL
■ Constructed types — new types based on a given base type
■ REF(structured-type) — a pointer that persistently denotes an instance of
a structured type that resides in the database
■ base-type ARRAY[n] — an array of n base-type elements
■ Locators — entities that are logical pointers to data that resides on the database
server. A LOCATOR exists in the client environment and is a transient, logical
pointer to data on the server. A locator typically refers to data that is too large to
materialize on the client, such as images or audio. There are operators defined at
the SQL level to retrieve random-access pieces of the data denoted by the locator.
■ LOCATOR(structured-type) — locator to a structured instance in server
■ LOCATOR(array) — locator to an array in server
■ LOCATOR(blob) — locator to a Binary Large Object in server
■ LOCATOR(clob) — locator to a Character Large Object in server
■ Type for managing data external to the data source
■ Datalink — a reference to data external to the data source that is managed by
the data source. Datalink values are part of SQL MED (Management of
External Data), a part of the SQL ANSI/ISO standard specification. Having the
data source manage the reference to external data has several advantages:
ii. Access control — access to the data may be configured such that it is
controlled by the data source instead of the file system
iv. Transaction consistency — changes that affect both relational and external
data are executed in a transactional context to preserve the integrity and
consistency of the data
The other advanced data types with default mappings to the Java programming
language are:
■ DISTINCT — the type to which the base type is mapped. For example, a
DISTINCT value based on an SQL NUMERIC type maps to a
java.math.BigDecimal type because NUMERIC maps to BigDecimal in the
Java programming language.
■ DATALINK — a java.net.URL object.
For locator based implementations, Blob, Clob and NClob objects remain valid for
at least the duration of the transaction in which they are created, unless their free
method is invoked or they are garbage collected.
For implementations that fully materialize the Large Object (LOB), the Blob, Clob
and NClob objects will remain valid until such time as the free method is called or
the LOB is garbage collected.
Portable applications should not depend upon the LOB persisting past the end of the
transaction.
The Blob interface contains operations for returning the length of the BLOB value, a
specific range of bytes contained in the BLOB value, and so on. The Clob interface
contains corresponding operations that are character based. The API documentation
gives more details.
The example, CODE EXAMPLE 16-3, retrieves a 100 bytes of data from a BLOB and
CLOB object starting at offset 250.
InputStream is = aBlob.getBinaryStream(250, 100);
BufferedReader br = aClob.getCharacterStream(250, 100);
CODE EXAMPLE 16-3 Retrieving a data subset from a BLOB and CLOB object
The setBinaryStream and setObject methods may also be used to set a Blob
object as a parameter in a PreparedStatement object. The setAsciiStream,
setCharacterStream, and setObject methods are alternate means of setting a
Clob object as a parameter.The methods setNCharacterStream, and setObject
provide an alternate means of setting a NClob object as a parameter.
Note – For maximum portability, applications should only rely on the storing of
locator-based Blob, Clob and NClob objects within the connection that created the
LOB.
Similarly, the Clob or NClob methods setString and truncate can be used to
change the value of a column containing a Clob or a NClob object.
The semantics of updates made to LOB objects are implementation defined. In some
implementations, the changes may be made to a copy of the LOB, and in others the
changes are made directly to the LOB. In implementations where the changes are
made to a copy of the LOB, a separate update statement must be issued to update
the LOB stored in the DBMS.
In CODE EXAMPLE 16-6, the method Clob.free is used to release the resources held
for a previously created Clob object.
Clob aClob = con.createClob();
int numWritten = aClob.setString(1, val);
aClob.free();
CODE EXAMPLE 16-6 releasing resources for a Clob object
For example, CODE EXAMPLE 16-8 retrieves a SQLXML value from the first column of
the ResultSet rs.
SQLXML objects remain valid for at least the duration of the transaction in which
they are created, unless their free method is invoked.
The example, CODE EXAMPLE 16-11, retrieves and parses the data from an SQLXML
object using the SAXSource object returned by the invocation of the getSource
method:
SQLXML xmlVal= rs.getSQLXML(1);
SAXSource saxSource = sqlxml.getSource(SAXSource.class);
XMLReader xmlReader = saxSource.getXMLReader();
xmlReader.setContentHandler(myHandler);
xmlReader.parse(saxSource.getInputSource());
CODE EXAMPLE 16-13 uses the method setResult to return a SAXResult object to
populate a newly created SQLXML object.
SQLXML sqlxml = con.createSQLXML();
SAXResult saxResult = sqlxml.setResult(SAXResult.class);
ContentHandler contentHandler =
saxResult.getXMLReader().getContentHandler();
contentHandler.startDocument();
// set the XML elements and attributes into the result
contentHandler.endDocument();
CODE EXAMPLE 16-13 Using the setResult method to obtain a SAXResult object
}
CODE EXAMPLE 16-14 Using setCharacterStream to initialize a SQLXML object
In CODE EXAMPLE 16-15, the method SQLXML.free is used to release the resources
held for a previously created SQLXML object.
SQLXML xmlVar = con.createSQLXML();
xmlVar.setString( val);
xmlVar.free();
CODE EXAMPLE 16-15 releasing resources for a SQLXML object
By default, a JDBC driver should implement the Array interface using the
appropriate locator type. An application does not deal directly with the locator type
that is defined in SQL.
For locator based implementations, Array objects remain valid for at least the
duration of the transaction in which they are created, unless their free method is
invoked or they are garbage collected.
For implementations that fully materialize the Array objects, the objects will remain
valid until such time as the free method is called or the object is garbage collected.
Array a = rs.getArray(1);
By default, a JDBC driver should implement the Array interface using an SQL
LOCATOR(array) internally. Also by default, Array objects remain valid only
during the transaction in which they are created.
Note – For maximum portability, applications should only rely on the storing of
locator-based Array objects within the connection that created the Array.
In CODE EXAMPLE 16-19, the method Array.free is used to release the resources
held for a previously created Array object.
Array aArray = con.createArrayOf("VARCHAR", coffees);
...
aArray.free();
CODE EXAMPLE 16-19 releasing resources for a Array object
16.6.5 Metadata
The type REF is defined in the class 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.
CODE EXAMPLE 16-24 uses the method getBigDecimal to retrieve a MONEY value
because the underlying SQL NUMERIC type is mapped to the
java.math.BigDecimal type.
java.math.BigDecimal bd = rs.getBigDecimal(1);
CODE EXAMPLE 16-24 Retrieving a distinct type
16.7.3 Metadata
The type code DISTINCT is defined in the class java.sql.Types. This value is
returned by methods such as DatabaseMetaData.getTypeInfo and
DatabaseMetaData.getColumns when a JDBC driver supports this data type.
Each row in the ResultSet object returned by the method getUDTs describes a
UDT. Each row contains the following columns:
The Struct interface contains methods for retrieving the attributes of a structured
type as an array of java.lang.Object values. By default, a JDBC driver
materializes the contents of a Struct prior to returning a reference to it to the
application. Also, by default a Struct object is considered valid as long as the Java
application maintains a reference to it.
Note – For maximum portability, applications should only rely on the storing of
locator-based structured type objects within the connection that created the
structured type.
16.8.4 Metadata
The type code STRUCT is defined in the class java.sql.Types. This value is
returned by methods such as DatabaseMetaData.getTypeInfo and
DatabaseMetaData.getColumns when a JDBC driver supports structured data
types.
If the driver does not support UDTs or no matching UDTs are found, an empty
result set is returned. See section 16.7.3 for a description of the result set returned by
the method getUDTs.
16.9 Datalinks
A DATALINK value references a file outside of the underlying data source that the
data source manages.
In cases where the type of URL returned by the methods getObject or getURL is
not supported by the Java platform, the URL can be retrieved as a String by calling
the method getString.
ROWID_UNSUPPORTED Indicates that this data source does not support the
ROWID type.
ROWID_VALID_OTHER Indicates that the lifetime of a RowId from this data
source is implementation dependant.
ROWID_VALID_TRANSACTION Indicates that the lifetime of a RowId from this data
source is at least the containing transaction as long
as the identified row is not deleted.
ROWID_VALID_SESSION Indicates that the lifetime of a RowId from this data
source is at least the containing session as long as
the identified row is not deleted.
ROWID_VALID_FOREVER Indicates that the lifetime of a RowId from this data
source is, effectively, unlimited as long as the
identified row is not deleted.
A RowId value may also be used in an updatable ResultSet to update a column with
a specific RowId value.
ResultSet rs = ...
rs.next();
rs.updateRowId(1, rowId_1);
CODE EXAMPLE 16-31 Setting a RowId value in the current row of a ResultSet
A RowId object value is typically not portable between data sources and should be
considered as specific to the data source when using the set or update method in
PreparedStatement and ResultSet objects respectively. It is therefore
inadvisable to get a RowId from a ResultSet object with a connection ’Foo’ and
then attempt to use the RowId in a unrelated ResultSet object with a connection
’Bar’.
This chapter describes the support that the JDBC API provides for mapping SQL
structured and distinct types to classes in the Java programming language. Because
the mechanism for this custom mapping is an extension of the existing getObject
and setObject mechanism, it involves minimal extensions to the JDBC API from
the user’s point of view.
1. Writing an implementation of the SQLData interface for the UDT. This class
typically maps the attribute(s) of an SQL structured type (or the single attribute of
a DISTINCT type) to fields. There is, however, great latitude allowed in how a
UDT is custom mapped. It is expected that most SQLData implementations will
be created using a tool.
161
b. The Class object for the SQLData implementation. It is an error if the class
listed in a type map entry does not implement the SQLData interface.
The method Connection.getTypeMap returns the type map associated with the
Connection object conn; the method Connection.setTypeMap sets the given
java.util.Map object as the type map for conn.
When an SQL value with a custom mapping is being retrieved (by the method
ResultSet.getObject, CallableStatement.getObject, or any of the other
methods that materialize an SQL value’s data on the client), the driver will check to
see if there is an entry in the connection’s type map for the SQL value that is to be
retrieved. If there is, the driver will map the SQL UDT to the class specified in the
type map. If there is no entry for the UDT in the connection’s type map, the UDT is
mapped to the default mapping.
Certain methods may take a type map as a parameter. A type map supplied as a
parameter supersedes the type map associated with the connection. A UDT that does
not have an entry in the type map supplied as a parameter will be mapped to the
default mapping. When a type map is explicitly supplied to a method, the
connection’s type map is never used.
The SQLData interface contains methods that convert instances of SQL UDTs to Java
class instances and that convert Java class instances back to SQL UDTs. For example,
the method SQLData.readSQL reads a stream of data values and builds a Java
object, while the method SQLData.writeSQL writes a sequence of values from a
Java object to a stream. These methods will typically be generated by a tool that
understands the database schema.
If multiple inheritance is used, then the attributes of supertypes should appear in the
stream in the order in which the supertypes are listed in the type declaration. This
protocol does not require the database server to have any knowledge of the Java
programming language. However, as there is no support for multiple inheritance in
SQL:2003, this issue should not arise.
SQLInput sqlin;
...
this.str = sqlin.readString();
this.blob = sqlin.readBlob();
The SQLInput.readString method reads a String value from the stream; the
SQLInput.readBlob method reads a Blob value from the stream. By default, the
Blob interface is implemented using an SQL locator, so calling the method
readBlob doesn’t materialize the SQL BLOB contents on the client. The
SQLInput.readObject method retrieves an object reference from the stream. In
the example, the Object returned is narrowed to an Employee object.
There are a number of additional methods defined on the SQLInput interface for
reading each of the types (readLong, readBytes, and so on). The
SQLInput.wasNull method can be called to check whether the last value read was
SQL NULL in the database.
sqlout.writeString(this.str);
sqlout.writeBlob(this.blob);
sqlout.writeObject(this.emp);
CODE EXAMPLE 17-3 Storing data using the SQLOutput interface
The example shows how the contents of an SQLData object can be written to an
SQLOutput stream. The SQLData object—the this object in the example—contains
three persistent fields: the String str, the Blob object blob, and the Employee
object emp. Each field is written in turn to the SQLOutput stream, SQLData. The
SQLOutput interface contains methods for writing each of the types defined in the
JDBC API.
The types created in CODE EXAMPLE 17-4 are presumed to be created in the current
schema for the following examples.
CODE EXAMPLE 17-5 creates two tables that are maintained by the DBMS
automatically. The CREATE statements do two things:
1. Create tables that store instances of the structured types named in the OF part of
the statement (RESIDENCE in the first one, PERSON in the second). Each of the
subsequent INSERT INTO statements adds a new row representing an instance of
the UDT.
2. Create a REF value that is a pointer to each instance that is inserted into the table.
As indicated in the CREATE statement, the REF value is generated by the system,
which is done implicitly. Because REF values are stored in the table, they are
persistent pointers. This contrasts with LOCATOR types, which are logical pointers
but exist only as long as the transactions in which they are created.
CODE EXAMPLE 17-6 uses INSERT INTO statements to populate the tables created in
CODE EXAMPLE 17-5. For example, the INSERT INTO PEOPLE statement inserts an
instance of the UDT PERSON into the table PEOPLE. When this command is executed,
the DBMS will also automatically generate a REF value that is a pointer to this
instance of PERSON and store it in the column OID (the column name specified in the
CREATE statement that created the table PEOPLE).
Each column value in these special tables is an attribute of the UDT, which may
itself be a UDT. For example, the first attribute of the UDT PERSON is the value in
the column NAME, which must be an instance of the UDT FULLNAME. The example
assumes that the UDT FULLNAME has an additional two parameter constructor.
CODE EXAMPLE 17-8 puts entries for custom mappings in the connection’s type map.
Then it retrieves the Ref instance stored in the OCCUPANT column of the table
HOMES. This Ref instance is set as a parameter in the where clause of the query to
get the name of the occupant. When the method getObject is called to retrieve an
instance of FULLNAME, the driver looks in the connections type map and uses the
SQLData implementation, Fullname, to custom map the FULLNAME value.
PreparedStatement pstmt;
ResultSet rs;
pstmt = con.prepareStatement("SELECT OCCUPANT FROM HOMES");
rs = pstmt.executeQuery();
rs.next();
Ref ref = rs.getRef(1);
pstmt = con.prepareStatement(
"SELECT FULLNAME FROM PEOPLE WHERE OID = ?");
pstmt.setRef(1, ref);
rs = pstmt.executeQuery(); rs.next();
Fullname who = (Fullname)rs.getObject(1);
The following Java classes can represent data of those SQL types. Class Student
extends Person, mirroring the SQL type hierarchy. Methods SQLData.readSQL
and SQLData.writeSQL of the subclass cascade each call to the corresponding
method in its superclass in order to read or write the superclass attributes before
reading or writing the subclass attributes.
import java.sql.*;
...
public class Person implements SQLData {
public String name;
public Date birth;
The Java class hierarchy need not mirror the SQL inheritance hierarchy. For
example, the class Student above could have been declared without a superclass.
In this case, Student could contain fields to hold the inherited attributes of the SQL
type STUDENT as well as the attributes declared by STUDENT itself.
If transform groups are used for a user-defined type, and the application has not
defined a mapping for that type to a Java class, then the ResultSetMetaData
method getColumnClass should return the Java class corresponding to the data
type produced by the transformation function (that is, String for a VARCHAR).
A user can even map a single SQL type to different Java classes, depending on
arbitrary conditions. To do that, the user must customize the implementation of
SQLData.readSQL to construct and return objects of different classes under
different conditions.
Similarly, the user can map a single SQL value to a graph of Java objects. Again, that
is accomplished by customizing the implementation of the method
SQLData.readSQL to construct multiple objects and distribute the SQL attributes
into fields of those objects.
Relationship to Connectors
The Java EE Connector Architecture 1.5 Specification defines a set of contracts that
allow a resource adapter to extend a container in a pluggable way. A resource
adapter provides connectivity to an external system from the application server. The
resource adapter’s functionality is similar to that provided by the JDBC interfaces
used in the Java EE platform to establish a connection with a data source. These
interfaces, which the Connector specification refers to as the service provider interface
(SPI), are the following:
■ DataSource
■ ConnectionPoolDataSource
■ XADataSource
The following standard contracts are defined between an application server and a
data source system:
■ A connection management contract that enables application components to
connect to a data source.
The connection management contract is equivalent to the services described by
the JDBC interfaces DataSource and ConnectionPoolDataSource.
■ A transaction management contract between the transaction manager and a data
source supporting transactional access to its resources.
177
The transaction contract is equivalent to the services described by the JDBC
interface XADataSource.
■ A security contract that enables secure access to a data source.
The security contract does not have an equivalent in the JDBC API.
Authentication in the JDBC API always consists of providing a user name and
a password.
The JDBC specification does not make a distinction between its application
programming interface (API) and the SPI. However, a driver can map the JDBC
interfaces in the SPI to the Connector system contracts.
1. To write a set of classes that wrap a JDBC driver and implement the Connector
system contracts. Constructing these wrappers is fairly straightforward and
should allow JDBC driver vendors to provide resource adapters quickly enough
so that they are available when application server vendors have implemented the
Connector contracts.
2. To implement the Connector system contracts natively. This approach avoids the
overhead of wrapper classes, but the implementation effort may be more
involved and time-consuming. This alternative is a more long-term option.
Either approach will allow JDBC driver vendors to package their drivers as resource
adapters and get all the benefits of pluggability, packaging, and deployment.
Note – There are no plans to deprecate or remove the current JDBC interfaces,
DataSource, ConnectionPoolDataSource and XADataSource.
The deployment descriptor maps the classes in the resource adapter to the specific
roles that they perform. The descriptor also details the capabilities of the resource
adapter in terms of what level of transactional support it provides, the kind of
security it supports, and so on.
CODE EXAMPLE 18-1 is an example of a deployment descriptor for a JDBC driver. The
class com.acme.JdbcManagedConnectionFactory could be supported by an
implementation of javax.sql.XADataSource. The resource adapter section
contains information on the level of transaction support, the mechanism used for
authentication, and configuration information for deploying the data source in the
JNDI namespace.
<resourceadapter-class>
com.acme.JdbcResourceAdapter
<authentication-mechanism>
<authentication-mechanism-type>
Wrapper Interface
The Wrapper interface provides a mechanism for JDBC users to be able to access an
instance of a resource which has been wrapped for architectural reasons. This
mechanism helps to eliminate the need to use non-standard means to access vendor
specific resources.
183
19.1.1 unwrap method
The unwrap method is used to return an object that implements the specified
interface allowing access to vendor-specific methods. The object that is returned may
either be the object found to implement the specified interface or a proxy for that
object.
If the receiver implements the specified interface then that is the object which is
returned. If the receiver is a wrapper and the wrapped object implements the
interface then the wrapped object or a proxy for the object is returned. Otherwise the
returned object is the result of calling unwrap recursively on the wrapped object. If
the receiver is not a wrapper and does not implement the specified interface, then an
SQLException is thrown.
If the object implements the specified interface then a value of true is returned. If
this instance is a wrapper, then the method isWrapperFor is recursively called on
the wrapped object. If the instance does not implement the interface and is not a
wrapper, a value of false is returned.
CODE EXAMPLE 19-1 provides an example of using the Wrapper interface methods:
Revision History
185
TABLE A-1 Revision History
Renamed ClientInfoException to
SQLClientInfoException
Revised
DatabaseMetaData.getColumns to be
ordered by TABLE_CAT,
TABLE_SCHEM, TABLE_NAME
and ORDINAL_POSITION to
match ODBC. Also added a new
column IS_AUTOINCREMENT
to be returned in the ResultSet.
Changed the method name of
DatabaseMetaData.getFunctionPara
meters to getFunctionColumns. Also
allow for getFunctions and
getFunctionColumns to support
system functions and return
ResultSet.
Clarified that
Statement.setMaxFieldSize only
effects reads to align it with the
javadocs and current JDBC driver
implementations
The tables provided here describe the various mappings and conversions that
drivers must support.
This table shows the conceptual correspondence between JDBC types and Java types.
A programmer should write code with this mapping in mind. For example, if a
value in the database is a SMALLINT, a short should be the data type used in a
JDBC application.
All CallableStatement getter methods except for getObject use this mapping. The
getObject methods for both the CallableStatement and ResultSet interfaces use the
mapping in TABLE B-3.
This table shows the mapping a driver should use for the updater methods in the
ResultSet interface and for IN parameters. PreparedStatement setter methods and
RowSet setter methods use this table for mapping an IN parameter, which is a Java
type, to the JDBC type that will be sent to the database. Note that the setObject
methods for these two interfaces use the mapping shown in TABLE B-4.
Note – The JDBC 1.0 specification defined the Java object mapping for the
SMALLINT and TINYINT JDBC types to be Integer. The Java language did not
include the Byte and Short data types when the JDBC 1.0 specification was
finalized. The mapping of SMALLINT and TINYINT to Integer is maintained to
preserve backwards compatibility.
191
TABLE B-4 Java Object Types Mapped to JDBC Types
TABLE B-5 Conversions by setObject and setNull from Java Object Types to JDBC
Types
This table shows which JDBC types may be specified as the target JDBC type to the
methods PreparedStatement.setObject, PreparedStatement.setNull, RowSet.setNull
and RowSet.setObject.
This table shows which JDBC types may be returned by ResultSet getter methods. A
bold X indicates the method recommended for retrieving a JDBC type. A plain x
indicates for which JDBC types it is possible to use a getter method.
CHAR String
VARCHAR String
LONGVARCHAR String
NUMERIC java.math.BigDecimal
DECIMAL java.math.BigDecimal
BIT boolean
BOOLEAN boolean
TINYINT byte
SMALLINT short
INTEGER int
BIGINT long
REAL float
FLOAT double
DOUBLE double
BINARY byte[]
VARBINARY byte[]
LONGVARBINARY byte[]
DATE java.sql.Date
TIME java.sql.Time
TIMESTAMP java.sql.Timestamp
CLOB java.sql.Clob
BLOB java.sql.Blob
ARRAY java.sql.Array
DISTINCT mapping of underlying type
STRUCT java.sql.Struct
REF java.sql.Ref
DATALINK java.net.URL
JAVA_OBJECT underlying Java class
ROWID java.sql.RowId
NCHAR String
NVARCHAR String
LONGNVARCHAR String
NCLOB java.sql.NClob
SQLXML java.sql.SQLXML
CHAR String
VARCHAR String
LONGVARCHAR String
NUMERIC java.math.BigDecimal
DECIMAL java.math.BigDecimal
BIT Boolean
BOOLEAN Boolean
TINYINT Integer
SMALLINT Integer
INTEGER Integer
BIGINT Long
REAL Float
FLOAT Double
DOUBLE Double
BINARY byte[]
VARBINARY byte[]
LONGVARBINARY byte[]
DATE java.sql.Date
TIME java.sql.Time
TIMESTAMP java.sql.Timestamp
DISTINCT Object type of underlying type
CLOB java.sql.Clob
BLOB java.sql.Blob
ARRAY java.sql.Array
STRUCT java.sql.Struct or java.sql.SQLData
REF java.sql.Ref
DATALINK java.net.URL
JAVA_OBJECT underlying Java class
ROWID java.sql.RowId
NCHAR String
NVARCHAR String
LONGNVARCHAR String
NCLOB java.sql.NClob
SQLXML java.sql.SQLXML
LONGNVARCHAR
LONGVARCHAR
JAVA_OBJECT
VARBINARY
TIMESTAMP
NVARCHAR
SMALLINT
BOOLEAN
VARCHAR
DATALINK
NUMERIC
DECIMAL
INTEGER
DOUBLE
SQLXML
STRUCT
TINYINT
BINARY
NCHAR
NCLOB
ARRAY
BIGINT
ROWID
FLOAT
CHAR
BLOB
CLOB
REAL
DATE
TIME
REF
BIT
String x x x x x x x x x x x x x x x x x x x x x x x
java.math. x x x x x x x x x x x x x x
BigDecimal
Boolean x x x x x x x x x x x x x x
Byte x x x x x x x x x x x x x x
Short x x x x x x x x x x x x x x
Integer x x x x x x x x x x x x x x
Long x x x x x x x x x x x x x x
Float x x x x x x x x x x x x x x
Double x x x x x x x x x x x x x x
byte[] x x x
java.sql. x x x x x
Date
java.sql. x x x x
Time
java.sql. x x x x x x
Timestamp
java.sql.Array x
java.sql.Blob x
java.sql.Clob x
java.sql.Struct x
java.sql.Ref x
java.net.URL x
Java class x
java.sql.RowId x
java.sql.NClob x
java.sql.SQLXML x
TABLE B-5 Conversions Performed by setObject and setNull Between Java Object
Types and Target JDBC Types
LONGNVARCHAR
LONGVARCHAR
JAVA_OBJECT
VARBINARY
TIMESTAMP
NVARCHAR
SMALLINT
BOOLEAN
VARCHAR
DATALINK
NUMERIC
DECIMAL
INTEGER
DOUBLE
SQLXML
STRUCT
TINYINT
BINARY
NCHAR
NCLOB
ARRAY
BIGINT
ROWID
FLOAT
CHAR
CLOB
BLOB
REAL
DATE
TIME
REF
BIT
getByte X x x x x x x x x x x x x x x
getShort x X x x x x x x x x x x x x
getInt x x X x x x x x x x x x x x
getLong x x x X x x x x x x x x x x
getFloat x x x x X x x x x x x x x x
getDouble x x x x x X X x x x x x x x
getBigDecimal x x x x x x x X X x x x x x
getBoolean x x x x x x x x x X X x x x
getString x x x x x x x x x x x X X x x x x x x x x x x x
getNString x x x x x x x x x x x x x x x x x x x x x X X x
getBytes X X x
getDate x x x X x
getTime x x x X x
getTimestamp x x x x x X
getAsciiStream x x X x x x x x
getBinaryStream x x X x x
getCharacterStream x x X x x x x x x x x x
getNCharacterStream x x x x x x x x x X x x
getClob X x
getNClob x X
getBlob X
getArray X
getRef X
getURL X
getObject x x x x x x x x x x x x x x x x x x x x x x x x x X X x x x x x x
getRowId X
getSQLXML X
TABLE B-6 Use of ResultSet getter Methods to Retrieve JDBC Data Types
Scalar Functions
The JDBC API supports escape syntax for numeric, string, time, date, system, and
conversion functions on scalar values. These scalar functions may be used in SQL
strings as described in Section 13.4.1 “Scalar Functions” on page 13-112. The Open
Group CLI specification provides more information on the semantics of the scalar
functions. The scalar functions are listed below for reference.
If a DBMS supports a scalar function, the driver should also. Because scalar
functions are supported by different DBMSs with slightly different syntax, it is the
driver’s job either to map them into the appropriate syntax or to implement the
functions directly in the driver.
A user should be able to find out which functions are supported by calling metadata
methods. For example, the method DatabaseMetaData.getNumericFunctions returns a
comma separated list of the Open Group CLI names of the numeric functions
supported. Similarly, the method DatabaseMetaData.getStringFunctions returns a list of
string functions supported, and so on.
201
DEGREES(number) Degrees in number radians
EXP(float) Exponential function of float
FLOOR(number) Largest integer <= number
LOG(float) Base e logarithm of float
LOG10(float) Base 10 logarithm of float
MOD(integer1, integer2) Remainder for integer1 / integer2
PI() The constant pi
POWER(number, power) number raised to (integer) power
RADIANS(number) Radians in number degrees
RAND(integer) Random floating point for seed integer
ROUND(number, places) number rounded to places places
SIGN(number) -1 to indicate number is < 0;
0 to indicate number is = 0;
1 to indicate number is > 0
SIN(float) Sine of float radians
SQRT(float) Square root of float
TAN(float) Tangent of float radians
TRUNCATE(number, places) number truncated to places places
Note – Previous versions of the JDBC specification defined the SQLtype without the
SQL_ prefix, for example BIGINT and BINARY. JDBC drivers should continue to
support this form for SQLtype.
Related Documents
Data Management: SQL Call Level Interface (X/Open SQL CLI) Available at http:/
/www.opengroup.org.
JDBC 3.0 Specification (JDBC 3.0). Copyright 2001, Sun Microsystems, Inc. Available
at http://java.sun.com/products/jdbc.
JDBC 2.1 API (JDBC 2.1). Copyright 1998, Sun Microsystems, Inc. Available at
http://java.sun.com/products/jdbc.
JDBC 2.0 Standard Extension API (JDBC extension specification). Copyright 1998,
1999, Sun Microsystems, Inc. Available at http://java.sun.com/products/
jdbc.
JDBC 1.22 API (JDBC 1.22). Copyright 1998, Sun Microsystems, Inc. Available at
http://java.sun.com/products/jdbc.
Java Transaction API, Version 1.0.1 (JTA Specification). Copyright 1998, 1999, Sun
Microsystems, Inc. Available at http://java.sun.com/products/jta.
Enterprise Java Beans Specification, Version 3.0 (EJB). Copyright 2006, Sun
Microsystems, Inc. Available at http://java.sun.com/products/ejb.
205
J2EE Connector Architecture Specification, Version 1.5 Copyright 2003, Sun
Microsystems, Inc. Available at http://java.sun.com/j2ee.