Home  >  Article  >  Database  >  Why am I getting a \"ClassNotFoundException: com.mysql.jdbc.Driver\" error when connecting to my MySQL database using JDBC?

Why am I getting a \"ClassNotFoundException: com.mysql.jdbc.Driver\" error when connecting to my MySQL database using JDBC?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-02 20:17:30813browse

Why am I getting a

Java Runtime Error: ClassNotFoundException: com.mysql.jdbc.Driver

Problem Description:

A Java application using JDBC to connect to a MySQL database encounters a "ClassNotFoundException" when attempting to instantiate the MySQL JDBC driver.

Possible Causes:

  1. Missing JDBC Driver Library: The com.mysql.jdbc.Driver class is missing from the application's runtime classpath.

Solution:

  1. Add JDBC Driver to Classpath: Add the MySQL JDBC driver JAR file (e.g., mysql-connector-java-5.1.25-bin.jar) to the application's classpath. This can be done when running the application using the "-cp" parameter:

    • Windows: java -cp .;mysql-connector-java-5.1.25-bin.jar ClientBase
    • Linux/Mac: java -cp .:mysql-connector-java-5.1.25-bin.jar ClientBase

Example Code:

<code class="java">// import packages
import java.sql.*;

// create class ClientBase
public class ClientBase {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/CLIENTBASE";

    // Database credentials
    static final String USER = "root";
    static final String PASS = "";

    // Begin method main
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;

        try {
            // Register JDBC driver
            Class.forName(JDBC_DRIVER); // Add this line if the driver is not found

            // Open connection
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // Execute a query
            System.out.println("Creating statement...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, name, address, address 2, city, phone, state, zip, fax FROM CLIENTBASE";
            ResultSet rs = stmt.executeQuery(sql);

            // Extract data from result set
            while (rs.next()) {
                // Retrieve by column name
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String address = rs.getString("address");
                String address2 = rs.getString("address2");
                String city = rs.getString("city");
                String phone = rs.getString("phone");
                String state = rs.getString("state");
                String zip = rs.getString("zip");
                String fax = rs.getString("fax");

                // Display values
                System.out.print("ID: " + id);
                System.out.print(" Name: " + name);
                System.out.println("Address:" + address);
                System.out.println(address2);
                System.out.print("City:" + city);
                System.out.print(" State: " + state);
                System.out.println(" Zip: " + zip);
                System.out.print("Phone: " + phone);
                System.out.println(" Fax: " + fax);
            } // end while

            // clean up
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            // Handle errors for JDBC
            se.printStackTrace();
        } catch (Exception e) {
            // Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            // finally block used to close resources
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se) {
                se.printStackTrace();
            } // end finally
        } // end try
        System.out.println("Goodbye.");
    } // End method main
} // end class ClientBase</code>

The above is the detailed content of Why am I getting a \"ClassNotFoundException: com.mysql.jdbc.Driver\" error when connecting to my MySQL database using JDBC?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn