发生错误:java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

huangapple go评论71阅读模式
英文:

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

问题

以下是翻译好的内容:

我正在尝试在我的 Java 程序中建立 MySQL 和我的连接。

我的系统配置是:

  1. Windows 10
  2. 64 位
  3. Java 版本:1.8
  4. JAR 文件:mysql-connector-java-8.0.19

我的源代码:
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:8080/" + database;
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, userName, password);       
            System.out.println("成功连接到数据库");
        } catch (Exception e) {
            System.out.println("发生错误:" + e);
        }
        return conn;
    }
    public DatabaseConnectivity() {
        
    }
}

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("records", "root", "dics");
            System.out.println("成功连接到数据库");
        } catch (Exception e) {
            System.out.println("发生错误:" + e);
        }
    } // main 方法结束
} // Test 类结束

我已经设置了我的类路径

>set classpath
classpath="E:\softwares\java\jar files\mysql-connector-java-8.0.19.jar;"

我尝试运行我的代码

javac DatabaseConnectivity.java
javac -classpath e:\user\java\jdbc\ Test.java
java -classpath e:\user\java\jdbc\ test.Test

它抛出错误

发生错误:java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
成功连接到数据库
英文:

I trying make connection between Mysql and my java program

My system configuration is:

  1. Windows :10
  2. 64 bit
  3. java version: 1.8
  4. jar file: mysql-connector-java-8.0.19

My source code:
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:8080/"+database;
		Class.forName("com.mysql.cj.jdbc.Driver");
		Connection 	conn=DriverManager.getConnection(url,userName,password);		
		System.out.println("Connected to database successfully");
		}
		catch(Exception e)
		{
			System.out.println("Error occured"+e);
		}
		return conn;
	}
	public DatabaseConnectivity()
	{
		
	}

}

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("records","root","dics");
			System.out.println("Connected to database successfully");
		}
		catch(Exception e)
		{
			System.out.println(" Error occured"+e);
		}
	}//End of main method
}//End of class Test

I have set my classpath as

>set classpath
classpath="E:\softwares\java\jar files\mysql-connector-java-8.0.19.jar;";

I tried to run my code

javac DatabaseConnectivity.java
javac -classpath e:\user\java\jdbc\ Test.java
java -classpath e:\user\java\jdbc\ test.Test

It throws error

Error occuredjava.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
Connected to database successfully

答案1

得分: 0

有一些问题。

  1. 问题出在我通过环境变量设置classpath时。并且通过 -classpath 变量传递它会覆盖默认的classpath环境变量。

因此,在 java.exe 后面添加 jar 文件路径到 classpath 选项

  1. 不再在 mysql(mysql-connector-java-8.0.19) 中使用 Class.forName()。我不知道为什么?

源代码:

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. 第三个问题,我使用的是 mysql 8+,而我的 jar 文件版本是 5+。你的 mysql 版本和 jar 文件版本应该保持一致。现在,正如你在 classpath 中看到的,它是 8。

  2. 检查 mysql 的端口号

登录到 mysql -u root -p


mysql>show variables where variable_name="port";

它是 3306。正如你在我的问题中所看到的,我使用的是默认的端口号 8080。我在 mysql 安装时可能已经将其更改,因为其他一些应用程序已经在 8080 上运行(可能是 tomcat 或 wamp 服务器)。

执行命令

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

成功连接到数据库
表创建成功!

英文:

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!

huangapple
  • 本文由 发表于 2020年5月30日 17:13:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/62100315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定