Solved [MySQL] Getting 'Top Players'

Discussion in 'Plugin Development' started by minelazz, Dec 3, 2013.

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

    minelazz

    I am kind of new to MySQL and wonder how i can get the top 3 scores and then the players name.

    Her is my code so far

    Code:java
    1. public static String[] getTopPlayers() throws SQLException
    2. {
    3. String[] result = new String[3];
    4. Statement s = c.createStatement();
    5. if(s != null)
    6. {
    7. ResultSet rs = s.executeQuery("SELECT PlayerName FROM `SkyWars_UserData` ORDER BY `Credits` LIMIT 3;");
    8. for(int i = 0; i < 3; i++)
    9. {
    10. if(rs.next())
    11. {
    12. result[i ] = rs.getString("PlayerName");
    13. }
    14. else // Set it to ""
    15. {
    16. Bukkit.getServer().getConsoleSender().sendMessage("[SkyWars]" + ChatColor.RED + "ELSE");
    17. result[i ] = "";
    18. }
    19. }
    20. }
    21. return result;
    22. }
    23.  
     
  2. Offline

    amhokies

    minelazz
    You'll want to loop through the ResultSet however many times you want to. If you want the top three, you'll need to loop through three times.

    EDIT: Sorry, misread the code because the format is off. What is wrong with what you have? It looks like it should work.
     
  3. Offline

    calebbfmv

    Look at this snippet:
    Code:
        public static HashMap<String, Double> getMaxCredits() {
           HashMap<String, Double> highest = new HashMap<String, Double>();
            MySQL sql = Main.sql;
            ResultSet res = sql.querySQL("SELECT * FROM Credits MAX");
            try {
                if (res.next()) {
                    String playername = res.getString("Player");
                    double balance = res.getDouble("Balance");
                    highest.put(playername, balance);
                    res.close();
                } else {
                    res.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return highest;
        }
    }
    
     
  4. Offline

    amhokies

    All of that looks alright, but you should probably close the ResultSet in a finally block. You won't have to put it there twice, you can get ride of the else statement, and the ResultSet will close even if an exception is thrown in your try block.
     
  5. Offline

    minelazz

    amhokies and calebbfmv thanks alot! Tried the code, but i still can't get it to work. The code return string "java.lang.String;@c7c08f" :(
     
  6. Offline

    amhokies

    Where are you trying to print the results? Looks like you're trying to print the entire array at once instead of each individual String object.
     
  7. Offline

    calebbfmv

    It was just a snipet, must be run in a for loop.
     
  8. Offline

    minelazz

    Yes that was that i did, but tried also "getMaxCredits().get(0)" and "getMaxCredits().size()" it just return the same. This is the code i used

    Code:java
    1. //The table
    2. statement.execute("CREATE TABLE SkyWars_UserData (PlayerName VARCHAR(40), Credits int, SW_Score int, Snowballs int, Enderpearls int, SW_Kit1 int, SW_Kit2 int, SW_Kit3 int, SW_Kit4 int);");
    3.  
    4. //Top score method
    5. public static HashMap<String, Double> getMaxCredits() throws SQLException {
    6. HashMap<String, Double> highest = new HashMap<String, Double>();
    7. try {
    8. MySQL MySQL = new MySQL(plugin, "mysqleu.envioushost.com", "3306", "xxxx", "xxxxx", "xxxxxxx");
    9.  
    10. ResultSet res = MySQL.querySQL("SELECT * FROM Credits MAX");
    11.  
    12. if (res.next()) {
    13. String playername = res.getString("PlayerName");
    14. double balance = res.getDouble("SW_Score");
    15. highest.put(playername, balance);
    16. res.close();
    17. }
    18. res.close();
    19. } catch (SQLException e) {
    20. e.printStackTrace();
    21. }
    22. return highest;
    23. }
    24.  
    25. //used this onEnable()
    26. Bukkit.getServer().getConsoleSender().sendMessage("[SkyWars]" + ChatColor.RED + getMaxCredits.get(0));
    27.  


    EDIT; Got it finally to work! Using list instead of HashMap now :)
    Thanks a lot for the help guys!

    Here is my new code;

    Code:java
    1.  
    2. public static List<String> getTopPlayers() throws SQLException{
    3. List<String> result = new ArrayList<String>();
    4. Statement s = c.createStatement();
    5. if(s != null){
    6. ResultSet rs = s.executeQuery("SELECT PlayerName FROM `SkyWars_UserData` ORDER BY `Credits`");
    7. rs.findColumn("PlayerName");
    8. if(rs.last()){
    9. result.add(rs.getString("PlayerName"));
    10. }if(rs.previous()){
    11. result.add(rs.getString("PlayerName"));
    12. }if(rs.previous()){
    13. result.add(rs.getString("PlayerName"));
    14. }
    15. }
    16. return result;
    17. }
    18.  
     
  9. Offline

    calebbfmv

    What mine does, when properly executed, is out print the names AND their values
     
Thread Status:
Not open for further replies.

Share This Page