SQLException:java.sql.SQLException:ResultSet关闭后不允许操作,而ResultSet从未关闭

SQLException:java.sql.SQLException: Operation not allowed after ResultSet closed, While ResultSet never closed

据我所知,我们不能在 closed.and 之后调用 ResultSet 这是关闭每个 ResultSetStatement 和 [=16= 的好习惯] 使用后

但是在我的代码中我从来没有关闭连接那么为什么有-

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

我的代码如下:

已编辑-

try{
        Connection con=CommonUtil.getConnection();
        Statement st=con.createStatement();

        ResultSet rs=st.executeQuery("select * from logirecord");
        int flag=0;
        while(rs.next()){
            if(rs.getString(2).trim().equals(username)&&rs.getString(3).trim().equals(password)){
                flag=1;

                ResultSet rs1=st.executeQuery("select * from personrecord where LoginId='"+ rs.getString(1).trim()+"'");
                if(rs1.next()){
                String name=rs1.getString(1);
                String address=rs1.getString(2);
                String hobby=rs1.getString(4);
                }
                //Exception here.
                ResultSet rs2=st.executeQuery("select * from interest where LoginId='"+rs.getString(1).trim()+"'");
                //at com.org.test.LoginServlet.doPost(LoginServlet.java:49)
                if(rs2.next()){
                String interest=rs2.getString(2);
                String interest2=rs2.getString(3);
                String interest3=rs2.getString(4);
                }
                request.getRequestDispatcher("display.jsp").forward(request, response);
                return;
            }
        }   
    }catch(Exception e){
        e.printStackTrace();
    }

好吧,你正在

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

因为你试图遍历一个结果集,同时遍历相同连接的另一个结果集。

如果您嵌套处理同一数据库中的两个结果集,那您就错了。这些集合的组合应该在数据库端完成。

您有几个属于同时打开的同一个语句对象的结果集。 (参见此处 - http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html):

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.

而且你没有关闭 rs 使它变得更糟。