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!
teachers.add(rs.getString(2));.school = new School()should be before the while loop,andschool.setStudents()andschool.setTeachers()should be after the while loop. You might also want to check if your connection, statement, result set objects are null before trying to close them.