0

I am trying to print the contents of a list, but I am getting the wrong values.

(com.school.student.StudentModel@949774e) instead of the query result

Below is the 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;
    }
}

Query method:

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;
}

This is how I tried to call the method:

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));
}

The query works fine, so I am assuming the issue occurs in the for loop when I call the StudentDetails method.

Thank you very much.

mike
  • 3
  • 2

1 Answers1

1

There is issue in your if clause, it should be a while.

                while (rCST.next()) {
                    StudentModel  rM = new StudentModel ();
                    rM.setStudentId(rCST.getInt(1));
                    rM.setStudentName(rCST.getString(2));
                    students.add(rM);
                }

And you can try instead printing the reference value of the object, the name of the student for example

System.out.println(" " + student.get(i).getStudentName()); 

or you can

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;
    }

}
diyoda_
  • 5,274
  • 8
  • 57
  • 89
  • i have change it to while and i am still getting result like This question already has an answer here: How do I print my Java object without getting “SomeType@2f92e0f4”? 4 answers I am trying to print the contents of a list, but I am getting the wrong values. (com.school.student.StudentModel@949774e) instead of the query result – mike Feb 15 '16 at 22:14
  • @mike, when priting you should iterate your list like this: `for(StudentModel myStudent : student) {System.out.println(myStudent.getStudentId()); System.out.println(myStudent.getStudentName();}`. – António Ribeiro Feb 15 '16 at 22:17