我正在使用JDBC和SQLite在同一个.java文件中的2个独立数据库上运行查询,但是当我尝试第二次调用单独的结果集时,我收到一个错误,结果集已关闭。我设置了连接和所有内容,然后进行3次单独调用,如下所示....然后调用ResultSet
对象来获取数据。第一次调用mealSet
Class.forName("org.sqlite.JDBC");
Connection conn1 = null;
conn1 = DriverManager.getConnection("jdbc:sqlite:workoutLog.db");
@SuppressWarnings("null")
PreparedStatement workoutQuery= conn1.prepareStatement("SELECT * FROM workoutLog");// ORDER BY t desc limit 1");
ResultSet workoutSet = workoutQuery.executeQuery();
Class.forName("org.sqlite.JDBC");
Connection conn2 = null;
conn2 = DriverManager.getConnection("jdbc:sqlite:foodLog.db");
PreparedStatement getFoodLog = conn2.prepareStatement("SELECT * FROM foodLog");// ORDER BY t desc limit 3");
ResultSet mealSet = getFoodLog.executeQuery();
mealSet.next();
meals.add(breakfast);
meals.add(lunch);
meals.add(dinner);
weekStart = workoutSet.getInt(ID_COLUMN);
weekEnd = weekEnd + 6;
Class.forName("org.sqlite.JDBC");
Connection conn3 = null;
conn3 = DriverManager.getConnection("jdbc:sqlite:projectLog.db");
PreparedStatement getProjectLog = conn3.prepareStatement("SELECT * FROM projectLog");
ResultSet projectSet = getProjectLog.executeQuery();
ResultSetMetaData meta = projectSet.getMetaData();
for (int i=3;i<meta.getColumnCount()-1;i++){
String prj="";
projects.add(prj);
}
while ( projectSet.next()){
for (int i=3;i<=meta.getColumnCount()-1;i++){
int arrayPos = 0;
String temp =projectSet.getString(i) + "\n";
projects.set(arrayPos, projects.get(arrayPos).concat(temp));
projectNames.add(meta.getColumnName(i));
totalHours+=projectSet.getInt(3);
totalMinutes+=projectSet.getInt(meta.getColumnCount());
arrayPos++;
}
for (int k=0;k<=2;k++){
breakfast=mealSet.getString(BREAKFAST_COLUMN)+" \n";
lunch=mealSet.getString(LUNCH_COLUMN)+" \n";
dinner=mealSet.getString(DINNER_COLUMN)+" \n";
meals.set(0, meals.get(0).concat(breakfast));
meals.set(1, meals.get(1).concat(lunch));
meals.set(2, meals.get(2).concat(dinner));
mealSet.next();
}
错误......
Exception in thread "main" java.sql.SQLException: ResultSet closed
at org.sqlite.RS.checkOpen(RS.java:63)
at org.sqlite.RS.markCol(RS.java:77)
at org.sqlite.RS.getString(RS.java:313)
at WeeklyReport.main(WeeklyReport.java:99)
答案 0 :(得分:1)
对于大量查询,您不能重复使用相同的ResultSet
实例。一个ResultSet
实例只能用于获取一个查询的结果。
此外,正如ResultSet JavaDoc中所写:
ResultSet
对象在生成它的Statement
对象关闭,重新执行或用于从多个结果序列中检索下一个结果时自动关闭。