weixin_43381244
2020-05-24 20:45
采纳率: 100%
浏览 1.2k
已采纳

包引用了,MySQL8.0.17版本,用的jar包是8.0.18的,但是一直报错

java.lang.ClassNotFoundException: com.mysql.cj.jdbc.driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at util.try1.main(try1.java:29)
数据库连接失败

代码
package util;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.cj.jdbc.Driver;

public class try1 {
private static String DBUrl="jdbc:mysql://localhost:3306/bishe?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false";
private static String DBUser="root";
private static String DBPwd="xsj123..0";
public static Connection conn=null;
public static ResultSet rs=null;
public static PreparedStatement pst=null;

public static void main(String[] args) {
    // TODO Auto-generated method stub



          try {
            String Driver = "com.mysql.cj.jdbc.driver";
            Class.forName(Driver);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }





          try {
            DriverManager.getConnection(DBUrl, DBUser, DBPwd);
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            System.out.println("数据库连接失败1");
        }
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 已采纳

    检查下是不是包引入的路径不对,或者是根本没引进来

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题