Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2621
  • Last Modified:

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)

0
junkyforjc
Asked:
junkyforjc
  • 12
  • 9
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
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?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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
0
 
junkyforjcAuthor Commented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Mayank SAssociate Director - Product EngineeringCommented:
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?).
0
 
junkyforjcAuthor Commented:
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>
<%}%>
                         
0
 
junkyforjcAuthor Commented:
Again, as before, this is code that worked for a couple of years prior to moving my site to new host!
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
junkyforjcAuthor Commented:
Where should the close bracket be placed?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
else{

%>
     <jsp:forward page="user_login.jsp" >
                <jsp:param name="msg" value="Please enter a valid Username and Password." />
                </jsp:forward>
<% } } %> // --> HERE - ADDED
0
 
junkyforjcAuthor Commented:
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....????
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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

}
0
 
junkyforjcAuthor Commented:
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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Post the updated code.
0
 
junkyforjcAuthor Commented:
<%@ 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>
<%}}%>
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Does it enter the else part?

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

%>
0
 
junkyforjcAuthor Commented:
yes that shows up in the stderr.log....
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Then I don't know why it does not enter the <jsp:forward page="user_login.jsp" > part. Try putting it outside the while.

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>

<%
    }
  }

%>

<jsp:forward page="user_login.jsp" >
<jsp:param name="msg" value="Please enter a valid Username and Password." />
</jsp:forward>
0
 
mmuruganandamCommented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
The other way would be to use a flag and to do it outside the while loop by checking the flag.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Oh sorry, Muruga has also done that.
0
 
junkyforjcAuthor Commented:
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.

0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 12
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now