Search code examples
javaoracle-databasejdbc

java.sql.SQLException: Closed Resultset: next


I didn't find the reason for a SQLExeption, I got it at while(dbrs.next()). It is simple, it was working, but after update to Oracle Server 19, I got this error. I got still a result set in Oracle Developer with the same account.

Statement st = conn.createStatement();
if (Oracle){
  ResultSet dbrs = st.executeQuery("select table_name from all_tables");
  while(dbrs.next()){
     if (dbrs.getString(1).equals(G_IN)){
        st.execute("DROP TABLE "+G_IN);
        System.out.println(G_IN+" gelöscht");
     }
  }
  dbrs.close();
}else{
   String loeschen="DROP TABLE IF EXISTS \"" + G_IN +"\"";
   System.out.println(loeschen);
   st.execute( loeschen );
}

Solution

  • You shouldn't reuse a Statement object as is the case in your while loop. To execute a new query, you need to use a new Statement object.

    Replace

    st.execute("DROP TABLE " + G_IN);
    

    with

    conn.createStatement().execute("DROP TABLE " + G_IN);
    

    and it should work.