JDBC Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.11

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: \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

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 Downloading and Installing JDK - Java SE

 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

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

 Oracle - Reference Implementation of JdbcRowSet

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Microsoft SQL Server 2005 Express Edition

 Microsoft JDBC Driver for SQL Server - sqljdbc.jar

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

 Additional Tutorial Notes to Be Added

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2007
Loading Derby JDBC Driver Classes