java.sql.SQLException:不允许对 ResultSet.TYPE_FORWARD_ONLY 类型的结果集进行操作 [重复]

问题描述 投票:0回答:0

我收到这个错误:

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)

这是我的旧代码项目,当我使用 NetBeans 8.2 时它可以工作,但在 NetBeans 17 中就不行了。我不知道这段时间代码是否有变化。

我有一个 Java 类,当我从不同的源执行一个进程时,我可以很容易地调用它。这是代码:

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;
    }
}
java mysql jdbc
© www.soinside.com 2019 - 2024. All rights reserved.