Loading JDBC Driver Class - com.mysql.cj.jdbc.Driver

Describes how to load the MySQL JDBC driver class, com.mysql.cj.jdbc.Driver, from mysql-connector-java-8.0.17.jar.

By reading the MySQL Connector/J 8.0.17 reference guide, I see that the standard JDBC driver interface is implemented with a class called com.mysql.cj.jdbc.Driver. So I wrote the following Java program to load com.mysql.cj.jdbc.Driver class using the Class.forName() method:

/* MySqlLoadDriver2.java
 * Copyright (c) 2005 HerongYang.com. All Rights Reserved.
 */
import java.sql.*;
public class MySqlLoadDriver2 {
  public static void main(String [] args) {
    Connection con = null;
    try {

      // Load the MySQL JDBC driver
      Class.forName("com.mysql.cj.jdbc.Driver");
      System.out.println("MySQL JDBC driver loaded ok.");

    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
    }
  }
}

The compilation and execution tests were recorded below. Notice the exception error I got without mysql-connector-java-8.0.17.jar in the classpath:

herong> java -version
java version "12.0.1" 2019-04-16
Java(TM) SE Runtime Environment (build 12.0.1+12)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

herong> javac MySqlLoadDriver2.java

herong> java MySqlLoadDriver2
Exception: com.mysql.jdbc.Driver

herong> java -cp .;\local\lib\mysql-connector-java-8.0.17.jar
   MySqlLoadDriver2

MySQL JDBC driver loaded ok.

By the way, in previous MySQL Connector/J releases, like mysql-connector-java-5.1.38-bin.jar, the standard JDBC driver interface was implemented with a class called com.mysql.jdbc.Driver.

Here is my old test program to load the com.mysql.jdbc.Driver:

/* MySqlLoadDriver.java
 * Copyright (c) 2005 HerongYang.com. All Rights Reserved.
 */
import java.sql.*;
public class MySqlLoadDriver {
  public static void main(String [] args) {
    Connection con = null;
    try {

      // Load the MySQL JDBC driver
      Class.forName("com.mysql.jdbc.Driver");
      System.out.println("MySQL JDBC driver loaded ok.");

    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
    }
  }
}

Test result of MySqlLoadDriver.java with JDK 8 and MySQL Server 5.7.10.0 is listed below as a reference:

herong> java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode, sharing)

herong> javac MySqlLoadDriver.java

herong> java MySqlLoadDriver
Exception: com.mysql.jdbc.Driver

herong> java -cp .;\local\lib\mysql-connector-java-5.1.38-bin.jar
   MySqlLoadDriver

MySQL JDBC driver loaded ok.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 PHP Programs and MySQL Server

 Perl Programs and MySQL Servers

Java Programs and MySQL Servers

 MySQL Connector/J - Download and Installation

Loading JDBC Driver Class - com.mysql.cj.jdbc.Driver

 JDBC Driver Connection URL

 Connection URL Tests on Older MySQL Connector/J

 Creating Connections with DataSource Class

 Getting Driver and Server Information

 Creating Tables with AUTO_INCREMENT Columns

 "INSERT INTO" Statements

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Window Functions for Statistical Analysis

 Use Index for Better Performance

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 System, User-Defined and Stored Procedure Variables

 MySQL Server Administration

 Storage Engines in MySQL Server

 InnoDB Storage Engine - Primary and Secondary Indexes

 Performance Tuning and Optimization

 Bulk Changes on Large Tables

 MySQL Server on macOS

 Installing MySQL Server on Linux

 Connection, Performance and Second Instance on Linux

 Archived Tutorials

 References

 Full Version in PDF/EPUB