Queue System (SQL) doesn't return all values needed?

Discussion in 'Spigot Plugin Development' started by JordanOsterberg, May 15, 2015.

  1. I was using this queue system for a project, and this code doesn't give all of the queued values:
    PHP:
    public List<String> getQueuedPlayers(){
           
            try {
               
                String q = "SELECT `uuid` FROM `users` WHERE `queued` = true";
               
                PreparedStatement st = connection.prepareStatement(q);
               
                //st.setString(1, p.getUniqueId().toString());
               
                ResultSet rs = st.executeQuery();
               
                if (rs.next()){
                   
                   
                    String uuid = null;
                    uuid = rs.getString("uuid");
                   
                    List<String> queued = new ArrayList<String>();
                   
                    queued.add(uuid);
                   
                    rs.close();
                    st.close();
                   
                    return queued;
                }
               
            } catch (Exception e){
                e.printStackTrace();
            }
           
            return null;
        }
    In this case, two people have the queued boolean in the database set as true. It only gives me one of the player's uuids when I use this method.
    Thank you!
     
  2. gigosaurus

    Supporter

    You need to make a loop and keep checking if rs.next() is true. Every time it is, add rs.getString to your queued list. Once rs.next() is false, close everything and return the list.
     
  3. Use
    Code (Text):

    while(rs.next) { //code her
     
  4. @gigosaurus @Gsoares1928, Using that creates this error:
    Code (Text):
    java.sql.SQLException: Before start of result set
    [17:17:08 WARN]:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    [17:17:08 WARN]:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    [17:17:08 WARN]:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    [17:17:08 WARN]:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    [17:17:08 WARN]:     at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
    [17:17:08 WARN]:     at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5656)
    [17:17:08 WARN]:     at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5576)
    [17:17:08 WARN]:     at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5616)
    [17:17:08 WARN]:     at com.hidden.hidden.utils.SQL.getQueuedPlayers(SQL.java:93)
    Current Code:
    PHP:
    public List<String> getQueuedPlayers(){
           
            List<String> queued = new ArrayList<String>();
           
            try {
               
                String q = "SELECT `uuid` FROM `users` WHERE `queued` = true";
               
                PreparedStatement st = connection.prepareStatement(q);
               
                //st.setString(1, p.getUniqueId().toString());
               
                ResultSet rs = st.executeQuery();
                do {
                    String uuid = null;
                    uuid = rs.getString("uuid");
                   
                   
                    queued.add(uuid);
                   
                   
                } while (rs.next());
                   
                rs.close();
                st.close();
               
                    return queued;
               
               
            } catch (Exception e){
                e.printStackTrace();
            }
           
            return null;
        }
     
  5. Use a simple while loop instead a do-while
     
  6. I actually found the error,
    I was just using a loop without checking if rs.next() first. Thank you for your help!
     
  7. Which you wouldn't have to do if you had just used a simple while loop rather than a do-while loop. Much simpler, isn't it?