You need to enable JavaScript to run this app. 已添加MySQL Connector包仍出现JDBC Connection为null错误如何解决
优惠活动
大模型
产品
解决方案
定价
更多

已添加MySQL Connector包仍出现JDBC Connection为null错误如何解决

问题根源及修复方案

已明确的错误点

  • 驱动类名配置错误
    你代码中加载的驱动类com.sql.jdbc.Driver是无效值,正确配置如下:

    • MySQL 5.x及更低版本:使用com.mysql.jdbc.Driver
    • MySQL 8.x及更高版本:使用com.mysql.cj.jdbc.Driver
      该错误会直接导致驱动加载失败,连接创建逻辑抛出异常,最终CP.createc()返回null,触发空指针。
  • SQL语法错误
    插入语句insert into students(sname,sphone scity)values(?,?,?)中,sphone和scity字段之间缺少逗号,即使连接创建成功也会触发SQL语法报错。

  • 参数绑定顺序不匹配
    SQL字段顺序为sname、sphone、scity,你代码中第2个参数绑定了城市、第3个参数绑定了手机号,顺序完全错位,会触发数据类型不匹配报错。

  • 空值未做校验
    CP.createc()返回的连接未做非空判断,直接调用prepareStatement方法,连接创建失败时就会触发空指针异常。

其他需排查的潜在问题

  • 确认MySQL服务已正常启动,3306端口可正常访问
  • 确认student_manage数据库已创建,用户mysql的密码配置正确,且该用户持有student_manage库的读写权限
  • 确认导入的MySQL Connector Jar包版本和本地安装的MySQL大版本匹配,比如MySQL 8.x必须使用8.x版本的Connector包

修正后代码

数据库连接类CP

import java.sql.Connection;
import java.sql.DriverManager;

public class CP {
    public static Connection createc() {
        Connection con = null;
        try {
            // 请根据你的MySQL版本选择对应驱动类,注释掉另一个
            Class.forName("com.mysql.cj.jdbc.Driver"); // MySQL8.x用这行
            // Class.forName("com.mysql.jdbc.Driver"); // MySQL5.x用这行
            
            String user = "mysql";
            String password = "mysql";
            // MySQL8.x需要额外配置时区参数,5.x版本可去掉后面的时区参数
            String url = "jdbc:mysql://localhost:3306/student_manage?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8";
            con = DriverManager.getConnection(url, user, password);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return con;        
    }
}

数据操作类Studentdao

import java.sql.Connection;
import java.sql.PreparedStatement;

public class Studentdao {

    public static boolean insertStudenttoDB(Student st) {
        boolean f = false;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = CP.createc();
            // 连接非空判断
            if (con == null) {
                System.err.println("数据库连接创建失败");
                return false;
            }
            // 修正SQL语法
            String q = "insert into students(sname,sphone,scity) values(?,?,?)";
            pstmt = con.prepareStatement(q);
            // 修正参数绑定顺序
            pstmt.setString(1, st.getStudentname());
            pstmt.setLong(2, st.getStudentphone());
            pstmt.setString(3, st.getStudentcity());
            
            pstmt.executeUpdate();
            f = true;
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            // 新增资源释放逻辑,避免连接泄漏
            try {
                if (pstmt != null) pstmt.close();
                if (con != null) con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return f;
    }
}

内容的提问来源于stack exchange,提问作者HIMANSHU SRIVASTAVA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 17:36:10