weixin_43562234
FUN_ADDER
采纳率33.3%
2019-07-07 15:22 阅读 3.5k

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条回答 默认 最新

  • hjs218 Json-Huang 2019-07-07 16:43

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

    点赞 3 评论 复制链接分享
  • qq_43201025 大叔家的宝贝 2019-07-07 15:46

    是不是没有导jar包啊

    点赞 2 评论 复制链接分享
  • weixin_39808420 世纪怪咖 2019-07-08 09:06

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

    点赞 评论 复制链接分享

相关推荐