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

This section describes how to load the MySQL JDBC driver class, com.mysql.jdbc.Driver, from mysql-connector-java-5.0.7-bin.jar.

MySQL JDBC driver 5.1.38 is a JDBC 4.0 driver. The driver class, com.mysql.jdbc.Driver, needs to be loaded, if you want to use the DriverManager class to create Connection objects. The simplest way to load a driver class is to call the Class.forName() method as shown in the following sample program. Of course, you have to include the mysql-connector-java-5.1.38-bin.jar file in the classpath when you execute this program:

 * MySqlLoadDriver.java
 * Copyright (c) 2015, 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());

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

C:\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)

C:\herong>javac MySqlLoad

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

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

MySQL JDBC driver loaded ok.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 Perl Programs and MySQL Servers

 PHP Programs and MySQL Servers

Java Programs and MySQL Servers

 MySQL Connector/J - Download and Installation

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

 JDBC Driver Connection URL

 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

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 Outdated Tutorials


 PDF Printing Version