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.

Table of Contents

 About This Book

JDBC (Java Database Connectivity) Introduction

 What Is JDBC?

 JDBC Version and History

 JDBC Driver Types

 Establishing Connections from JDBC to Databases

DriverManager - Loading JDBC Driver

 DriverManager - Connection URL

 Downloading and Installing JDK - Java SE

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 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
DriverManager - Loading JDBC Driver