Search code examples
javanetbeanssqlexception

SQLEXCEPTION Operation not allowed after ResultSet closed error insist


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();
     }

Solution

  • 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