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

784 views Asked by At

Getting Exception

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%><%
        }

%>

And Here is the message shown in output window of Netbeans

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

What is the issue with the above code which is .jsp file trying to validate login credential of admin and other users.

2

There are 2 answers

2
cbsecommerce On BEST ANSWER

I changed the whole code which I posted in my question, after getting answers from you all experts. We as a beginner expect your right guidance and proper clarification that's all. Here is the code -

<%
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%>
    <% } %>

Thank you everybody, and sorry for little harsh. Thank you again.

6
user207421 On

You are executing ResultSet.next() after it has already returned false.

This code doesn't make any sense. You are checking rs == null where it cannot be null. ResultSets returned by executeQuery() cannot be null. You could do the whole thing with a single query and a join. Why you would have a password field in two tables is a mystery, or else the second query is a bug.