In this section, you will learn about the new features in JDBC 4.0.In this section, you will learn about the new features in JDBC 4.0.
Java database connectivity (JDBC) is the Java Soft
specification of the API. It allows Application Programs to interact with the Database to access the Relational Data. Typically the JDBC API consists of a set of interfaces and classes written in the Java programming language. The applications can be written to connect to databases using these standard interfaces and classes and also to process the results by sending queries written in SQL. Furthermore the back-end Database session can be connected by JDBC API which can execute the Queries to get the Results.
Some of the new set of features which come along with Mustang is JDBC 4.0 are:
No need for Class.forName("DriverName")
Changes in Connection and Statement Interface
Using ResultSet Becomes More Flexible
More APIs Become Available
The three most important features of Driver and Connection Management in JDBC are described below with some new features and enhancements.
1. Getting Connected Becomes Easier
This is one of the most important feature in JDBC that has relieved the developers from loading the driver explicitly. Exactly, you got it right, no need to explicitly load the driver by calling Class.forName anymore. So from now on don't bother to load the driver because the DriverManager will automatically load the driver found in the application CLASSPATH while your application tries to connect the database for the first time. However it is still preferred to create an appropriate DataSource object to retrieve a connection. Since the properties of data source instance can be modified which allows portability and transparency, there is no need to modify any application code to connect to a different database instance. Hence to retrieve a connection there is no need to change any application code to connect to a different database instance that requires loading a different driver.
For example: Earlier the programmers used to
manually load the drivers before making any Database Calls. The code which was
used by the programmers is:
The disadvantage is that the code will be changed if the Database Driver vendor wants to change the class name. Now this problem doesn't persist anymore. With the help of Service Provider Mechanism available from Java 5, the Driver will be Loaded Automatically by the JVM provided that the Jar files corresponding to the Driver Class are in the appropriate class path.
To understand this mechanism consider two entities, one is the service which contains a set of interfaces and the other entity is the provider who gives a well-defined Implementation for the Service. Now to maintain a directory structure, this service is packaged as a Jar by the provider. The structure of a directory is:
After maintaining the directory structure, if the JDBC Driver Provider wants to make his implementation as a Service then he has to maintain the file name along with the directory structure as,
Rememeber the file should only contain one entry that is the Name of the Provider Class. The function of this entry is to implement the Driver. Also the name of the file is java.sql.Driver in the above code. For instance the content of the file in My SQL would be,
File - Java.sql.Driver
com.mysql.jdbc.Driver # Class name of the Driver
Hence any Java program will automatically load the JDBC driver by the procedure. Moreover if the application class-path contains multiple drivers then the preference will be given to the first matching driver only ignoring the rest of the drivers.
2. Using ResultSet Becomes More Flexible
Several important features of ResultSet interfaces hierarchy are described below:
The ResultSet provided by the RowSet sub-interface is scrollable, updateable, and offline-editable . The JDBC programming has become much more easier and flexible due to the current version's support for the SQLXML data type along with these features.
Another feature of the ResultSet is the WebRowSet sub-interface. This interface provides the ability to read data from database tables. Moreover it is used to serialize the data to an XML document, and deserialize from XML back to result set.