java.sql.SQLException: 结果集关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21728494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:22:20  来源:igfitidea点击:

java.sql.SQLException: ResultSet closed

javasqlite

提问by Laxmeena

I cant not do the further process because I get ResultSet closed while running this code. I am adding the value after 15 row of the sqlite database table and I want to find average value of 15 row and that should be store in ArrayList.

我不能做进一步的处理,因为我在运行此代码时关闭了 ResultSet。我在 sqlite 数据库表的 15 行之后添加值,我想找到 15 行的平均值,并且应该存储在 ArrayList 中。

Here is the code:-

这是代码:-

try {
    Class.forName("org.sqlite.JDBC");
    c = DriverManager.getConnection("jdbc:sqlite:test.db");
    c.setAutoCommit(false);
    System.out.println("Opened database successfully3");
    stmt = c.createStatement();
    rs = stmt
            .executeQuery("SELECT TIME,BID,ASK FROM '" + title + "' ;");
    System.out.println("Opened database successfully 20");
    while (rs.next()) {
        i++;
        System.out.println(i);
        if (i > 15) {
            System.out.println("i am in");
            List<String> stocklist = new ArrayList<String>();
            String main = rs.getString(1);
            System.out.println(rs.getString(1));
            String s[] = main.split(" ", 2);
            String date = s[0];
            String time = s[1];
            stocklist.add(date);
            stocklist.add(time);
            stocklist.add(df.format(rs.getFloat("BID")));
            stocklist.add(df.format(rs.getFloat("ASK")));
            rs1 = stmt
                    .executeQuery("SELECT ASK FROM '" + title + "' ;");
            int j = 1;
            while (rs1.next()) {
                if (j < i) {
                    System.out.println(rs1.getFloat("ASK"));
                    avg = avg + rs1.getFloat("ASK");
                }
                j++;
            }
            rs1.close();

            System.out.println("i am out");
            avg = avg / 15;
            changepercent = ((rs.getFloat("ASK") - avg) / avg) * 100;
            stocklist.add(df.format(changepercent));
            stocklist.add("ETE");
            stocklist.add("HUN");
            stocklist.add("ALU");
            stocklist.add("ETE");
            stocklist.add("HUN");
            stocklist.add("ALU");
            stocklist.add("ETE");
            stocklist.add("HUN");
            hntm.addRow(stocklist);
        }
    }
    rs.close();
    stmt.close();
    c.close();
} catch (Exception e) {
    System.err.println(e.getClass().getName() + ": " + e.getMessage());
    System.exit(0);
}

采纳答案by Simulant

you should not reuse a Statement. When you create a new Query, you need so use a new Statement Object. Replace rs1 = stmt.executeQuery("SELECT ASK FROM '" + title + "' ;");with rs1=c.createStatement().executeQuery("SELECT ASK FROM '" + title + "' ;");

您不应该重复使用 Statement。创建新查询时,您需要使用新的语句对象。替换rs1 = stmt.executeQuery("SELECT ASK FROM '" + title + "' ;");rs1=c.createStatement().executeQuery("SELECT ASK FROM '" + title + "' ;");

回答by thiagola92

This is not the answer for this question, but i was also having problem with java.sql.SQLException: ResultSet closed.

这不是这个问题的答案,但我也遇到了java.sql.SQLException: ResultSet closed.

In my case the result from the query was empty, so the ResultSet returned closed.

在我的情况下,查询结果为空,因此 ResultSet 返回关闭。

PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, email);
ResultSet resultSet = preparedStatement.executeQuery();

// resultSet closed == didn't find anything
if(resultSet.isClosed())
    return false;