The java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception occurs if the MySQL JDBC driver class can not found in the java class path. The Class.forName() fails to loads the mysql driver class that is not available in the class path. If the MySQL JDBC driver jar is not available, or if the jar is missing in the java class path, the exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver will be thrown.

The MySQL JDBC driver is used to connect a Java application to a MySQL database. The mysql driver sends the database query to the database from Java. The Mysql database executes the query and returns the data. The MySQL JDBC driver gathers data from the MySQL database and sends it back to the Java application. If the MySQL JDBC driver is not available, the exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver will be thrown by the java application.



Exception

The stack trace of the exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver will be as below. The driver class is not found in the java class path.

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at com.yawintutor.DBConnection.main(DBConnection.java:8)


How to reproduce this exception

If the MySQL JDBC driver jar is missing or not available in the java class path, the exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver will be thrown. The example below will show the exception.

package com.yawintutor;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {
	public static void main(String[] args) throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/test";
		String user = "root";
		String password = "root";

		Connection con = DriverManager.getConnection(url, user, password);
		if (con != null) {
			System.out.println("Database Connected successfully");
		} else {
			System.out.println("Database Connection failed");
		}
	}
}


Solution 1

The MySQL JDBC driver class is available in the mysql driver jar. download the mysql driver jar from this path https://dev.mysql.com/downloads/connector/j/. The name of mysql driver jar is same as mysql-connector-java-8.0.20.jar. Add the jar to the java class path.

download the jar mysql-connector-java-8.0.20.jar from https://dev.mysql.com/downloads/connector/j/ 
( 
  goto https://dev.mysql.com/downloads/connector/j/
  select "Select Operating System:" as your operating system and install
      or
  select "Select Operating System:" as "Platform Independent", download the zip and extract
)

Add in your java project.

In eclipse
Right click in mysql-connector-java-8.0.20.jar -> Build Path -> Add to Build Path


Solution 2

If the java project is a maven project, add the mysql connector dependency in the pom.xml. The dependency will download the mysql-connector-java-8.0.22.jar in the repository and link to the project. The latest mysql connector dependency can be found in https://mvnrepository.com/artifact/mysql/mysql-connector-java

pom.xml

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


Solution 3

If the java project is build using the Gradle, add the dependency as below. When the project is build, the MySQL JDBC driver will be added to the java project.

dependencies {
    compile 'mysql:mysql-connector-java:8.0.22'
}


Solution 4

If the java project is created with a basic editor and want to build and execute the java program, use the below command to compile and run.

javac -cp ./lib/mysql-connector-java-8.0.20.jar
java -cp ./lib/mysql-connector-java-8.0.20.jar 


Solution 5

If the application is created as a web application, The jar file must be placed in the web server class path. For example, in tomcat, you can place either tomcat lib director or application WEB-INF\lib directory.

<tomcat_install_dir>\lib\mysql-connector-java-8.0.20.jar
     or
<tomcat_install_dir>\webapps\<application>\WEB-INF\lib\mysql-connector-java-8.0.20.jar



Leave a Reply