Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Tuesday, July 27, 2021

JDBC - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Solution

"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"  is a frequent Exception Java programmer face while writing JDBC connectivity code for MySQL, especially if you are trying to establish a JDBC connection to MySQL database first time. we have touched base on ClassNotFoundException in MySQL on our earlier articles on how to resolve ClassNotFoundException in Java and here we will see in detail what causes "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" and how to fix com.mysql.jdbc.Driver error in Java.

Cause of java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

How to fix java.lang.ClassNotFoundException: com.mysql.jdbc.DriverIn order to connect to the MySQL database from the Java program, we need an implementation of JDBC driver for MySQL database which is implemented as "com.mysql.jdbc.Driver" and comes with mysql-connector.jar

The Java program dynamically loads this JDBC driver when you run java program it looks for "com.mysql.jdbc.Driver" class in  Java Classpath, if JVM doesn't find this class after scanning classpath it throws "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver".

Possible Cause of "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"
here are some of the most possible reasons of "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" in your
code which is based on mistakes often observed:

1) You don't have mysql-connector.jar in your Classpath. as stated earlier this jar file contains "com.mysql.jdbc.Driver" class it must be present in the classpath in order to successfully connect to MySQL database. you can download mysql-connector.jar from

2) mysql-connector.jar is in your classpath but somehow your classpath is getting overridden.
Classpath is tricky in Java and classpath specified in jar may override CLASSPATH path variable. See how classpath works in Java to understand this issue in detail.

3) mysql-connector.jar is in classpath but current user doesn't have read permission.
This problem often happens in Unix or Linux operating system which has sophisticated file and directory permission based on user, group and owner level. just get the right permission and run your program again.


Example of java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

here is a real sample java code which will throw java.lang.ClassNotFoundException: com.mysql.jdbc.Driver when run
because it doesn't had mysql-connector.jar in its classpath. worth remembering is that  java.lang.ClassNotFoundException: com.mysql.jdbc.Driver comes when you use  Class.forName("com.mysql.jdbc.Driver") code to load JDBC driver, if you are using DriverManager.getConnection("com.mysql.jdbc.driver" ,"root", "root") than it will throw different exception like

Exception in thread "main" java.sql.SQLException: No suitable driver found for com.mysql.jdbc.Driver
        at java.sql.DriverManager.getConnection(

Here is a sample Java program that will throw java.lang.ClassNotFoundException: com.mysql.jdbc.Driver if you run this without having MySQL JDBC driver in your classpath.

public class MySQLConnectionExample {

public static void main(String args[]) throws SQLException {
        Connection mysqlCon =
try {
            mysqlCon = DriverManager.
getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
} catch (Exception e) {

Output: java.
lang.ClassNotFoundException: com.mysql.jdbc.Driver

That’s all on how to fix java.lang.ClassNotFoundException: com.mysql.jdbc.Driver, we have seen possible reasons for getting this Exception in Java JDBC code and now familiar with how to fix this.

Other Java Exception and troubleshooting tutorials you may like:


Rajivdh said...

Alert!: Oracle has announced their marquee event JavaOne and Oracle Develop 2012 being held in Hyderabad on 3-4 May. Registrations have also started (Link: ).

Rohini said...

Hi I am getting following SQLException while trying to connect to MySQL database running on my machine :

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
at java.sql.DriverManager.getConnection(
at java.sql.DriverManager.getConnection(

I have also attached mysql driver in classpath still I am getting this exception please help.

Rohini said...

I solve the problem actually "mysql-connector-java-5.1.17-bin.jar" was not on my classpath. I have only added in classpath. Instead of that I extracted zip file and added jar file "mysql-connector-java-5.1.17-bin.jar" into classpath and it worked.

gecmca said...

consider this scenario let say there are 5 apple,5 persons each person will eat one apple solve this problem usins program

siva said...

I have classes.jar file and also i have connecter, but i can't able to connect the database. I got the error like 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver'. I request you all please solve this problem...

Jackpot Money said...

Heloo sir,
iam Getting this eroor agai and again
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

i have set all class path correctly but still i getting this error on my web page please can you provide me exact snapshot of this class path so i can check it again.
is thaire any entry in path variable ?
thank you

javin paul said...

try putting MySQL JDBC driver on lib directory of tomcat or your web server

Anonymous said...

we are getting below error, do you know what causes it? we are trying to connect to SQL Server from Java

Caused by: java.sql.SQLException: Login failed for user 'test'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(
at net.sourceforge.jtds.jdbc.TdsCore.login(
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.(
at net.sourceforge.jtds.jdbc.ConnectionJDBC3.(
at net.sourceforge.jtds.jdbc.Driver.connect(
at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(
at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(
at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(

Post a Comment