I have an sql database where I am attempting to load data from. RstImport Student is importing from the student table which contains firstName, middleName, lastName, phoneNumber and dob. RstImportModule is then importing from the moduleslist table which contains 6 modules and grades. The imported data from each is then used to create a new student object, with the modules and grades being added to an array in this object. The student object should then be added to the listOfStudents arraylist. I am receiving the error 'Operation not allowed after ResultSet closed'. When debugging eclipse is telling me there is an error occurring at the start of the second while loops. Why am I receiving this error?
public class LoadData {
public static ArrayList<Student> loadStudentData() throws ClassNotFoundException, SQLException {
Statement stm = null;
System.out.println("Connecting to Database");
System.out.println("**********************");
Connection myConn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/schoolstudentsystem", "root", "");
stm = myConn.createStatement();
String sqlImportStudent = "Select * From students";
String sqlImportModule = "Select * From moduleslist";
ResultSet rstImportStudent = stm.executeQuery(sqlImportStudent);
ResultSet rstImportModule = stm.executeQuery(sqlImportModule);
ModuleGrade[] moduleGradeArray = new ModuleGrade[6];
while (rstImportModule.next()) {
for(int i = 0; i < Student.modulesList.length; i++) {
ModuleGrade mg1 = new ModuleGrade(rstImportModule.getString("m1"), rstImportModule.getInt("mg1"));
ModuleGrade mg2 = new ModuleGrade(rstImportModule.getString("m2"), rstImportModule.getInt("mg2"));
ModuleGrade mg3 = new ModuleGrade(rstImportModule.getString("m3"), rstImportModule.getInt("mg3"));
ModuleGrade mg4 = new ModuleGrade(rstImportModule.getString("m4"), rstImportModule.getInt("mg4"));
ModuleGrade mg5 = new ModuleGrade(rstImportModule.getString("m5"), rstImportModule.getInt("mg5"));
ModuleGrade mg6 = new ModuleGrade(rstImportModule.getString("m6"), rstImportModule.getInt("mg6"));
moduleGradeArray[0] = mg1;
moduleGradeArray[1] = mg2;
moduleGradeArray[2] = mg3;
moduleGradeArray[3] = mg4;
moduleGradeArray[4] = mg5;
moduleGradeArray[5] = mg6;
}
while (rstImportStudent.next()) {
Student student = new Student(rstImportStudent.getString("firstName"), rstImportStudent.getString("middleName"),
rstImportStudent.getString("lastName"),
rstImportStudent.getString("email"), rstImportStudent.getString("phoneNumber"), rstImportStudent.getInt("dob"),
moduleGradeArray);
ClassGroup.listOfStudents.add(student);
}
}
rstImportStudent.close();
rstImportModule.close();
myConn.close();
return ClassGroup.listOfStudents;
}
}
答案 0 :(得分:1)
如果您对一条语句调用close,它将自动从该con出现的那一刻起关闭con。
您在这里收到错误消息吗?
rstImportStudent.close();
rstImportModule.close();
myConn.close();
如果关闭前有异常怎么办?您应该对资源使用try。