我无法执行进一步的处理,因为我在运行此代码时关闭了ResultSet。我在sqlite数据库表的15行之后添加了值,我想找到15行的平均值,并且应该存储在ArrayList中。
以下是代码: -
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);
}
答案 0 :(得分:8)
您不应该重复使用Statement。创建新查询时,需要使用新的Statement对象。将rs1 = stmt.executeQuery("SELECT ASK FROM '" + title + "' ;");
替换为rs1=c.createStatement().executeQuery("SELECT ASK FROM '" + title + "' ;");
答案 1 :(得分:0)
这不是这个问题的答案,但是我对java.sql.SQLException: ResultSet 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;