我有一個有兩個字符串列表的類。包含字符串列表的對象
public class School {
private List<String> students;
private List<String> teachers;
//getters and setters
}
我在我的DAO類中有以下方法,其中學校對象正在填充。
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;
}
現在,如果我這樣做,
School school = MyDAO.fetchPersonDeviceIdInfo("SJSU");
System.out.println(school);
請問學校有對象的SJSU所有的信息?我知道我可以在理想情況下自行嘗試並檢查,但我的應用程序非常複雜,並且需要一段時間才能將此代碼與我的應用程序集成。請建議。另外,我需要一個字符串嗎?
非常感謝!
COL_TEACHERS是第二列,所以你需要'teachers.add(rs.getString(2));'。 'school = new School()'應該在while循環之前,'school.setStudents()'和'school.setTeachers()'應該在while循環之後。在嘗試關閉它們之前,您可能還想檢查連接,語句,結果集對象是否爲空。 – Nishan
teachers.add(rs.getString(1));應該是teachers.add(rs.getString(2)); – Thihara
答案實際上取決於表結構。請發佈這些細節。 – Thihara