qq_36931352
2019-08-20 22:02
采纳率: 100%
浏览 474
已采纳

java中ResultSet.getDate()读取正常,但是放到list中却读取出null值?

这是Student类
package JDBCTest;

import java.util.Date;

public class Student {
private int id;
private String name;
private boolean gender;
private Date birthday;
public Student() {
}

public Student(int id, String name, boolean gender, Date birthday) {
    this.id = id;
    this.name = name;
    this.gender = gender;
    this.birthday = birthday;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public boolean isGender() {
    return gender;
}

public void setGender(boolean gender) {
    this.gender = gender;
}

public Date getBirthday(java.sql.Date birthday) {
    return this.birthday;
}

public void setBirthday(Date birthday) {
    this.birthday = birthday;
}

@Override
public String toString() {
    return "Student{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", gender=" + gender +
            ", birthday=" + birthday +
            '}';
}

}

package JDBCTest;

import java.sql.*;

public class Demo6 {
public static void main(String[] args) throws SQLException {
Student student = new Student();
Connection conn = JdbcUtil.getConnection();
PreparedStatement ps = conn.prepareStatement("select * from stu1 where id =?");
ps.setInt(1,2);
ResultSet rs = ps.executeQuery();
if (rs.next()){
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getBoolean("gender"));
student.getBirthday(rs.getDate("birthday"));
}
System.out.println(rs.getDate("birthday"));
JdbcUtil.close(conn, ps, rs);
System.out.println(student);
}
}
为什么这段代码中rs.getDate("birthday")读取是正常的,但是
student.getBirthday(rs.getDate("birthday"));读取出来的却是null值?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • JonathanYan 2019-08-20 22:16
    已采纳
    public Date getBirthday(java.sql.Date birthday) {
        return this.birthday;
    }
    

    你怕不是在逗我,你这里传入了一个Date,然后传出了类自己的Date,这两个变量根本没关系,返回的值是是类初始化的。

    if (rs.next()){
        student.setId(rs.getInt("id"));
        student.setName(rs.getString("name"));
        student.setGender(rs.getBoolean("gender"));
        student.getBirthday(rs.getDate("birthday"));
    }
    

    在这个里面第四句你应该是想setBirthday吧,从上面读下来也应该是set,不知道你这个什么意思。

    点赞 打赏 评论
  • BestwishesTolover 2019-08-21 13:56

    你都没set怎么get啊大哥

    点赞 打赏 评论

相关推荐 更多相似问题