MySQL(SELECT)在Java中检索null

时间:2016-03-31 16:22:23

标签: java mysql jdbc

检索文本框中的信息时没有任何反应:

Sql Class(StudentImpl):

public Student retrieve(int dbId) {

    Student student = new Student();
    try {
        String SQL = "SELECT * FROM student WHERE 1=1 and db_id = '" + dbId + "'";
        conn = DatabaseConnection.connectDatabase();
        ResultSet rs = stmt.executeQuery(SQL);
        while (rs.next()) {
            student.setDbId(rs.getInt("db_id"));
            student.setStudentNo(rs.getString("student_no"));
            student.setLname(rs.getString("lname"));
            student.setFname(rs.getString("fname"));
            student.setMname(rs.getString("mname"));
            student.setAddress(rs.getString("address"));
            student.setContactNo(rs.getString("contact_no"));
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return student;

}

在另一个类(JForm)中显示文本框:

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    StudentImpl service = new StudentImpl();
    Student student = new Student(); 
    int se = Integer.parseInt(searchText.getText());
    service.retrieve(se);
    txtStudentNo.setText(student.getStudentNo());
    txtLname.setText(student.getLname());
    fNameText.setText(student.getLname());
    mnameTxt.setText(student.getMname());
    addTxt.setText(student.getAddress());
    txtContactNo.setText(student.getContactNo());
}

1 个答案:

答案 0 :(得分:2)

您不使用方法retrieve的返回值:

Student student = new Student(); 
int se = Integer.parseInt(searchText.getText());
service.retrieve(se); 

因此改为写

int se = Integer.parseInt(searchText.getText());
Student student = service.retrieve(se);