JDBC驱动加载失败,异常:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

昨天写一个Swing小程序,用JDBC连接数据库,运行的时候报java.lang.ClassNotFoundException: com.mysql.jdbc.Driver这个异常。
于是写一个小demo测试一下:

package fly;

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","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=6199: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\SwingProgram\out\production\SwingProgram" fly.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 fly.test2.getConnection(test2.java:11)
    at fly.test2.main(test2.java:28)
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at fly.test2.getConnection(test2.java:17)
    at fly.test2.main(test2.java:28)

Process finished with exit code 0

真是心态爆炸,希望知道是什么问题的大佬指点一下迷津,感激不尽!!!!

3个回答

是不是没有导jar包啊

把mysql-connector-java-5.1.40-bin.jar放到 C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext  试试

weixin_43562234
FUN_ADDER 老哥可以说一下原因吗?
大约一年之前 回复
weixin_43562234
FUN_ADDER 谢谢指导,问题解决了
大约一年之前 回复

因为你没把这个包放到classpath下,引jar并不是把它复制过去就能用的

weixin_43562234
FUN_ADDER 嗯嗯,三Q
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问