Allright been trying to figure this out the last 2 days.
Statement statement = con.createStatement();
String query = "SELECT * FROM sell";
ResultSet rs = query(query);
while (rs.next()){//<--- I get there operation error here
This is the query method.
public static ResultSet query(String s) throws SQLException {
try {
if (s.toLowerCase().startsWith("select")) {
if(stm == null) {
createConnection();
}
ResultSet rs = stm.executeQuery(s);
return rs;
} else {
if(stm == null) {
createConnection();
}
stm.executeUpdate(s);
}
return null;
} catch (Exception e) {
e.printStackTrace();
con = null;
stm = null;
}
return null;
}
How can I fix this error?
there are few things you need to fix. Opening a connection, running a query to get the rs, closing it, and closing the connection all should be done in the same function scope as far as possible. from your code, you seem to use the "con" variable as a global variable, which could potentially cause a problem. you are not closing the stm object. or the rs object. this code does not run for too long, even if it has no errors. Your code should be like this:
IMHO, you should do everything you need with your ResultSet before you close your connection.
I know this is a few years late, but I've found that synchronizing the db methods usually get rid of this problem.
use another Statement object in inner loop Like
It's hard to be sure just from the code you've posted, but I suspect that the
ResultSet
is inadvertently getting closed (orstm
is getting reused) inside the body of thewhile
loop. This would trigger the exception at the start of the following iteration.Additionally, you need to make sure there are no other threads in your application that could potentially be using the same DB connection or
stm
object.