JDBC for Oracle - Herong's Tutorial Examples - v3.13, by Herong Yang
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 as described in another tutorial in this book.
/* LoadJdbcDriver.java * Copyright (c) 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 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 with Class.forName() with newer versions of JDK by follow the commands below:
herong> java -version java version "17.0.1" 2021-10-19 LTS herong> java -cp mssql-jdbc.jar LoadJdbcDriver.java Before loading SQLServerDriver: com.microsoft.sqlserver.jdbc.SQLServerDriver After loading SQLServerDriver: com.microsoft.sqlserver.jdbc.SQLServerDriver
Test 2 - Load Microsoft JDBC Driver with jdbc.drivers property with JDK by follow the commands below. The backslash at the end of line indicates that the command continues on the next line.
herong> java -cp mssql-jdbc.jar \ -Djdbc.drivers="com.microsoft.sqlserver.jdbc.SQLServerDriver" \ LoadJdbcDriver.java 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 newer versions of JDK have changed on how JDBC drivers are loaded.
Test 3 - Load Microsoft JDBC Driver with Class.forName() with JDK 1.6 by follow the commands below:
herong> java -version java version "1.6.0_02" Java(TM) SE Runtime Environment (build 1.6.0_02-b06) Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing) herong> javac LoadJdbcDriver.java herong> java -cp sqljdbc.jar LoadJdbcDriver Before loading SQLServerDriver: sun.jdbc.odbc.JdbcOdbcDriver After loading SQLServerDriver: sun.jdbc.odbc.JdbcOdbcDriver com.microsoft.sqlserver.jdbc.SQLServerDriver
Test 4 - Load Microsoft JDBC Driver with jdbc.drivers property with JDK 1.6 by follow the commands below:
herong> java -cp 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:
Table of Contents
►JDBC (Java Database Connectivity) Introduction
Establishing Connections from JDBC to Databases
►DriverManager - Loading JDBC Driver
DriverManager - Connection URL
Oracle Express Edition Installation on Windows
Oracle - Reference Implementation of JdbcRowSet
Oracle - JBDC CallableStatement
Oracle CLOB (Character Large Object) - TEXT
Oracle BLOB (Binary Large Object) - BLOB