JDBC 4.0
Java Database Connectivity 4.0
Java SE 6 includes several enhancements to the Java Database Connectivity (JDBC) API. These enhancements will be released as JDBC version 4.0. The main objectives of the new JDBC features are to provide a simpler design and better developer experience. This article provides an overview of the JDBC 4.0 enhancements and what benefits they offer to enterprise Java developers. JDBC 4.0 adds some functionality to the core API.
Java developers no longer need to explicitly load JDBC drivers using code like Class.forName() to register a JDBC driver. The DriverManager class takes care of this by automatically locating a suitable driver when the DriverManager.getConnection() method is called. This feature is backward-compatible, so no changes are needed to the existing JDBC code.
Major features of JDBC 4.0:
. Auto-loading of JDBC driver class
. Connection management enhancements
. Support for RowId SQL type
. Scalar Function Support
. DataSet implementation of SQL using Annotations
. SQL exception handling enhancements
. SQL XML support
. Data Type Support
. Support for National Character Set Conversion
There are also other features such as improved support for large objects (BLOB/CLOB) and National Character Set Support.
DataSources. To support the JDBC 4.0 ease of development, Derby introduces new implementations of javax.sql.DataSource.
Autoloading of JDBC drivers. In earlier versions of JDBC, applications had to manually register drivers before requesting Connections. With JDBC 4.0, applications no longer need to issue a Class.forName() on the driver name; instead, the DriverManager will find an appropriate JDBC driver when the application requests a Connection.
SQLExceptions. JDBC 4.0 introduces refined subclasses of SQLException. See Refined subclasses of SQLException.
Wrappers. JDBC 4.0 introduces the concept of wrapped JDBC objects. This is a formal mechanism by which application servers can look for vendor-specific extensions inside standard JDBC objects like Connections, Statements, and ResultSets.
Statement events. With JDBC 4.0, Connection pools can listen for Statement closing and Statement error events. New methods were added to javax.sql.PooledConnection: addStatementEventListener and removeStatementEventListener.
Streaming APIs. JDBC 4.0 adds new overloads of the streaming methods in CallableStatement, PreparedStatement, and ResultSet. These are the setXXX and updateXXX methods which take java.io.InputStream and java.io.Reader arguments. The new overloads allow you to omit the length arguments or to specify long lengths.
New methods. New methods were added to the following interfaces: java.sql.Connection, java.sql.DatabaseMetaData, java.sql.Statement.
Sample Code:
public class Example1 {
public static void main(String[] args) {
...
String dbName = "example1";
String tableName = "stu1";
ds = new EmbeddedDataSource40();
ds.setDatabaseName(dbName);
String connectionURL = "jdbc:derby:"+dbName+";create=true";
try {
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("select * from "+tableName);
int colCount= rs.getMetaData().getColumnCount();
for (int j=0; j< i =" 0;">Output:ID NAME COURSEExample2
1001 John Doe Statistics
1002 Jack McDonalds Linear Algebra
Java class that handles chained exceptionspublic class Example2 {I ran the class in Example2.java, specifying student4 as a table name that does not exist in the database. It raised a chained exception in following call.
public static void main(String[] args) {
String dbName = "example";
String tableName = "student4";
try {
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("select * from " + tableName);
} catch (SQLException sx) {
for(Throwable e : sx ) {
System.err.println("Error encountered: " + e);
}
}
finally{
//close connections
}
}
}
rs = stmt.executeQuery("select * from " + tableName);
In a real application, you need to catch such chained exceptions, inspect them, and take a suitable action. In this example, however, I will show how to print them to the error console. Here is how to do just that.
for(Throwable e : sx ) {
System.err.println("Error encountered: " + e);
}
Output running the class Example2:Error encountered: java.sql.SQLSyntaxErrorException:
Table/View 'STUDENT4' does not exist.
Error encountered: java.sql.SQLException:
Table/View 'STUDENT4' does not exist.
Exception in thread "main" java.lang.NullPointerException
at ex.Examlpe2.main(Examlpe2.java:51)
0 comments:
Post a Comment