Results 1 to 15 of 15
  1. #1
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default After End Of Result set error?

    ok so i have a problem with a database connection here is the query and the error i get please help

    Java Code:
        public String getonlineplayer(int mapid,int pid){
            String returnstring ="";
            if(mapid>0 && pid!=0){
                try{
                    pst = con.prepareStatement("select * from players where areaid='"+mapid+"' and online='1' and ID!='"+pid+"'");
                    rs = pst.executeQuery();
                    while(rs.next()){
                        if(rs!=null){
                            returnstring += "username="+rs.getString("uname")+
                            ",xpos="+rs.getInt("xpos")+",ypos="+rs.getInt("ypos")+",playerimage="+rs.getInt("playerimage")+"#";
                        }else{
                            break;
                        }
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            return returnstring;
        }
    Java Code:
    java.sql.SQLException: After end of result set
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    	at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
    	at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2674)
    	at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2815)
    	at Mysql.getonlineplayer(Mysql.java:95)
    	at EchoServer$ClientServiceThread.run(EchoServer.java:173)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: After End Of Result set error?

    Which line is line #95 in your code?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default Re: After End Of Result set error?

    ok so it is this "returnstring += "username="+rs.getString("uname") ... "

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: After End Of Result set error?

    Quote Originally Posted by mrhid6 View Post
    ok so it is this "returnstring += "username="+rs.getString("uname") ... "
    Are you fetching those columns from left to right in database table order? Some RDBMSs don't like it when you fetch columns from a row out of order.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default Re: After End Of Result set error?

    ok i have altered some of the code in order of left to right

    Java Code:
       public String getonlineplayer(int mapid,int pid){
            String returnstring ="";
            if(mapid>0 && pid!=0){
                try{
                    pst = con.prepareStatement("select * from players where areaid='"+mapid+"' and online='1' and ID!='"+pid+"'",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                    rs = pst.executeQuery();
                    if(rs.next()){
                        do{
                            if(rs!=null){
                                
                                String uname = rs.getString("uname");
                                String pimage = rs.getString("playerimage");
                                String xpos = rs.getString("xpos");
                                String ypos = rs.getString("ypos");
                                
                                returnstring += "username="+uname+
                                ",xpos="+xpos+",ypos="+ypos+",playerimage="+pimage+"#";
                            }else{
                                break;
                            }
                        }while(rs.next());
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            return returnstring;
        }
    its giving me line 97 which is "String pimage = rs.getString("playerimage");"

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: After End Of Result set error?

    Quote Originally Posted by mrhid6 View Post
    its giving me line 97 which is "String pimage = rs.getString("playerimage");"
    Same exception? Is there another dbms client that is updating your table?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: After End Of Result set error?

    before while loop, try to put rs.beforeFirst().

    Also remove the if condition before you do while loop because you are skipping one record by doing that.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: After End Of Result set error?

    Quote Originally Posted by mine0926 View Post
    before while loop, try to put rs.beforeFirst().

    Also remove the if condition before you do while loop because you are skipping one record by doing that.
    The beforeFirst() call should't be needed; from the API documentation of the next() method:

    Moves the cursor froward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.
    The first call to next() positions the ResultSet at the first row (if any).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default Re: After End Of Result set error?

    ok i have done all the changes you said but nothing still error forgot to say that the function is in a thread which is contanstly calling the method if that help? here is code

    Java Code:
       public String getonlineplayer(int mapid,int pid){
            String returnstring ="";
            if(mapid>0 && pid!=0){
                try{
                    pst = con.prepareStatement("select * from players where areaid='"+mapid+"' and online='1' and ID!='"+pid+"'",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                    rs = pst.executeQuery();
                    rs.beforeFirst();
                    while(rs.next()){
    
                        String uname = rs.getString("uname");
                        int pimage = rs.getInt("playerimage");
                        int  xpos = rs.getInt("xpos");
                        int ypos = rs.getInt("ypos");
    
                        returnstring += "username="+uname+
                        ",xpos="+Integer.toString(xpos)+",ypos="+Integer.toString(ypos)+",playerimage="+Integer.toString(pimage)+"#";
                    }
                }catch(Exception e){
                    e.printStackTrace();
                    System.out.println("["+pid+"]Online Players Error While Validating "+e);
                }
            }
            return returnstring;
        }

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: After End Of Result set error?

    Quote Originally Posted by mrhid6 View Post
    ok i have done all the changes you said but nothing still error forgot to say that the function is in a thread which is contanstly calling the method if that help?
    Sure, that helps; make that method a synchronized method so that it only runs once at a certain moment in time.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default Re: After End Of Result set error?

    Quote Originally Posted by JosAH View Post
    Sure, that helps; make that method a synchronized method so that it only runs once at a certain moment in time.
    thanks for reply and how can i do this?

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: After End Of Result set error?

    Quote Originally Posted by mrhid6 View Post
    thanks for reply and how can i do this?
    Java Code:
    synchronized public String getonlineplayer(int mapid,int pid) { ... }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: After End Of Result set error?

    Since this is multi-threaded I hope that your statment and resultset objects are local to this method.

    Since it is clear they are not I would suggest you change that, as that is the cause (almost certainly) of your problem.
    Synchronising this method will only be a sticky-tape fix, as you'll use up your db cursors before too long.

  14. #14
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default Re: After End Of Result set error?

    Ok got it sorted did what u said about the synchronized method and it has solved my problems thanks all! :)

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: After End Of Result set error?

    That isn't the solution.
    You've merely hidden the real bug, which is sharing your resources like that..

Similar Threads

  1. Keep getting result of 0.
    By dookie1293 in forum New To Java
    Replies: 7
    Last Post: 06-09-2011, 06:01 AM
  2. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 11:15 AM
  3. uncorrect result
    By jamborta in forum New To Java
    Replies: 3
    Last Post: 11-11-2009, 02:17 PM
  4. Replies: 2
    Last Post: 03-18-2009, 09:36 PM
  5. getting a random result
    By gradon in forum New To Java
    Replies: 2
    Last Post: 07-19-2007, 04:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •