ResultSet Format

Discussion in 'Plugin Development' started by TehVoyager, Nov 24, 2013.

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

    TehVoyager

    I have a MySQL table that looks like this:
    Code:
    playername :: permission
    random    :: essentials.perm
    random    :: essentials.perme
    random    :: essentials.perm4
    dude    :: essentials.perm2 
    I was to get all the perms random has so i used this:
    Code:java
    1. ResultSet res = plugin.statement.executeQuery("SELECT playername FROM playerperms WHERE playername='" + player.getName() + "'");

    My Question is how would I go through all the the Permissions as a result set and add them with:
    Code:java
    1. player.addAttachment(plugin, ("next Perm looped through here"), true);

    Thanks

    I can provide more code if needed.
     
  2. Offline

    jackwilsdon

    From what you are saying, I think you want to iterate the ResultSet, correct?
    If so, you will need to use ResultSet#next(), as can be seen in this code example

    Code:java
    1. while (res.next()) {
    2. String permission = res.getString("YOUR FIELD NAME");
    3. player.addAttachment(plugin, permission, true);
    4. }
     
  3. Offline

    TehVoyager

    What do you mean your field name?
    Would this work?
    Code:java
    1. public void loadPerms(Player player){
    2. try {
    3. ResultSet res = plugin.statement.executeQuery("SELECT playername FROM playerperms WHERE playername='" + player.getName() + "'");
    4. while(res.next()){
    5. player.addAttachment(plugin, res.toString(), true);
    6. }
    7. } catch (SQLException e) {
    8. // TODO Auto-generated catch block
    9. e.printStackTrace();
    10. }
    11. }

    jackwilsdon

    Bumped back to first page :p

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

    jackwilsdon

    I don't think just toString() would work. You would use res.getString("playername"), seeing as "playername" is the name of the field you are selecting in your query :)
     
  5. Offline

    TehVoyager

Thread Status:
Not open for further replies.

Share This Page