JDBC Tutorials - Herong's Tutorial Examples - v3.14, by Herong Yang
Loading Derby JDBC Driver Classes
This section describes how to load the Derby JDBC driver classes.
Derby JDBC driver has two driver classes for Derby (Java DB) running in two different modes:
1. Embedded Driver Class, org.apache.derby.jdbc.EmbeddedDriver - Used to create connections to Derby (Java DB) 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 (Windows): %DERBY_HOME%\lib\derby.jar JAR File (Linux and macOS): $DERBY_HOME/lib/derby.jar Connection URL: jdbc:derby:<database>
2. Client Driver Class, org.apache.derby.client.ClientAutoloadedDriver - Used to create connections to Derby (Java DB) 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.client.ClientAutoloadedDriver JAR File (Windows): %DERBY_HOME%\lib\derbyclient.jar JAR File (Linux and macOS): $DERBY_HOME/lib/derbyclient.jar
Since I am running Derby server in network mode, I need to include the JDBC driver JAR file, derbyclient.jar, in the -classpath (or -cp) option when compiling and running my Java programs. For example:
(on Windows) herong> javac -cp %DERBY_HOME%\lib\derbyclient.jar SomeClass.java herong> java -cp .;%DERBY_HOME%\lib\derbyclient.jar SomeClass (on Linux or macOS) herong> javac -cp $DERBY_HOME/lib/derbyclient.jar SomeClass.java herong> java -cp $DERBY_HOME/lib/derbyclient.jar SomeClass
After identified the Derby JDBC driver JAR file, I need to find a way to load it in my Java applications. There are 5 options to do this:
Of course, I like to try the "Do nothing" option. Here is my testing Java program:
/* AutoLoadJdbcDriver.java * Copyright (c) HerongYang.com. 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.
... (on Windows) herong> javac -cp %DERBY_HOME%\lib\derbyclient.jar AutoLoadJdbcDriver.java herong> java -cp .;%DERBY_HOME%\lib\derbyclient.jar AutoLoadJdbcDriver (on Linux or macOS) herong> javac -cp $DERBY_HOME/lib/derbyclient.jar AutoLoadJdbcDriver.java herong> java -cp .:$DERBY_HOME/lib/derbyclient.jar AutoLoadJdbcDriver List of drivers: org.apache.derby.client.ClientAutoloadedDriver Driver for jdbc:derby://somehost/somedb org.apache.derby.client.ClientAutoloadedDriver List of drivers: org.apache.derby.client.ClientAutoloadedDriver
Note that the Derby JDBC driver class name has been changed. In older Derby versions, it was called as, org.apache.derby.jdbc.ClientDriver. Here is the output of the same program running with Derby 10.11:
List of drivers: org.apache.derby.jdbc.ClientDriver Driver for jdbc:derby://somehost/somedb org.apache.derby.jdbc.ClientDriver List of drivers: org.apache.derby.jdbc.ClientDriver
Table of Contents
JDBC (Java Database Connectivity) Introduction
Installing and Running Derby (Java DB)
Derby (Java DB) Driver Features
►Loading Derby JDBC Driver Classes
Creating Connections to Derby (Java DB) Network Server
Derby (Java DB) Network Server and JDBC Driver Info
Derby (Java DB) - Creating New Tables
Derby (Java DB) - Inserting Data Rows to Existing Tables
Derby (Java DB) - Running SELECT Queries
Derby (Java DB) JDBC DataSource Objects
Derby (Java DB) - DML Statements
Derby (Java DB) - ResultSet Objects of Queries
Derby (Java DB) - PreparedStatement
Summary of JDBC Drivers and Database Servers