Java – operation is not allowed after the resultset is closed

Well, he has been trying to solve this problem for the past two days

Statement statement = con.createStatement();
                        String query = "SELECT * FROM sell";
                        ResultSet rs = query(query);
                        while (rs.next()){//<--- I get there operation error here

This is the query method

public static ResultSet query(String s) throws sqlException {
        try {
            if (s.toLowerCase().startsWith("select")) {
                if(stm == null) {
                    createConnection();
                }
                ResultSet rs = stm.executeQuery(s);
                return rs;
            } else {
                if(stm == null) {
                    createConnection();
                }
                stm.executeUpdate(s);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            con = null;
            stm = null;
        }
        return null;
    }

How can I solve this error?

Solution

It's hard to determine from your published code, but I suspect that the resultset is inadvertently closed (or STM is reused) in the while loop This will trigger an exception at the beginning of the next iteration

In addition, you need to ensure that no other thread in the application may use the same DB connection or STM object

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>