This question already has answers here:
How do I print my Java object without getting “SomeType@2f92e0f4”?
(9个答案)
4年前关闭。
我正在尝试打印列表的内容,但是获取了错误的值。
(
下面是豆
查询方法:
这是我尝试调用该方法的方式:
该查询工作正常,因此我假设在调用
非常感谢你。
您可以尝试打印对象的参考值,例如学生的姓名
或者你可以
(9个答案)
4年前关闭。
我正在尝试打印列表的内容,但是获取了错误的值。
(
com.school.student.StudentModel@949774e
)而不是查询结果下面是豆
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循环中。非常感谢你。
最佳答案
您的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;
}
}