Loading Driver Class Automatically

This section provides a tutorial program to show how JDK 1.7 and 1.8 automatically loads JDBC driver classes from JAR files specified in the classpath.

If you are using newer versions of JDK, the JDBC driver will be loaded automatically from the JAR file provided in the classpath. Here is an example program that you can use to list all JDBC drivers (driver classes):

/* ListDrivers.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.sql.*;
import java.util.*;
public class ListDrivers {
  public static void main(String [] args) {
    try {
      printDrivers();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static void printDrivers() {
    Enumeration driverList = DriverManager.getDrivers();
    System.out.println("\nList of drivers:");
    while (driverList.hasMoreElements()) {
      Driver driverClass = (Driver) driverList.nextElement();
      System.out.println("   "+driverClass.getClass().getName());
    }
  }
}

Compile and run the above program with the JDBC Driver JAR file included in the current directory. By the way, the classpath delimiter is ":" on macOS and Linux systems. For Windows systems, you need to use ";".

herong> java -version
java version "17.0.1" 2021-10-19 LTS

herong> java -cp .;mssql-jdbc-9.4.1.jre16.jar ListDrivers.java
List of drivers:
   com.microsoft.sqlserver.jdbc.SQLServerDriver

Test results of older JDBC Driver versions are listed below:

Test 1 - Run the example program with JDK 1.8 and JDBC Driver 4.2 JAR file specified in the classpath:

herong> javac ListDrivers.java

herong> java ListDrivers -cp .;\local\lib\sqljdbc42.jar ListDrivers

List of drivers:
   com.microsoft.sqlserver.jdbc.SQLServerDriver

Test 2 - Run the example program with JDK 1.7 and JDBC Driver 4.2 JAR file specified in the classpath:

herong> javac ListDrivers.java

herong> java -cp .;\local\lib\sqljdbc42.jar ListDrivers

List of drivers:
   sun.jdbc.odbc.JdbcOdbcDriver

Test 3 - Run the example program with JDK 1.7 and JDBC Driver 4.1 JAR file specified in the classpath:

herong> javac ListDrivers.java

herong> java -cp .;\local\lib\sqljdbc41.jar ListDrivers

List of drivers:
   sun.jdbc.odbc.JdbcOdbcDriver
   com.microsoft.sqlserver.jdbc.SQLServerDriver

If you are confused with these tests, see my explanations below:

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) 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 Express Edition

Microsoft JDBC Driver for SQL Server

 Installing Microsoft JDBC Driver for SQL Server

Loading Driver Class Automatically

 Loading Driver Class with Class.forName()

 DriverManager.getConnection() and Connection URL

 Enable TCP/IP with SQL Server Configuration Manager

 Specifying Port Number in Connection URL

 Instance Name Better than Port Number

 Specifying Instance Name in Connection URL

 Closing the Database Connection - con.close()

 Specifying Database Name in Connection URL

 Incorrect Database Name in Connection URL

 Creating Connections with DataSource Class

 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

 Using Connection Pool with JDBC

 Archived Tutorials

 References

 Full Version in PDF/EPUB