Resultset 自动关闭并显示错误 Operation not allowed after ResultSet closed

Resultset automatically close itself and showing error Operation not allowed after ResultSet closed

获取异常

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

<%
ResultSet rs=null,rs1=null;
Statement stmt=null,stmt1=null;
String UserID = request.getParameter("UserID"); 
String Password = request.getParameter("Password");
session.setAttribute("UserID",UserID);
int flag=0;
try{    
    System.out.println("Validating..1");
    //stmt =  con.createStatement();
    //stmt1 =  con.createStatement();

    String Query = "select * from login where UserID = '"+UserID+"' and Password='"+Password+"'";
    System.out.println(Query);
    rs = st.executeQuery(Query);
    System.out.println(rs);
    if(rs!=null)
    {
        String Query1="Select * from basicdetails where UserID='"+UserID+"' and Password='"+Password+"'";
        System.out.println(Query1);
        rs1=st.executeQuery(Query1);
        //System.out.println(rs1);
        if(!rs1.next())
        {
            System.out.println("RS1");
            while(rs1.next())
            {
                String PhotoPath=rs1.getString(4);
                System.out.println("-------------------"+PhotoPath);
                session.setAttribute("PhotoPath",PhotoPath);
            }
        }
    }
    if(!rs.next())
    {
        String Auth=rs.getString(3);
        session.setAttribute("Auth",new Integer(Auth));
        flag=1;
        if(Auth.equals("0"))
        {
            //Show Admin Menu
        %>
        <jsp:forward page="AdminMenu.jsp"/>
    <%
    }
        else if(Auth.equals("1"))
        {
            //Show user Menu
    %>
    <jsp:forward page="UserHome.jsp"/>
    <%
    }
    }
    else
    {
        flag=0;
    %>
    <jsp:forward page="Login.jsp"/>
    <%
    }
    stmt.close();
    con.close();
}catch(Exception e){
            System.out.println(e.getCause());
                        System.out.println(e.getStackTrace());
                        System.out.println(e.getClass());
            %><%=e%><%
        }

%>

这是 Netbeans

的输出 window 中显示的消息

Validating..1 select * from login where UserID = 'admin' and Password='admin' com.mysql.jdbc.JDBC4ResultSet@b2c1e7 Select * from basicdetails where UserID='admin' and Password='admin' RS1 null [Ljava.lang.StackTraceElement;@1d8608c class java.sql.SQLException

上面的代码有什么问题。jsp 文件试图验证管理员和其他用户的登录凭据。

您正在执行 ResultSet.next() 之后它已经返回 false。

这段代码没有任何意义。您正在检查 rs == null 不能为空的地方。 executeQuery() 返回的 ResultSets 不能为空。你可以用一个查询和一个连接来完成整个事情。为什么你会在两个表中有一个密码字段是一个谜,否则第二个查询是一个错误。

在得到各位专家的回答后,我更改了我在问题中发布的整个代码。作为初学者,我们希望得到您的正确指导和适当的说明,仅此而已。这是代码-

<%
ResultSet rs=null,rs1=null;

    String UserID = request.getParameter("UserID"); 
    String Password = request.getParameter("Password");
    session.setAttribute("UserID",UserID);
    int flag=0;
    try{    
        System.out.println("Validating..1");
        int Auth=1,flag1 = 0;

        String Query = "select * from login where UserID = '"+UserID+"' and Password='"+Password+"'";
        String Que = "select * from basicdetails where userid='"+UserID+"' and password='"+Password+"';";
        System.out.println(Que);
        rs = st.executeQuery(Query);
        int i=0;
        while(rs.next())
        {
            if(UserID.equals(rs.getString(1)) && Password.equals(rs.getString(2)))
            {
               Auth=rs.getInt(3);
               session.setAttribute("Auth",new Integer(Auth));
            }
            i++;
        }
        rs.close();

        if(i>0)
        {
         System.out.println("I " + i);
            if(Auth==1)
            {
                System.out.println("USER Verification");
            rs1= st.executeQuery(Que);
            System.out.println(rs1);
            rs1.next();
            String PhotoPath=rs1.getString(4);
            System.out.println("-------------------"+PhotoPath);
            session.setAttribute("PhotoPath",PhotoPath);
            %>
                <jsp:forward page="UserHome.jsp"/>
            <%
            }
            else if(Auth==0)
            {
            %>
                <jsp:forward page="AdminMenu.jsp"/>
            <%
            }
        }
        else
        {
            %>
            <jsp:forward page="Login.jsp"/>
            <%
        }        
        st.close();
        con.close();
    }
    catch(Exception e)
    {
    %>
      <%=e%>
    <% } %>

谢谢大家,不好意思。再次感谢。