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:

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) 2015, HerongYang.com, 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() with JDK 1.8 by follow the commands below:

C:\>\Progra~1\java\jdk1.8.0_45\bin\javac LoadJdbcDriver.java

C:\>\Progra~1\java\jdk1.8.0_45\bin\java 
   -cp .;\local\lib\sqljdbc.jar LoadJdbcDriver

Before loading SQLServerDriver:
   com.microsoft.sqlserver.jdbc.SQLServerDriver
After loading SQLServerDriver:
   com.microsoft.sqlserver.jdbc.SQLServerDriver

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

C:\>\Progra~1\java\jdk1.8.0_45\bin\java 
   -cp .;\local\lib\sqljdbc.jar
   -Djdbc.drivers="com.microsoft.sqlserver.jdbc.SQLServerDriver"
   LoadJdbcDriver
   
Before loading SQLServerDriver:
   com.microsoft.sqlserver.jdbc.SQLServerDriver
After loading SQLServerDriver:
   com.microsoft.sqlserver.jdbc.SQLServerDriver

What I learned from the outputs of the two tests:

If we compare the output of the same tests with JDK 1.6 as presented below, you will see that JDK 1.8 has changed when JDBC drivers are loaded.

Test 1: Load Microsoft JDBC Driver 1.0 with Class.forName() with JDK 1.6 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 with JDK 1.6 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 with JDK 1.6:

Last update: 2015.

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

 JDK (Java SE) Installation

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

 Outdated Tutorials

 References

 PDF Printing Version