Classnotfoundexception Com.Mysql.Jdbc.Driver

ClassNotFoundException com.mysql.jdbc.Driver

The most common cause is that you have some conflict in where your classes are loaded from. For example if you have 2 locations and one has JDBC drivers and the other one not then if your classloader loads from the 1st location and some class from the 1st location wants to use the driver - the driver is not there. So look for the duplicate JARs that are using your driver

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver comes up at Class.forName(com.mysql.jdbc.Driver).newInstance();

make sure you've mysql-connector.jar in your Classpath. "com.mysql.jdbc.Driver" must be present in the classpath in order to successfully connect to MySQL database.

you can download from :-
https://dev.mysql.com/downloads/connector/j/

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver even after using the mySql Maven Dependency on Intellij

You have to add your jdbc_connector to your tomcat server manually, you should to include your jar to this folder $TOMCAT_HOME/lib

How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

You need to download mysql connector jar file(driver implementation) and add it to your class path.

On the side note if your are using JDBC 4.0 (Java 7 and even 6 I think) then you need not use Class.forName("com.mysql.jdbc.Driver");. Simply add the jar to the class path. Driver implementation will automatically be searched and loaded from class path.

Error occuredjava.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

There were few problems.

  1. The problem was when I set classpath by environment variables. And pass -classpath variable it overrides the default classpath environment variable.

So add jar file path to classpath option passed with java.exe


  1. Class.forName() is not used anymore in mysql(mysql-connector-java-8.0.19). I
    don't know why?

Source code:

E:\user\java\jdbc\test\DatabaseConnectivity.java

package test;
import java.sql.*;
public class DatabaseConnectivity
{
Connection conn;
Connection getConnection(String database,String userName,String password)
{

try{
String url;
url="jdbc:mysql://localhost:3306/"+database;
//Class.forName("com.mysql.jdbc.Driver");
//Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection(url,userName,password);
System.out.println("Connected to database successfully");
}
catch(Exception e)
{
System.out.println("In Class DatabaseConnectivity Error occured"+e);
}
return conn;
}
public DatabaseConnectivity()
{

}
public static void main(String args[])
{

}

}

E:\user\java\jdbc\test\Test.java

package test;
import java.sql.*;
import test.*;
class Test
{
public static void main(String args[])
{
try
{
DatabaseConnectivity db=new DatabaseConnectivity();
Connection conn=db.getConnection("test","root","dics");
Statement st = conn.createStatement();
String table = "CREATE TABLE Employee11(Emp_code integer, Emp_name varchar(10))";
st.executeUpdate(table);
System.out.println("Table creation process successfully!");

}
catch(Exception e)
{
System.out.println(" class Test Error occured"+e);
}
}//End of main method
}//End of class Test

  1. Third problem, I was using mysql 8+ and my jar file version was 5+. Your mysql version and jar file version should be same. Now, It is 8 as you can see in the classpath.

  2. Check the port number of mysql

Login to mysql -u root -p


mysql>show variables where variable_name="port";

It was 3306. As you can see in my question I was usnig default port number which is 8080. I must have changed it while mysql installation because some other application was already running on 8080(probably tomcat or wamp server).

To execute

E:\user\java\jdbc\test>javac -classpath "e:\softwares\java\jar files\mysql-connector-java-8.0.19.jar";e:\user\java\jdbc\; DatabaseConnectivity.java

E:\user\java\jdbc\test>javac -classpath "e:\softwares\java\jar files\mysql-connector-java-8.0.19.jar";e:\user\java\jdbc\; Test.java

E:\user\java\jdbc\test>java -classpath "e:\softwares\java\jar files\mysql-connector-java-8.0.19.jar";e:\user\java\jdbc\; test.Test

Connected to database successfully
Table creation process successfully!



Related Topics



Leave a reply



Submit