Home > Java > javaTutorial > How to Fix 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver' in JDBC?

How to Fix 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver' in JDBC?

Barbara Streisand
Release: 2024-12-06 02:09:17
Original
436 people have browsed it

How to Fix

JDBC Driver Error in Connecting to MySQL: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

In Java, the error message "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" indicates that the MySQL Connector/J library is not present or improperly configured. Here's what you can do to resolve the issue:

Solution 1: Include MySQL Connector/J Dependency

IntelliJ:

  1. Right-click on the project in the "Project Explorer."
  2. Hover over "Add" and select "Library."
  3. Choose "Java" and add the path to the mysql-connector-java.jar file.

Maven Project:

Add the following dependency to the pom.xml file:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>
Copy after login

Solution 2: Manual Jar File Addition

  1. Download the mysql-connector-java.jar file from the MySQL website.
  2. Right-click on the project in the "Project Explorer."
  3. Select "Build Path" > "Configure Build Path."
  4. Click the "Libraries" tab.
  5. Click "Add External JARs."
  6. Choose the downloaded mysql-connector-java.jar file.

Explanation:

The exception occurs because the Java Virtual Machine cannot find the com.mysql.jdbc.Driver class, which is part of the MySQL Connector/J library. By adding the dependency or manually including the JAR file, you provide the driver class and enable the application to establish a connection to the MySQL database.

The above is the detailed content of How to Fix 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver' in JDBC?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template