1

I am getting this error:

java.sql.SQLException: Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.result.ResultSetImpl.last(ResultSetImpl.java:1787)
    at ub.li.QueryProcessor.getAllRecord(QueryProcessor.java:39)
    at ub.li.Home.setsytable(Home.java:40)
    at ub.li.Home.<init>(Home.java:36)
    at ub.li.UBLI.main(UBLI.java:19)

It was my old code project and it works when I use NetBeans 8.2, but not anymore in NetBeans 17. I do not know if there was a change in code over the time.

I have a Java class where I can easily call when I execute a process from different source. Here is the code:

public class QueryProcessor {
    Statement stmt;
    ResultSet rs; //represents the result set of a database query .  refers to the row and column data contained in a ResultSet object
    Connection con;
    ResultSetMetaData metadata;

    public QueryProcessor() {
        try {
            DBConnection.setConnection();
            con = DBConnection.getConnection();
            stmt = con.createStatement();
        } catch (Exception e) {e.printStackTrace();}
    }

    public String[][] getAllRecord(String query) {
        String row[][] = null;
        try {
            rs = stmt.executeQuery(query);
            if (rs.last()) {
                metadata = rs.getMetaData();
                row = new String[rs.getRow()][metadata.getColumnCount()];
                rs.first();
                int r = 0;
                do {
                    for (int col = 0; col<metadata.getColumnCount(); col++) {
                        row[r][col] = rs.getString(col+1);
                    }
                    r++;
                } while(rs.next());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return row;
    }

    
    public String getSpecificField(String query) {
        String record = null;
        try {
            rs = stmt.executeQuery(query);
            if (rs.first()) {
                metadata = rs.getMetaData();
                for (int col = 0; col<metadata.getColumnCount(); col++) {   
                    record = rs.getString(col);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            javax.swing.JOptionPane.showMessageDialog(null, "ERROR");
        }
        return record;
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Can you highlight the code where the exception is occurring? – prasad_ Apr 04 '23 at 03:34
  • You cannot call `last()` (nor `first()` on a forward-only result set. It is also entirely unnecessary. You should be able to call `rs.getMetaData()` immediately. And instead of an array, use an `ArrayList`, and you don't need to know the number of rows in advance. – Mark Rotteveel Apr 05 '23 at 10:18

0 Answers0