Solve java.sql.SQLException: Invalid operation for forward only resultset


In this post we will take a look at java.sql.SQLException :Invalid operation for forward only resultset in JDBC java. And we will try to figure out why it happen and find appropriate solution to solve it.
You must Read : Sqlexception-in-java




ResultSet type TYPE_FORWARD_ONLY does not allow first(), last(), previous(), absolute or relative()

ResultSet type = ResultSet.TYPE_FORWARD_ONLY
ResultSet concurrency = ResultSet.CONCUR_READ_ONLY or
   ResultSet.CONCUR_UPDATABLE



java.sql.SQLException: Invalid operation for forward only resultset : first

   prepStmt = con.prepareStatement("select * from EMPLOYEE",
                 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
   rs = prepStmt.executeQuery();
   rs.last(); //java.sql.SQLException: Invalid operation for forward only resultset : last
   

  
java.sql.SQLException: Invalid operation for forward only resultset : last

   prepStmt = con.prepareStatement("select * from EMPLOYEE",
                 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
   rs = prepStmt.executeQuery();
   rs.last(); //java.sql.SQLException: Invalid operation for forward only resultset : last
      

  
java.sql.SQLException: Invalid operation for forward only resultset : previous

   prepStmt = con.prepareStatement("select * from EMPLOYEE",
                 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
   rs = prepStmt.executeQuery();
   rs.next();
      rs.previous(); //java.sql.SQLException: Invalid operation for forward only resultset : last
      

  
java.sql.SQLException: Invalid operation for forward only resultset : absolute

   prepStmt = con.prepareStatement("select * from EMPLOYEE",
                 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
   rs = prepStmt.executeQuery();
   rs.absolute(1); //java.sql.SQLException: Invalid operation for forward only resultset : absolute
      

java.sql.SQLException: Invalid operation for forward only resultset : relative

   prepStmt = con.prepareStatement("select * from EMPLOYEE",
                 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
   rs = prepStmt.executeQuery();
   rs.absolute(1); //java.sql.SQLException: Invalid operation for forward only resultset : relative
      





ResultSet concurrency mode CONCUR_READ_ONLY does not allow first(), last(), previous(), absolute or relative() in JDBC java

ResultSet type = ResultSet.TYPE_FORWARD_ONLY or
ResultSet.TYPE_SCROLL_INSENSITIVE or
ResultSet.TYPE_SCROLL_SENSITIVE
ResultSet concurrency = ResultSet.CONCUR_READ_ONLY



java.sql.SQLException: Invalid operation for read only resultset: updateString

when ResultSet  mode is CONCUR_READ_ONLY it cannot be update.

      prepStmt = con.prepareStatement("select NAME from EMPLOYEE",
                ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  
   rs = prepStmt.executeQuery();
  
   rs.next(); //move cursor to first row in ResultSet object
   //Print data in first row
   System.out.println(rs.getString("NAME")); //will print data in first row
  
   rs.updateString("NAME", "ankitUpdated");//java.sql.SQLException: Invalid
                      operation for read only resultset: updateString



java.sql.SQLException: Invalid operation for read only resultset: deleteRow

when ResultSet  mode is CONCUR_READ_ONLY row cannot be deleted from it.

      prepStmt = con.prepareStatement("select NAME from EMPLOYEE",
                ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  
   rs = prepStmt.executeQuery();
  
   rs.next(); //move cursor to first row in ResultSet object
   //Print data in first row
   System.out.println(rs.getString("NAME")); //will print data in first row
  
   rs.deleteRow();//java.sql.SQLException: Invalid operation for read only resultset: deleteRow



java.sql.SQLException: Invalid operation for read only resultset: moveToInsertRow

when ResultSet  mode is CONCUR_READ_ONLY row cannot be inserted into it.

prepStmt = con.prepareStatement("select NAME from EMPLOYEE",
                ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  
   rs = prepStmt.executeQuery();
  
   rs.moveToInsertRow(); //java.sql.SQLException: Invalid operation for read only
                                resultset: moveToInsertRow
   rs.updateString("NAME", "ankitNew");
rs.insertRow();


Labels: Core Java JDBC
eEdit
Must read for you :