JDBC Tutorials - Herong's Tutorial Examples - Version 3.03, by Dr. Herong Yang
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 JDK 1.7 or 1.8, 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) 2015, 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()); } } }
Test 1 - Run the example program with JDK 1.8 and JDBC Driver 4.2 JAR file specified in the classpath:
C:\herong>\Progra~1\java\jdk1.8.0_45\bin\javac ListDrivers.java C:\herong>\Progra~1\java\jdk1.8.0_45\bin\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:
C:\herong>\Progra~1\java\jdk1.7.0_45\bin\javac ListDrivers.java C:\herong>\Progra~1\java\jdk1.7.0_45\bin\java ListDrivers -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:
C:\herong>\Progra~1\java\jdk1.7.0_45\bin\javac ListDrivers.java C:\herong>\Progra~1\java\jdk1.7.0_45\bin\java ListDrivers -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:
Last update: 2015.
Table of Contents
JDBC (Java Database Connectivity) Introduction
Installing and Running Java DB - Derby
Derby (Java DB) JDBC DataSource Objects
Java DB (Derby) - DML Statements
Java DB (Derby) - ResultSet Objects of Queries
Java DB (Derby) - PreparedStatement
MySQL JDBC Driver (MySQL Connector/J)
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 - Reference Implementation of JdbcRowSet
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
Installing Microsoft JDBC Driver for SQL Server
►Loading Driver Class Automatically
Loading Driver Class with Class.forName()
DriverManager.getConnection() and Connection URL
Enabling 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