Wednesday, March 14, 2012

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 JDBC connection to mysql database first time. we have touched base on ClassNotFoundException in mysql on our earlier articles 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.Driver In order to connect to mysql database from 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. Java program dynamically load 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 classpath in order to successful connection to mysql database. you can downlad mysql-connector.jar from mysql.com.

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(DriverManager.java:602)


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

public class MySQLConnectionExample {

   
public static void main(String args[]) throws SQLException {
        Connection mysqlCon =
null;
       
try {
            Class.
forName("com.mysql.jdbc.Driver");
            mysqlCon = DriverManager.
getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
       
} catch (Exception e) {
            System.
out.println(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 of getting this Exception in Java JDBC code and now familiar with how to fix this.

Other Java Exception and troubleshooting tutorial you may like:


8 comments :

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: http://www.oracle.com/javaone/in-en/index.html?pcode=WWMK11024795MPP084&src=7268797&Act=155 ).

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(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)

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 mysql-connector-java-5.1.17.zip 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...

Anonymous said...

that was so useful thank you

nikhil kandekar 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

Post a Comment