java.sql.SQLException: Operation not allowed after ResultSet closed MySQL Java

后端 未结 1 1945
北恋
北恋 2020-12-12 05:49

I keep getting this whenever I try to use this method.

java.sql.SQLException: Operation not allowed after ResultSet closed
    at com.mysql.jdbc.SQLError.cre         


        
相关标签:
1条回答
  • 2020-12-12 06:14

    While iterating through your ResultSet, you are reusing the Statement object for updating the data (inside your query method), which closes the ResultSet from the query.

    See the ResultSet documentation:

    "A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results."

    You should create separate Statements for the query and the update, and pass them to your query method:

    public ResultSet query(String s, Statement statement) throws SQLException {
        ...
    }
    

    I assume that statement is declared static inside your class - there is usually no need for that: create the two statements for query and update in the constructor of your class, and either pass them to the query() method or use the one or the other depending on the statement.

    0 讨论(0)
提交回复
热议问题