'java.sql.SQLException: ResultSet closed [duplicate]
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.
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);
}
Solution 1:[1]
This is not the answer for this question, but i was also having problem with java.sql.SQLException: ResultSet closed.
In my case the result from the query was empty and i was using SQLite, so the ResultSet returned closed.
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
// resultSet closed == didn't find anything
if(resultSet.isClosed())
return false;
I tested using PostgreSQL and ResultSet stay open even when query is empty. I didn't expect different behavior from different databases.
If anyone want to check my test: https://gist.github.com/thiagola922/fcd80f0f29b83106304c3b64cf64b2b1
As @marquis-of-lorne said, a better way to check if the connection still open is using resultSet.next().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
