MySQL, Getting 'top stats'

Discussion in 'Plugin Development' started by njb_said, Aug 3, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    njb_said

    I am having an issue with my plugin, I am still new to MySQL
    I have tried a few different ways of doing this:
    I am trying to get the top 10 stats from my table. I know how to do this in PHP but not in bukkit.
    Here is my current 'get' code:
    Code:
    public String[] getTop(String type, int amount) {
            String[] array = new String[amount];
            try {
                Statement s = c.createStatement();
                ResultSet r = s.executeQuery("SELECT * FROM zombie_stats ORDER BY CONVERT(" + type.toLowerCase() + ", UNSIGNED INTEGER) DESC LIMIT " + amount);
                if (r.next()) {
                    array[amount - 1] = r.getString("username") + "." + r.getInt(type.toLowerCase());
                 
                }
            }catch(SQLException e) { }
            return array;
        }
    After that I have in my "/stats top kills" command.
    Code:
    Zombies.getZombies().chat.sendMessage(player, "&aTop 10 Stats &7(Kills)");
                        for (String top : Zombies.getZombies().db.getTop("kills", 10)) {
                            String[] array = top.split(".");
                            Zombies.getZombies().chat.sendMessage(player, "&6" + array[0] + ": &7" + array[1]);
                        }
    It gives me a NPE (NullPointerException) at top.split(".")

    I'm hoping this makes sense and hope someone can help. Thanks
     
  2. Offline

    Pew446

    What you're doing right now is selecting say, 10 results, then all you do is get data from the first result, and you set it to amount -1 (10 -1 = 9).. so your array would look like {null, null, null, null, null, null, null, null, null, "blah.blah"}

    This obviously isn't what you want, so what you should do is while(r.next()) instead of if(r.next())
    Also you will need a counter that you increase each time the while loop ends so that you put the data in the right place in the array. ( array[counter] = r.getString("username") + "." + r.getInt(type.toLowerCase()) )

    This might work for you:

    Code:
    public String[] getTop(String type, int amount) {
    String[] array = new String[amount];
    try {
    Statement s = c.createStatement();
    ResultSet r = s.executeQuery("SELECT * FROM zombie_stats ORDER BY CONVERT(" + type.toLowerCase() + ", UNSIGNED INTEGER) DESC LIMIT " + amount);
    int counter = 0;
    while(r.next()) {
    array[counter] = r.getString("username") + "." + r.getInt(type.toLowerCase());
    counter++;
    }
    }catch(SQLException e) { }
    return array;
    }
    When you do r.next() it is only grabbing the next row in the ResultSet, not all rows. Also make sure you close your ResultSet if you plan on using it again. :)
     
  3. Offline

    njb_said

    Thanks, I'll try this now, like I said im not great with mysql, I have it do while() in my PHP so it makes sense ill try what you posted now :)

    Pew446 - Thanks
    http://puu.sh/3TuLx.png
    Im going to use this for a leaderboard (literally). Using signs updating from the servers stats cache.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
    thecrystalflame likes this.
  4. Offline

    Pew446

    Nice! Glad I could help.
     
  5. Offline

    the_ceed

Thread Status:
Not open for further replies.

Share This Page