Solved

java.sql.SQLException: After end of result set

Posted on 2004-04-01
22
2,538 Views
Last Modified: 2012-05-04
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
Comment
Question by:junkyforjc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
22 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738479
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738485
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
 

Author Comment

by:junkyforjc
ID: 10738496
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 10738506
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
 

Author Comment

by:junkyforjc
ID: 10738514
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
 

Author Comment

by:junkyforjc
ID: 10738520
Again, as before, this is code that worked for a couple of years prior to moving my site to new host!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738534
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
 

Author Comment

by:junkyforjc
ID: 10738569
Where should the close bracket be placed?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738597
else{

%>
     <jsp:forward page="user_login.jsp" >
                <jsp:param name="msg" value="Please enter a valid Username and Password." />
                </jsp:forward>
<% } } %> // --> HERE - ADDED
0
 

Author Comment

by:junkyforjc
ID: 10738622
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738646
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
 

Author Comment

by:junkyforjc
ID: 10738652
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738672
Post the updated code.
0
 

Author Comment

by:junkyforjc
ID: 10738710
<%@ 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738730
Does it enter the else part?

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

%>
0
 

Author Comment

by:junkyforjc
ID: 10738756
yes that shows up in the stderr.log....
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 10738794
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10738804
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738818
The other way would be to use a flag and to do it outside the while loop by checking the flag.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738829
Oh sorry, Muruga has also done that.
0
 

Author Comment

by:junkyforjc
ID: 10738863
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 10739086
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question