Monday, December 31, 2007

Mustang(Java6) - Loading JDBC Driver

Mustang introduced service loader to load JDBC drivers by including a jar file in your classpath.
Before Mustang,
To get a JDBC connection using JDBC-ODBC driver you need to load the JDBC driver classes like below
class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
but this is not required from Mustang release.
This was changed in Java6 with java.util.ServiceLoader. As JDBC driver being packaged as a service, we just need to include the appropriate jar file in the class path.

Let's say if you want to use the Derby database. All you need to do is include the derby.jar file in your classpath, and the driver will automatically be available to us. There is no more need to use Class.forName(). Mustang ensures backward compatibility.So your older code still works.
The ServiceLoader mechanism can be used for any services, not just for your JDBC driver.


No comments: