JDBC Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.11

Loading JDBC Driver Class - mysql-connector-java-5.0.7-bin.jar

This section describes how to load the MySQL JDBC driver class - mysql-connector-java-5.0.7-bin.jar.

MySQL JDBC driver 5.0.7 is a JDBC 3.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.0.7-bin.jar file in the classpath when you execute this program:

/**
 * MySqlLoadDriver.java
 * Copyright (c) 2007 by Dr. Herong Yang. 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.0.7-bin.jar in the classpath:

C:\>javac MySqlLoadDriver.java

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

C:\>java -cp .;\local\lib\mysql-connector-java-5.0.7-bin.jar 
  MySqlLoadDriver

MySQL JDBC driver loaded ok.

Sections in This Chapter

MySQL Connector/J - Download and Installation

Loading JDBC Driver Class - mysql-connector-java-5.0.7-bin.jar

JDBC Driver Connection URL

Creating Connections with DataSource Class

Getting Driver and Server Information

Creating Tables with AUTO_INCREMENT Columns

"INSERT INTO" Statements

Dr. Herong Yang, updated in 2007
Loading JDBC Driver Class - mysql-connector-java-5.0.7-bin.jar