【已解决】数据库中有数据,但是JDBC查询出来的为null
当我遇到这个问题的时候,我的第一反应就是上CSDN找答案,可是大多数的博客都解释说可能是数据表的字段名和实体类的属性名不一致,例如:表字段名为user_id,那么实体类的属性名就应该是userId,即使用驼峰这种写法。但是我的表字段根本就不存在这样的问题,如下图:
字段名都不包含下划线这种结构,所以排除这种可能。那就纳了闷了?怎么查询就为空呢?这时我在翻笔记的时候,忽然注意到一张关于SQL数据类型与Java数据类型的转换这样一张表,它长这样:
Java类型 | SQL类型 |
---|---|
boolean | BIT |
byte | TINYINT |
short | SMALLINT |
int | INTEGER |
long | BIGINT |
String | CHAR、VARCHAR、LONGVARCHAR |
byte array | BINARY、VAR BINARY |
java.sql.Date | DATE |
java.sql.Time | TIME |
java.sql.Timestamp | TIMESTAMP |
那有没有可能是我在遍历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);
}
再来测试,就有数据了!记录一下,以免再此入坑。