Link to home
Start Free TrialLog in
Avatar of Jerry Rankin
Jerry Rankin

asked on

java.sql.SQLException: After end of result set

Gentlemen,

You guys were great the other night in helping me isolate the main problem with the move of my website and getting to the bottom of the problem with the db.  I now have only the following error when I try to look up a user:

500 Servlet Exception
java.sql.SQLException: After end of result set
      at org.gjt.mm.mysql.ResultSet.checkRowPos(Unknown Source)
      at org.gjt.mm.mysql.ResultSet.getString(Unknown Source)
      at org.gjt.mm.mysql.ResultSet.getString(Unknown Source)
      at _java._roundrockonline._users._check_0user_0login__jsp._jspService(/java/roundrockonline/users/check_user_login.jsp:31)
      at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
      at com.caucho.jsp.Page.subservice(Page.java:506)
      at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:182)
      at com.caucho.server.http.Invocation.service(Invocation.java:315)
      at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:346)
      at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:274)
      at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
      at java.lang.Thread.run(Thread.java:536)

Avatar of Mayank S
Mayank S
Flag of India image

You're trying to access a ResultSet after you have already scrolled through it, perhaps. You're not at the correct position in your ResultSet. What type of ResultSet is it? Can you scroll back to the first record and then start scrolling again?
rs = stmt.executeQuery ( "some query" ) ;

while ( rs.next () )
  System.out.println ( rs.getString ( "someColumn" ) ) ;

while ( rs.next () ) // -> will throw exception
  ....

String str = rs.getString ( "someColumn" ) ; // -> will also throw exception
Avatar of Jerry Rankin
Jerry Rankin

ASKER

I get this error when I input a username (email address) and password for signing into the site for current users.  If I put in a name or login that does not exist, I get the proper message that says try again.  I can also add new without problem.  This only pops up when I attempt to login with a valid name.
Can you post the code? You seem to be accessing at an invalid position when resultSet.next () is true. Either a wrong row or a wrong column (are you using column-index or column-names?).
This is the code.  Line 31 is marked.


<%@ page language="Java" contentType="text/html" %>
<%@ page import="java.util.*,java.sql.*" %>


<% roundrockonline.connect.ConnectionPool pool = roundrockonline.connect.ConnectionPool.getInstance();
String username="",password="",pass="";
int user_id=0;
Connection conn=pool.getConnection();

if(request.getParameter("username")!=null){
    username=request.getParameter("username");
}
if(request.getParameter("password")!=null){
    password=request.getParameter("password");
}

Statement stmt = conn.createStatement();
Statement stmt1 = conn.createStatement();
ResultSet rs1;
ResultSet rs = stmt.executeQuery("SELECT * from user_login where email_id='"+username+"'");

while(rs.next())
   pass = rs.getString("password");

 if(conn!=null) pool.returnConnection(conn);

if(pass.equals(password)){

    session=request.getSession(true);
    session.setMaxInactiveInterval(1800);
line 31:::    session.setAttribute("email_id",rs.getString("email_id"));

 %>
 <jsp:forward page="user_home.jsp" >
                   <jsp:param name="msg" value="Welcome to Customer section." />
                   </jsp:forward>
<%}
  else{

%>
     <jsp:forward page="user_login.jsp" >
                   <jsp:param name="msg" value="Please enter a valid Username and Password." />
                   </jsp:forward>
<%}%>
                         
Again, as before, this is code that worked for a couple of years prior to moving my site to new host!
The problem is actually exactly what I said - you are scrolling out of the ResultSet and then accessing it. Notice your while loop. It ends at:

>> pass = rs.getString("password");

You will keep reading into pass from rs. You need to put braces to access the same row-position in rs.getString ( "email_id" ) ;

