Home >Java >javaTutorial >How to Resolve ClassNotFoundException: com.mysql.jdbc.Driver?

How to Resolve ClassNotFoundException: com.mysql.jdbc.Driver?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-03 13:26:10493browse

How to Resolve ClassNotFoundException: com.mysql.jdbc.Driver?

ClassNotFoundException: com.mysql.jdbc.Driver with MySQLConnector/J

Cause

This error typically occurs when the MySQL Connector/J library is not included in the project's classpath. This library contains the com.mysql.jdbc.Driver class, which is used to establish a connection to a MySQL database.

Solutions

Maven Projects

Add the following dependency to your pom.xml file:

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

Refer to [here](https://mvnrepository.com/artifact/mysql/mysql-connector-java) for available versions.

Non-Maven Projects

Manually add the MySQL Connector/J JAR file to the project's build path:

  • Right-click the project -> Build Path -> Configure Build Path...
  • In the Libraries tab, click Add External JARs...
  • Select the MySQL Connector/J JAR file from your system
  • Download the JAR file from [here](https://dev.mysql.com/downloads/connector/j/).

Explanation

When building the project, Java requires the com.mysql.jdbc.Driver class from the MySQL Connector/J library. By adding the library to the classpath, Java can locate the class and establish the connection to the MySQL database.

The above is the detailed content of How to Resolve ClassNotFoundException: com.mysql.jdbc.Driver?. 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