Java Database Connectivity (JDBC), which has existed from the first public version of the core Java language, has evolved significantly over the last 10 years. In its current version, 4.0, which will be packaged with Java Standard Edition 6.0 (Java SE is Sun's new name for J2SE), it shows significant improvements in design and provides a richer API, with focus on ease of development and improvement in productivity.
This article discusses some of the important changes in the JDBC specification that either improve the design or facilitate better performance. The article does not enlist or survey every single change incorporated as a part of Java Specification Request 221, the JDBC 4.0 initiative.
After reading this article, you should be ready to leverage the new features in your next set of applications.
I assume you are already aware of annotations and generics, which were introduced in Java with J2SE 5.0. JDBC 4.0 introduces
annotations and the generic DataSet. This change aims to simplify execution of SQL queries (in scenarios that return a single result set) and SQL DML (data manipulation
language) statements (that return either a row count or nothing).
The new API defines a set of Query and DataSet interfaces. The Query interface defines a set of methods decorated with the JDBC annotations. These decorated methods describe the SQL select and
update statements, and specify how the result set should be bound to a DataSet. The DataSet interface is a parameterized type, as defined by generics. The DataSet interface provides a type-safe definition for the result set data.
All Query interfaces inherit from the BaseQuery interface. A concrete implementation of the interface can be instantiated using either the Connection.createQueryObject() or DataSource.createQueryObject() methods and passing a Query interface type as its parameter.
A DataSet interface inherits from java.util.List. A data class describing the columns of the result set data, returned by an annotated method of the Query interface, is its parameter type. A DataSet can be manipulated and operated upon both in a connected and disconnected mode. Thus, the DataSet is implemented either as a ResultSet or a CachedRowSet, depending on its operating mode: connected or disconnected. DataSet, being a sub-interface of the java.util.List, allows access of its data rows with the Iterator pattern, using the java.util.Iterator interface.
The data class or the user-defined class, which is a parameter type of the DataSet interface, can be specified in two ways: as a structure or as a JavaBeans object. Either method achieves the goal of binding
result set data columns to user-defined class definitions, but the JavaBeans component model is more elegant and facilitates
object definition reuse within other frameworks that support the JavaBeans model.
Listing 1 illustrates code snippets for a simple example to show how the new API is used to create and run SQL queries, define result set data using a user-defined class, and bind the returned result set to the user-defined specifications.
| Subject |
|
|
|
|
Time waste to read this articles.By Anonymous on June 20, 2009, 12:57 amFrom last 4 hours I am reading so many articles on JDBC4.0 all are copy paste and no one gives working examples. For me million dollar question is what happen to...
Reply | Read entire comment
batch SELECTs, batch UPDATEsBy Adam Wozniak on October 13, 2008, 4:21 pmHello There is at least one feature which I still waiting for in JDBC specification: * batch SELECTs * batch UPDATEs What I understand by “batch SELECTs”? I would...
Reply | Read entire comment
DataSet does not exist any longerBy Anonymous on October 6, 2008, 5:02 pmDoes anyone know what happened to javax.sql.DataSet and what replaces it for connecting JPA entities to DataModels?
Reply | Read entire comment
View all comments