【已解决】数据库中有数据,但是JDBC查询出来的为null

【已解决】数据库中有数据,但是JDBC查询出来的为null

​ 当我遇到这个问题的时候,我的第一反应就是上CSDN找答案,可是大多数的博客都解释说可能是数据表的字段名和实体类的属性名不一致,例如:表字段名为user_id,那么实体类的属性名就应该是userId,即使用驼峰这种写法。但是我的表字段根本就不存在这样的问题,如下图:

image-20220516185548029

字段名都不包含下划线这种结构,所以排除这种可能。那就纳了闷了?怎么查询就为空呢?这时我在翻笔记的时候,忽然注意到一张关于SQL数据类型与Java数据类型的转换这样一张表,它长这样:

Java类型SQL类型
booleanBIT
byteTINYINT
shortSMALLINT
intINTEGER
longBIGINT
StringCHAR、VARCHAR、LONGVARCHAR
byte arrayBINARY、VAR BINARY
java.sql.DateDATE
java.sql.TimeTIME
java.sql.TimestampTIMESTAMP

那有没有可能是我在遍历ResultSet集合的时候,没有注意到SQL类型和Java类型的转换呢?哇靠,还真是!

​ 我的实体类长这样:

public class smbmsUser {
    private Integer id; // id
    private String userCode; // 用户编码
    private String userName; // 用户名称
    private String userPassword; // 用户密码
    private Integer gender; // 性别
    private Date birthday; // 出生日期
    private String phone; // 电话
    private String address; // 地址
    private Integer userRole; // 用户角色
    private Integer createdBy; // 创建者
    private Date creationDate; // 创建时间
    private Integer modifyBy; // 更新者
    private Date modifyDate; // 更新时间
    private String idPicPath; // 证件照路径
    private String workPicPath; // 工作证照片路径


    public smbmsUser() {
    }
	
    public smbmsUser(Integer id, String userCode, String userName, String userPassword, Integer gender, Date birthday, String phone, String address, Integer userRole, Integer createdBy, Date creationDate, Integer modifyBy, Date modifyDate, String idPicPath, String workPicPath) {
        this.id = id;
        this.userCode = userCode;
        this.userName = userName;
        this.userPassword = userPassword;
        this.gender = gender;
        this.birthday = birthday;
        this.phone = phone;
        this.address = address;
        this.userRole = userRole;
        this.createdBy = createdBy;
        this.creationDate = creationDate;
        this.modifyBy = modifyBy;
        this.modifyDate = modifyDate;
        this.idPicPath = idPicPath;
        this.workPicPath = workPicPath;
    }




    @Override
    public String toString() {
        return "smbmsUser{" +
                "id=" + id +
                ", userCode='" + userCode + '\'' +
                ", userName='" + userName + '\'' +
                ", userPassword='" + userPassword + '\'' +
                ", gender=" + gender +
                ", birthday=" + birthday +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", userRole=" + userRole +
                ", createdBy=" + createdBy +
                ", creationDate=" + creationDate +
                ", modifyBy=" + modifyBy +
                ", modifyDate=" + modifyDate +
                ", idPicPath='" + idPicPath + '\'' +
                ", workPicPath='" + workPicPath + '\'' +
                '}';
    }
}

emmm,有点小长,再来看看遍历ResultSet集合时候使用的方法:

//遍历结果集
        while (rs.next()){
        
            String id = rs.getString(1);
            String userCode = rs.getString(2);
            String userName = rs.getString(3);
            String userPassword = rs.getString(4);
            String gender = rs.getString(5);
            String birthday = rs.getString(6);
            String phone = rs.getString(7);
            String address = rs.getString(8);
            String userRole = rs.getString(9);
            String createdBy = rs.getString(10);
            String creationDate = rs.getString(11);
            String modifyBy = rs.getString(12);
            String modifyDate = rs.getString(13);
            String idPicPath = rs.getString(14);
            String workPicPath = rs.getString(15);
                   
            smbmsUser smbmsUser = new smbmsUser(id, userCode, userName, userPassword, gender, birthday, phone, address, userRole, createdBy, creationDate, modifyBy, modifyDate, idPicPath, workPicPath);
            System.out.println(smbmsUser);

        }

当时为了快点,直接复制粘贴,方法都是清一色的getString(),好家伙,现在看我直接好家伙。上面构造器中的参数类型有Integer,Java.sql.Date,下面我就new对象就new了个寂寞,数据类型不一样,参数肯定传不进去啊!于是我把方法逐个修改过来,变成这样(和上面的属性对应即可):

 while (rs.next()){
            Integer id = rs.getInt(1);
            String userCode = rs.getString(2);
            String userName = rs.getString(3);
            String userPassword = rs.getString(4);
            Integer gender = rs.getInt(5);
            Date birthday = rs.getDate(6);
            String phone = rs.getString(7);
            String address = rs.getString(8);
            Integer userRole = rs.getInt(9);
            Integer createdBy = rs.getInt(10);
            Date creationDate = rs.getDate(11);
            Integer modifyBy = rs.getInt(12);
            Date modifyDate = rs.getDate(13);
            String idPicPath = rs.getString(14);
            String workPicPath = rs.getString(15);

           
            


            smbmsUser smbmsUser = new smbmsUser(id, userCode, userName, userPassword, gender, birthday, phone, address, userRole, createdBy, creationDate, modifyBy, modifyDate, idPicPath, workPicPath);
            System.out.println(smbmsUser);

        }

再来测试,就有数据了!记录一下,以免再此入坑。

image-20220516191521503

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Steph Wae

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值