while(rs.next())
{  // -> BRACE ADDED
   pass = rs.getString("password");

 if(conn!=null) pool.returnConnection(conn);

if(pass.equals(password)){

    session=request.getSession(true);
    session.setMaxInactiveInterval(1800);
    session.setAttribute("email_id",rs.getString("email_id"));

Put a closing brace for it too.

>> if(conn!=null) pool.returnConnection(conn);

I don't know why you have that line. Maybe you can remove it.
Where should the close bracket be placed?
else{

%>
     <jsp:forward page="user_login.jsp" >
                <jsp:param name="msg" value="Please enter a valid Username and Password." />
                </jsp:forward>
<% } } %> // --> HERE - ADDED
You are the greatest!  I cannot believe that I can get such GREAT help at the drop of a hat!!!!!!  I wish I had knowledge to pass back to you guys that help all of us out here!!

That worked... I don't understand what happened that the code worked before and not now....????
What happened before was:

while ( rs.next () )
  pass = rs.getString ( "...." ) ;

- the while loop ended here and the ResultSet moved out of position.

Outside the while loop, you are accessing rs.getString ( "email_id" ) at a wrong cursor position.

If you put a brace:

while ( rs.next () )
{
  pass = ....
  ..
  ..
  rs.getString ( "email_id" ) ; // still accesses the same cursor-position of rs

}
1 problem...now when I enter a  user name and password that does not exist I don't get to the screen that says to Please enter a valid Username and Password...I simply get a blank screen...sorry
Post the updated code.
<%@ page language="Java" contentType="text/html" %>
<%@ page import="java.util.*,java.sql.*" %>


<% roundrockonline.connect.ConnectionPool pool = roundrockonline.connect.ConnectionPool.getInstance();
String username="",password="",pass="";
int user_id=0;
Connection conn=pool.getConnection();

if(request.getParameter("username")!=null){
    username=request.getParameter("username");
}
if(request.getParameter("password")!=null){
    password=request.getParameter("password");
}

Statement stmt = conn.createStatement();
Statement stmt1 = conn.createStatement();
ResultSet rs1;
ResultSet rs = stmt.executeQuery("SELECT * from user_login where email_id='"+username+"'");

while(rs.next()){
   pass = rs.getString("password");

   //if(conn!=null) pool.returnConnection(conn);

if(pass.equals(password)){

    session=request.getSession(true);
    session.setMaxInactiveInterval(1800);
    session.setAttribute("email_id",rs.getString("email_id"));

 %>
 <jsp:forward page="user_home.jsp" >
                   <jsp:param name="msg" value="Welcome to Customer section." />
                   </jsp:forward>
<%}
  else{

%>
     <jsp:forward page="user_login.jsp" >
                   <jsp:param name="msg" value="Please enter a valid Username and Password." />
                   </jsp:forward>
<%}}%>
Does it enter the else part?

<%}
  else{
        System.out.println ( "Invalid password. " ) ; // DOES THIS GET PRINTED - just check

%>
yes that shows up in the stderr.log....
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
boolean isValid = false;

while(rs.next()){
   pass = rs.getString("password");

   //if(conn!=null) pool.returnConnection(conn);

if(pass.equals(password)){

    session=request.getSession(true);
    session.setMaxInactiveInterval(1800);
    session.setAttribute("email_id",rs.getString("email_id"));

isValid = true;
}
  else{
isValid = false;
}
}

if (isValid)
{
%>
 <jsp:forward page="user_home.jsp" >
                <jsp:param name="msg" value="Welcome to Customer section." />
                </jsp:forward>
<%
}
else
{
%>
     <jsp:forward page="user_login.jsp" >
                <jsp:param name="msg" value="Please enter a valid Username and Password." />
                </jsp:forward>

<%
}
%>


This would help you even when there is no users found.
The other way would be to use a flag and to do it outside the while loop by checking the flag.
Oh sorry, Muruga has also done that.
It works....use the code from above and replaced my code with the use of Muruga's code.  

How do you want me to post the points?  Mayankeagle has helped me through all of this and his code works too.

If you want to give Muruga some points, you can post a question "Points for mmuruganandam" and allot some points to that question. When he posts a comment on it, you can accept it as answer. Be sure to give a link to this question on that one, and also post a comment on this one once you have done that.