-2

同じデータベースから 2 つのクエリを使用したいのですが、次のエラーが発生します。

java.sql.SQLException: 結果セットの終了後

この問題を回避するにはどうすればよいですか? すべてのテーブルの行数が同じではないことに注意してください。

コード:

try {
    Dbconnection NewConnect = new Dbconnection();
    Dbconnection NewConnect2 = new Dbconnection();
    Connection con = NewConnect.MakeConnect();
    Connection con2 = NewConnect2.MakeConnect();
    Statement stmt = con.createStatement();
    Statement stmt2 = con2.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * from class");
    ResultSet rs2 = stmt2.executeQuery("SELECT * from intervals");
    while (rs.next()) {
        clasess.add(rs.getString(2));
    }
    while (rs2.next()) {
        intervals.add(rs.getString(2));
    }
} catch (Exception ex) {
    System.out.println(ex);
}
4

2 に答える 2

3

In your second while loop, you're calling rs.getString instead of rs2.getString.

This would be avoided if you used a more efficient setup in the first place; at the absolute minimum, you're holding the rs2 cursor open unnecessarily during your first loop. I can't see any difference in configuration between your database connections, so just use one Connection, and then reuse the Statement and ResultSet variables, replacing them between while loops.

于 2013-09-09T05:42:30.840 に答える