This section describes how to load the Derby JDBC driver classes.
Derby JDBC driver has two driver classes for Java DB (Derby) running in two different modes:
1. Embedded Driver Class, org.apache.derby.jdbc.EmbeddedDriver - Used to create connections
to Java DB (Derby) running in embedded mode. The JAR file and the connection URL format for the embedded driver class are listed below:
Class Name: org.apache.derby.jdbc.EmbeddedDriver
JAR File: \local\javadb\lib\derby.jar
Connection URL: jdbc:derby:<database>
2. Client Driver Class, org.apache.derby.jdbc.ClientDriver - Used to create connections
to Java DB (Derby) running in Network Server mode. The JAR file and the connection URL format for the client driver class are listed below:
Class Name: org.apache.derby.jdbc.ClientDriver
JAR File: \local\javadb\lib\derbyclient.jar
Connection URL: jdbc:derby://<host>:<port>/<database>
Running under Java SE 6, you have 5 options to load Derby JDBC driver:
1. Do nothing. The DriverManager class will automatically load all driver classes that in JAR fiels in the classpath.
2. Call the forName() method like this: Class.forName("org.apache.derby.jdbc.ClientDriver"). This is the commonly used way of load a driver class.
3. Create a dummy driver object like this: "new org.apache.derby.jdbc.EmbeddedDriver()". This forces you to include the required JAR file at compilation time.
4. Retrieve the static class object from the driver class like this: "Class c = org.apache.derby.jdbc.EmbeddedDriver.class ".
This also forces you to include the required JAR file at compilation time.
5. Set the system property jdbc.drivers in the configuration file or in the command line option "-D".
For example: "java -Djdbc.drivers=org.apache.derby.jdbc.ClientDriver application".
Of course, I like to try option #1. Here is my testing Java program:
/**
* AutoLoadJdbcDriver.java
* Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
*/
import java.sql.*;
import java.util.*;
public class AutoLoadJdbcDriver {
public static void main(String [] args) {
Connection con = null;
try {
listDrivers();
// Find the driver for a given URL
Driver driverClass = (Driver) DriverManager.getDriver(
"jdbc:derby://somehost/somedb");
System.out.println("\nDriver for jdbc:derby://somehost/somedb");
System.out.println(" "+driverClass.getClass().getName());
listDrivers();
} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
}
}
private static void listDrivers() {
Enumeration driverList = DriverManager.getDrivers();
System.out.println("\nList of drivers:");
while (driverList.hasMoreElements()) {
Driver driverClass = (Driver) driverList.nextElement();
System.out.println(" "+driverClass.getClass().getName());
}
}
}
The output below confirms that the DriverManager class is able to load any JDBC driver classes
as long as their JAR files are included in the classpath.
C:\>javac AutoLoadJdbcDriver.java
C:\>java -cp .;\local\javadb\lib\derbyclient.jar AutoLoadJdbcDriver
List of drivers:
sun.jdbc.odbc.JdbcOdbcDriver
org.apache.derby.jdbc.ClientDriver
Driver for jdbc:derby://somehost/somedb
org.apache.derby.jdbc.ClientDriver
List of drivers:
sun.jdbc.odbc.JdbcOdbcDriver
org.apache.derby.jdbc.ClientDriver