"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
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:
Tidak ada komentar:
Posting Komentar