This question already has answers here:
Getting java.sql.SQLException: Operation not allowed after ResultSet closed
(2 answers)
Closed 6 years ago.
Im getting the exception:
Operation not allowed after ResultSet closed
Where am I wrong?
code:
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","root");
Statement s=con.createStatement();
ResultSet rs1=s.executeQuery("Select * from items");
while(rs1.next()) {
q= (q+Integer.parseInt(rs1.getString("qty")));
//update items Set qty=5 where name='Maggi';
s.executeUpdate("update items SET qty="+q+" WHERE name='"+value+"'");
}
}
catch(Exception ee){
System.out.println(ee);
}
As you will need two different Statement Objects to execute the query and update parts try:
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","root");enter code here
Statement s=con.createStatement();
ResultSet rs1=s.executeQuery("Select * from items");
while(rs1.next()){
q= (q+Integer.parseInt(rs1.getString("qty")));
s=con.createStatement(); // <- create a new statement here
s.executeUpdate("update items SET qty="+q+" WHERE name='"+value+"'");
}
}
catch(Exception ee){
System.out.println(ee);
}
}
Also consider using a PreparedStatement and using set* method to avoid potential SQL Injection
Related
I am extracting about 10,000 patient ids and at the same time after performing encryption I want to insert them in a different table but I am getting this error
java.sql.SQLException: Operation not allowed after ResultSet closed.
Connection conn=null;
PreparedStatement pst=null;
try{
conn = DriverManager.getConnection(constring,username,password);
System.out.println("connected");
Statement stmt=(Statement) conn.createStatement();
ResultSet srs = stmt.executeQuery(
"select patient_id from patient");
while (srs.next()) {
patient_id = srs.getInt("patient_id");
System.out.println(patient_id);
JavaApplication5 o=new JavaApplication5(key);
g=patient_id;
o.c=main.g;
s=o.encrypt(o.c,key);
System.out.println(s);
String insert = "Insert into fyp1 (patient_id) values ('"+s+"')";
stmt.executeUpdate(insert);
}
}
catch(SQLException e){System.out.println(e);
You need to create another Statement since the first ResultSet srs is closed when it is run again at stmt.executeUpdate(insert);.
See in the JavaDocs for ResultSet
Note: A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.
Btw. Please post always the full stacktrace e.g. with the corresponding line numbers.
Im having this problem that I cant do a executeQuery and then a executeUpdate in the same method it gives me "PSQLException: This ResultSet is closed."
I have tried to create another statement and other ResultSet but with no success. I you have any Idea how you help me, I would appreciate it. Thanks
Statement stmt = pc.getStatement();
try
{
ResultSet rs = stmt.executeQuery("select * from artigos where
artigoc="+s);
while (rs.next())
{
int stock = rs.getInt("stock");
stmt.executeUpdate("update artigos set stock="+newStock+","
+ "vendidos="+newVendidos+" where artigoc="+s);
}
rs.close(); // muito importante depois da consulta!
} catch (Exception e) {
e.printStackTrace();
System.err.println("Problems retrieving data from db...");
}
The javadoc of Statement says:
All execution methods in the Statement interface implicitly close a current ResultSet object of the statement if an open one exists.
Use a different Statement object to execute the update.
error image
java.sql.SQLException: Closed Connection: next
at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at
oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:181)
at
com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:2859)
Code
if (rs != null) {
while (rs.next()) {
resultset = Stmt.executeQuery();
if (resultset.next()) {
count += resultset.getLong(1);
resultset.close();
resultset = null;
}
Stmt.close();
Stmt = null;
}
}
The error java.sql.SQLException: Closed Connection: next indicates that the resultSet is already closed when it is being used. Avoid closing the resultSet until it is guaranteed that it will not be used by the downstream code.
Looking at your code snippet, it looks like you are closing the Stmt instance withing the while loop and going to use it in the next iteration of the loop. That could be another reason for your issue. In that case, creating a new instance of the Stmt instance withing the loop should solve.
This question already has answers here:
JDBC ResultSet is giving only one row although there are many rows in table?
(3 answers)
Closed 6 years ago.
private void search_fKeyReleased(java.awt.event.KeyEvent evt) {
try {
is ther something wrong here
PreparedStatement pst =null;
ResultSet rst=(ResultSet) pst;
Connection con=(Connection)
DriverManager.getConnection("jdbc:mysql://localhost/iqari",
"root","");
String sql="select * from first where masaha_iqar=?";
pst=(PreparedStatement) con.prepareStatement(sql);
pst.setString(1,search_f.getText());
rst=pst.executeQuery();
in the text area i get on result instead of two available in my database
if (rst.next()){String add1=rst.getString("raqm_iqar");
jTextArea2.append(add1 + "\n");
System.out.format("%s",add1);
}
} catch (Exception e) {
}
TODO add your handling code here:
}
try {
PreparedStatement pst =null;
ResultSet rst=(ResultSet) pst;
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/iqari", "root","");
String sql="select * from first where masaha_iqar=?";
pst=(PreparedStatement) con.prepareStatement(sql);
pst.setString(1,search_f.getText());
rst=pst.executeQuery();
while (rst.next()){String add1=rst.getString("raqm_iqar");
jTextArea2.append(add1 + "\n");
System.out.format("%s",add1);
}
} catch (Exception e) {
}
Hello i need to do 2 active jobs with database in java.Firstly I did with 1 statement but after I read hints in here they said that I should use 2 statement.But although it still get Operation not allowed after ResultSet closed error.
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e){
System.err.println("Driver yok");
return;
}
Connection con=null;
try{
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/kutuphane","root","");
System.out.println("Veritabnı baglandıldı");
Statement stmt=con.createStatement();
String strSQL="UPDATE emanetler SET IADETARIH='"+strdate+"' WHERE KISIAD='"+jTextField1.getText()+"' "
Statement stmt2=con.createStatement();
stmt.execute(strSQL);
ResultSet rs=stmt2.executeQuery("SELECT * FROM kitaplar");
while(rs.next()){
if(rs.getString("KITAPAD").equals(jTextField2.getText())){
strSQL="UPDATE kitaplar SET KITAPADET="+rs.getInt("KITAPADET")+"+1 WHERE KITAPAD='"+jTextField2.getText()+"' ";
stmt2.execute(strSQL);
}
}
stmt.close();
stmt2.close();
}
catch(SQLException e){
System.out.println("Veritabanı baglanmadi");
e.printStackTrace();
}
First you are using stmt2 object
ResultSet rs=stmt2.executeQuery("SELECT * FROM kitaplar");
Then in while loop
strSQL="UPDATE kitaplar SET KITAPADET="+rs.getInt("KITAPADET")+"+1 WHERE KITAPAD='"+jTextField2.getText()+"' ";
stmt2.execute(strSQL);
This must be corrected to use a separate Statement Object in While Loop to execute queries.
Hope this helps