I have imported my library "mysql-connector-java-5.1.39" as answered by most people to exact same question , but I am still getting this error
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
here is the code of test class
package database;
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
getConnection();
}
public static Connection getConnection() throws Exception {
try{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String username="root";
String password="root";
Class.forName(driver);
Connection conn= DriverManager.getConnection(url,username,password);
System.out.println("connected");
return conn;
}
catch (Exception e){
System.out.println(e);
}
return null;
}
}
Using mac OS .
Everybody repeat after me. ( :-) )
"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" is NOT a compilation error.
Therefore, changing the >>build<< path or adding an import
cannot fix the problem.
The solution is to make sure that the JAR file is on the classpath when you run the application. For example, if your test class is in bin\database\Main.class
and the driver JAR is in lib
...
$ java -classpath bin:lib/mysql-connector-java-5.1.39.jar database.Main
If the jar is already added to your external libs, you can simply add :
import com.mysql.jdbc.Driver;
and it shall work in your class.
This shall help further SO-21580499
Some of the most possible reasons of "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" in your code are:
It looks like you app cannot find the JDBC driver for Microsoft SQL server. You can download it below and add the jar to your WEB-INF/lib:
https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
Maybe you are missing any one of the following things : Both of the following things are necessary to remove the error :
Adding mysql-connector.jar file to your WebContent / WEB-INF / lib folder.
Adding mysql-connector.jar file in your classpath
Download mysql-connector.jar from https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.11
For solving first problem paste the above downloaded jar file into your project's WebContent / WEB-INF / lib folder.
For Solving second problem - right click on your project , go to properties then go to Java Build Path and then in Libraries section choose Add External JARs and select the above downloaded jar file.
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With