Search code examples
javajdbcresultset

how to handle java sql exception:exhausted result set


i have problem with the below code. help me!!

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe","system","**********");
    Statement st=conn.createStatement();
    String sql="select password from db where username='user'";
    ResultSet rs=st.executeQuery(sql);
    rs.next();
    String password=rs.getString("password");
    if(password.equals(pass))
    {       
        RequestDispatcher rd=req.getRequestDispatcher("/home.jsp");
        rd.forward(req,res);
    }
    else
    {
        out.println("invalid username and password");
    }

when i execute this code i am getting an java sql exception : exhausted result set. thanks in advance...


Solution

  • Instead of using rs.next();, use it with while e.g while(rs.next()). Once you got the resultset, the pointer will point to the first record. Each time you do a rs.next(), the pointer will advances to the next record. If you use with while, once you reach to the end of your resultset, rs.next() will return false once all records are iterated. In your case, since you are not checking whether resultset has exhausted and trying to advanced the pointer, you are getting the exception.