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

DriverManager - Loading JDBC Driver

This section describes how to load a JDBC driver and register it with DriverManager.

If you want to use DriverManager class to create a connection to a database server, you need to load a JDBC driver that knows how to create a connection to that database server. The loaded JDBC driver class will be automatically registered to DriverManager.

There are two ways to load a JDBC driver:

  • Using the Class.forName() method - Loading the specified driver class when you need it.
  • Using the java.lang.System property jdbc.drivers setting - Loading the specified driver classes when the first call to a DriverManager method is made.

I wrote the following program to test both ways of loading JDBC drivers. To test this program, you need to download Microsoft JDBC Driver 1.0 as described in another tutorial in this book.

/**
 * LoadJdbcDriver.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
import java.util.*;
public class LoadJdbcDriver {
  public static void main(String [] args) {
    Connection con = null;
    try {
      System.out.println("Before loading SQLServerDriver:");
      listDrivers();

// Load Microsoft JDBC Driver 1.0
      Class.forName(
        "com.microsoft.sqlserver.jdbc.SQLServerDriver");
 
      System.out.println("After loading SQLServerDriver:");
      listDrivers();
    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
    }
  }
  private static void listDrivers() {
    Enumeration driverList = DriverManager.getDrivers();
    while (driverList.hasMoreElements()) {
      Driver driverClass = (Driver) driverList.nextElement();
      System.out.println("   "+driverClass.getClass().getName());
    }
  }
}

Test 1: Load Microsoft JDBC Driver 1.0 with Class.forName() by follow the commands below:

C:\>javac LoadJdbcDriver.java

C:\>java -cp .;\local\lib\sqljdbc.jar LoadJdbcDriver
Before loading SQLServerDriver:
   sun.jdbc.odbc.JdbcOdbcDriver
After loading SQLServerDriver:
   sun.jdbc.odbc.JdbcOdbcDriver
   com.microsoft.sqlserver.jdbc.SQLServerDriver

Test 2: Load Microsoft JDBC Driver 1.0 with jdbc.drivers property by follow the commands below:

C:\>javac LoadJdbcDriver.java

C:\>java -cp .;\local\lib\sqljdbc.jar 
   -Djdbc.drivers="com.microsoft.sqlserver.jdbc.SQLServerDriver"
   LoadJdbcDriver

Before loading SQLServerDriver:
   sun.jdbc.odbc.JdbcOdbcDriver
   com.microsoft.sqlserver.jdbc.SQLServerDriver
After loading SQLServerDriver:
   sun.jdbc.odbc.JdbcOdbcDriver
   com.microsoft.sqlserver.jdbc.SQLServerDriver

What I learned from the outputs of the two tests:

  • sun.jdbc.odbc.JdbcOdbcDriver, the JDBC-ODBC Bridge driver is loaded automatically by the JVM, because it showed up in the driver list automatically.
  • The loaded driver class is automatically registered to the DriverManager class, because I didn't call any DriverManager method to register any class.
  • The "-Djdbc.drivers=*" option worked correctly to bring driver classes to the DriverManager class, because the Microsoft class specified in the option showed in the driver list before the call of the forName() method.

Sections in This Chapter

What Is JDBC?

JDBC Version and History

JDBC Driver Types

Establishing Connections from JDBC to Databases

DriverManager - Loading JDBC Driver

DriverManager - Connection URL

Dr. Herong Yang, updated in 2007
DriverManager - Loading JDBC Driver