我正在尝试打印列表的内容,但是我得到了错误的值。
(com.school.student.StudentModel@949774e
)代替查询结果
下面是bean
public class StudentModel {
int studentId;
String studentName;
public int getStudentId() {
return studentId;
}
public void setStudentId(int restaurantId) {
this.studentId = resturantId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
}
查询方法:
public static List<StudentModel> StudentDetails() {
Connection conn = connection.dbConnection();
List<StudentModel> students = new ArrayList<StudentModel>();
try {
String checkCst = "select * from student";
PreparedStatement stmt = conn.prepareStatement(checkCst);
ResultSet rCST = stmt.executeQuery();
while (rCST.next()) {
StudentModel rM = new StudentModel ();
rM.setStudentId(rCST.getInt(1));
rM.setStudentName(rCST.getString(2));
students.add(rM);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return students;
}
这就是我试图调用方法的方法:
StudentModel rM = new StudentModel();
List<StudentModel> student = ListStudentDetails.StudentDetails();
for (int i = 0; i < student.size(); i++) {
System.out.println(" " + student.get(i));
System.out.println(" " + student.get(i));
}
查询工作正常,所以我假设在调用StudentDetails
方法时for循环中出现问题。
非常感谢。
答案 0 :(得分:1)
你的if子句有问题,应该是一段时间。
while (rCST.next()) {
StudentModel rM = new StudentModel ();
rM.setStudentId(rCST.getInt(1));
rM.setStudentName(rCST.getString(2));
students.add(rM);
}
您可以尝试打印对象的参考值,例如学生的姓名
System.out.println(" " + student.get(i).getStudentName());
或者你可以
public class StudentModel {
int studentId;
String studentName;
public int getStudentId() {
return studentId;
}
public void setStudentId(int restaurantId) {
this.studentId = resturantId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@Override
public String toString(){
return this.studentName;
}
}