Iterating the names of a MySQL database

Discussion in 'Plugin Development' started by Pitazzo, Jul 12, 2014.

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

    Pitazzo

    Hey guys,
    I've a MySQL database with a lot of information of the players of my server. I'd like to iterate the name of all those players, but I'm not sure about how to do it. I've writen this piece of code:

    Code:java
    1. public static synchronized Array getNames(){
    2. try{
    3. PreparedStatement sql = connection
    4. .prepareStatement("SELECT name FROM `bukkit`.`user_data`;");
    5.  
    6. ResultSet result = sql.executeQuery();
    7. result.next();
    8. Array names = result.getArray("name");
    9. sql.close();
    10. result.close();
    11. return names;
    12. }catch (Exception e) {
    13. e.printStackTrace();
    14. }
    15.  
    16. return null;
    17.  
    18.  
    19. }


    ERROR: "get cannot be resolved or is not a field"

    And then...
    Code:java
    1. public void anemiaCheck(){
    2. for(String player : MySQL.getNames()){
    3.  
    4. }
    5. }


    ERROR: "Can only iterate over an array or an instance of java.lang.Iterable"

    Any idea?

    Thanks in advance ;)
     
  2. Offline

    Dealyise

    Pitazzo This should work, not 100% sure.
    Code:java
    1. public static synchronized Array getNames(){
    2. try{
    3. PreparedStatement sql = connection
    4. .prepareStatement("SELECT name FROM `bukkit`.`user_data`;");
    5.  
    6. ResultSet result = sql.executeQuery();
    7. while(result.next()){
    8. Array names = result.getArray("name");
    9. }
    10. sql.close();
    11. result.close();
    12. return names;
    13. }catch (Exception e) {
    14. e.printStackTrace();
    15. }
    16.  
    17.  
    18.  
    19. }
     
Thread Status:
Not open for further replies.

Share This Page