Loading Derby JDBC Driver Classes

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 (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 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.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

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

Derby (Java DB) JDBC Driver

 Derby (Java DB) Driver Features

Loading Derby JDBC Driver Classes

 Creating Connections to Java DB (Derby) Network Server

 Java DB (Derby) Network Server and JDBC Driver Info

 Java DB (Derby) - Creating New Tables

 Java DB (Derby) - Inserting Data Rows to Existing Tables

 Java DB (Derby) - Running SELECT Queries

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 Summary of JDBC Drivers and Database Servers

 Using Connection Pool with JDBC

 Archived Tutorials

 References

 Full Version in PDF/EPUB