关于解决java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题

昨天写了一个Swing小程序,JDBC连接数据库,但是在运行的时候报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

检查了一下JDBC,并没有写错,于是就写了小demo如下:

package com.company;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class test2 {
    Connection connection;
    public Connection getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("数据库驱动家在成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false","root","root");
            System.out.println("连接成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  connection;

    }

    public static void main(String[] args) {
        test2 test2=new test2();
        test2.getConnection();
    }
}

运行之后报错如下:

"C:\Program Files\Java\jdk1.8.0_201\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.6\lib\idea_rt.jar=13257:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.6\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_201\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\rt.jar;E:\JavaData\test3\out\production\test3" com.company.test2
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at com.company.test2.getConnection(test2.java:13)
	at com.company.test2.main(test2.java:30)
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test?useSSL=false
	at java.sql.DriverManager.getConnection(DriverManager.java:689)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at com.company.test2.getConnection(test2.java:19)
	at com.company.test2.main(test2.java:30)

Process finished with exit code 0

纠结一番后,发现没有mysql-connector-java的jar包,于是在项目里创建一个lib目录,将mysql-connector-java-5.1.47-bin.jar放进去,就成功了。

还有一种方法,即在C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext目录下放入mysql-connector-java-5.1.47-bin.jar
这种方法是在我求救之后一位老哥告诉我的:
关于解决java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题_第1张图片
感谢这位老哥

个人比较推荐第一种方法,在实际项目开发中不会对其他项目造成影响。

你可能感兴趣的:(java)