Reputation: 2716
I have a class that has two lists of Strings.
public class School {
private List<String> students;
private List<String> teachers;
//getters and setters
}
I have the below method in my DAO class where the school object is getting populated.
public static School fetchSchoolInfo(String schoolName) throws SQLException
{
School school = null;
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
List<String> students = new ArrayList<String>();
List<String> teachers = new ArrayList<String>();
try {
connection = createConnection();
statement = connection.prepareStatement("SELECT COL_STUDENT,COL_TEACHERS FROM SCHOOL WHERE SCHOOLNAME=?");
statement.setString(1, schoolName);
rs = statement.executeQuery();
while(rs.next())
{
school = new School();
students.add(rs.getString(1));
teachers.add(rs.getString(1));
school.setStudents(students);
school.setTeachers(teachers);
}
} finally{
rs.close();
statement.close();
connection.close();
}
return school;
}
Now, if I do this,
School school = MyDAO.fetchPersonDeviceIdInfo("SJSU");
System.out.println(school);
Will the school object have all the info of SJSU? I know I can ideally try this on my own and check, but my application is very complex, and it will take a while to integrate this code with my application. Please suggest. Also, do I need a to string?
Many thanks!
Upvotes: 0
Views: 118
Reputation: 26094
Override your School.toString()
method like this
class School {
private List<String> students;
private List<String> teachers;
@Override
public String toString() {
return "School [students=" + students + ", teachers=" + teachers + "]";
}
}
Then you try
School school = MyDAO.fetchPersonDeviceIdInfo("SJSU");
System.out.println(school);
Upvotes: 1
Reputation: 7259
change the following :
while(rs.next())
{
school = new School();
students.add(rs.getString(1));
teachers.add(rs.getString(1));
school.setStudents(students);
school.setTeachers(teachers);
}
to
school = new School();
while(rs.next())
{
students.add(rs.getString(1));
teachers.add(rs.getString(2)); // changed
}
school.setStudents(students);
school.setTeachers(teachers);
Upvotes: 5