Solved

java.sql.SQLException: After end of result set

Posted on 2004-04-01
22
2,294 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
  • 12
  • 9
22 Comments
 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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
 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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:mayankeagle
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:mayankeagle
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:
mayankeagle 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:mayankeagle
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:mayankeagle
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:mayankeagle
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now