Getting Strings from mySQL

Discussion in 'Plugin Development' started by MCCoding, Oct 31, 2013.

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

    MCCoding

    I'm wanting to get a sting from my data base that i have set this is as far as i got until i got confused and was not sure on what i was doing, also if you could give a brief explanation on what the playing is doing so i'm not just copying and pasting code i would really appreciate it.

    So exactly what i'm wanting to is make a method that will get the playrs rank and check it so for example if the players rank is intermediate send them a message You are in the intermediate rank etc.


    Database:

    +------------+-----------+---------------------+
    | Username | IP | RANK | |
    +------------+-----------+---------------------+
    | MCCoding | 0.0.0.0 | Noob | |
    +------------+-----------+---------------------+

    Current code:

    Code:java
    1. public static void rankIntermediate(Player player) {
    2.  
    3. try {
    4.  
    5. PreparedStatement sql = SQL.SQLConnection.connection.prepareStatement("SELECT * FROM `Player_Data` WHERE Username = '" + player.getName() + "';");
    6. ResultSet result = sql.executeQuery();
    7. result.next();
    8.  
    9. if(result.getString("Rank") != null) {
    10.  
    11. }
    12.  
    13.  
    14. sql.close();
    15. result.close();
    16.  
    17. } catch(Exception e) {
    18. e.printStackTrace();
    19. } finally {
    20. SQL.SQLConnection.closeConnection();
    21. }
    22. }
     
  2. Offline

    CubieX

    What's your problem? You almost got it.
    Just insert your if() statement to compare if the string form the result set equals your rank to check against.

    BTW: If you only need the rank here, better do not use "*". State the wanted column name directly in your SELECT statement, so you only get this column entry and not a bunch of unnecessary data in your result set.
     
    MCCoding likes this.
  3. Offline

    MCCoding

    CubieX
    That's the thing i don't know how to compare it to see if it equals the rank name.
     
  4. Offline

    CubieX

    I guess you know how to compare two strings.
    So what you really mean is, you do not know how to get the current player rank, to be precise?
    If so, you could use the API of your permission plugin (PermissionsEx for example) or you can use Vault.
    The latter has the advantage that your plugin will work with every common permissions plugin.
    Because you cannot know which plugin other server owners use.
    (only applies, if you are going to release this plugin later)

    PEX API documentation is here.
    Vault is here (with example how to hook into it)
     
  5. Offline

    MCCoding

    CubieX
    I made my own ranking system which saves the players rank to the database, but what I'm wanting to do is make it so you can't re-rank someone so I had to add a check if they are in the rank which what I'm trying to do. So this method I'm trying to make will check the players rank if it is the intermideate rank, if they are when I set a players rank it won't reset it as they are alreay in a rank.
     
  6. Offline

    CubieX

    Then I do not understand what your problem is.
    You can already read the current player rank from your DB and you can compare this rank (which is a string) to any other rank using .equals().
    So what exactly troubles you?
     
  7. Offline

    MCCoding

    CubieX
    So the code posted works? I must be getting errors off something else then I will show you u other code to see of you can find something wrong but in not on my computer to post it in will add
    It later
     
  8. Offline

    CubieX

    I think the way you get your rank from DB should work.
    If there are errors, post the whole stack trace and the method or class where the error occurs.
     
    MCCoding likes this.
  9. Offline

    MCCoding

    This is why whole class on where i must be getting the error from here is the code i have,

    Class:

    Code:java
    1. public class SQLConnection implements Listener {
    2.  
    3. public static Connection connection;
    4.  
    5. public synchronized static void openConnection() {
    6. try {
    7. connection = DriverManager.getConnection("Dont need to know this");
    8. } catch(Exception e) {
    9. e.printStackTrace();
    10. }
    11. }
    12.  
    13. public synchronized static void closeConnection() {
    14. try {
    15. connection.close();
    16. } catch(Exception e) {
    17. e.printStackTrace();
    18. }
    19. }
    20.  
    21. public synchronized static boolean playerDataContainsPlayer(Player player) {
    22. try {
    23. PreparedStatement sql = connection.prepareStatement("SELECT * FROM `Player_Data` WHERE Username=?;");
    24. sql.setString(1, player.getName());
    25. ResultSet restultSet = sql.executeQuery();
    26.  
    27. boolean containsPlayer = restultSet.next();
    28.  
    29. sql.close();
    30. restultSet.close();
    31.  
    32. return containsPlayer;
    33.  
    34. } catch(Exception e) {
    35. e.printStackTrace();
    36. return false;
    37. }
    38. }
    39.  
    40. @EventHandler
    41. public void onSQLConnection(PlayerJoinEvent event) {
    42. openConnection();
    43. try {
    44.  
    45. if(playerDataContainsPlayer(event.getPlayer())) {
    46. PreparedStatement sql = connection.prepareStatement("SELECT IP FROM `Player_Data` WHERE Username=?;");
    47. sql.setString(1, event.getPlayer().getName());
    48.  
    49. ResultSet result = sql.executeQuery();
    50. result.next();
    51.  
    52. result.getString("IP");
    53.  
    54. PreparedStatement ipUpdate = connection.prepareStatement("UPDATE `Player_Data` SET IP=? WHERE Username=?");
    55. ipUpdate.setString(1, event.getPlayer().getAddress().getHostName());
    56. ipUpdate.setString(2, event.getPlayer().getName());
    57. ipUpdate.executeUpdate();
    58.  
    59. ipUpdate.close();
    60. sql.close();
    61. result.close();
    62.  
    63. } else {
    64.  
    65. PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO `Player_Data` VALUES (?,?,?);");
    66. newPlayer.setString(1, event.getPlayer().getName());
    67. newPlayer.setString(2, event.getPlayer().getAddress().getHostName());
    68. newPlayer.setString(3, "Beginner");
    69. newPlayer.execute();
    70. newPlayer.close();
    71. }
    72.  
    73. } catch (Exception e) {
    74. e.printStackTrace();
    75. System.out.println("Error setting mySQL data.");
    76.  
    77. } finally {
    78. closeConnection();
    79. }
    80. }
    81. }
    82.  


    Error:

    Code:
    [SEVERE] com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'IP' at row 1 
    If you need more of the error let me know its just that in the server.txt its messy and complicated to follow along with.

    CubieX
    Also when i try and use the rank check method i made it keeps saying to change void to boolean here is how i'm getting the method as well.

    Code:
    if(SQL.SQLMethods.rankIntermediate(player)) {
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  10. Offline

    CubieX

    How did you define the column "IP"?
    Perharps you used the wrong type or the length is too short for storing an IP adress. A v4 IP can be upt to 15 characters long.

    I guess .rankIntermediate() does not return a boolean.
    Can you show us this method, please?
     
  11. Offline

    MCCoding

    CubieX

    here is the method

    Code:java
    1. public static void rankIntermediate(Player player) {
    2. SQL.SQLConnection.openConnection();
    3.  
    4. try {
    5.  
    6. PreparedStatement sql = SQL.SQLConnection.connection.prepareStatement("SELECT RankFROM `Player_Data` WHERE Username = '" + player.getName() + "';");
    7. ResultSet result = sql.executeQuery();
    8. result.next();
    9.  
    10. if(result.getString("Class") != null && result.equals(Intermediate")) {
    11.  
    12. }
    13.  
    14.  
    15. sql.close();
    16. result.close();
    17.  
    18. } catch(Exception e) {
    19. e.printStackTrace();
    20. } finally {
    21. SQL.SQLConnection.closeConnection();
    22. }
    23. }
     
  12. Offline

    CubieX

    .rankIntermediate() does not return a boolean. So your if() statement will not work. (as eclipse says)
    Just call .rankIntermediate().

    And in this method in line 10 you forgot a " before the word "Intermediate".
     
  13. Offline

    d49s7

    I've been working on the same problem all weekend and finally got a solution. Try this.
    My changes are the added lines: line 6, line 12, and line 14.


    Code:java
    1. public static void rankIntermediate(Player player) {
    2. SQL.SQLConnection.openConnection();
    3.  
    4. try {
    5.  
    6. Object rank = 0;
    7.  
    8. PreparedStatement sql = SQL.SQLConnection.connection.prepareStatement("SELECT RankFROM `Player_Data` WHERE Username = '" + player.getName() + "';");
    9. ResultSet result = sql.executeQuery();
    10. result.next();
    11.  
    12. rank = result.getString("Rank");
    13.  
    14. if(rank.equals("Intermediate")) {
    15.  
    16. }
    17.  
    18.  
    19. sql.close();
    20. result.close();
    21.  
    22. } catch(Exception e) {
    23. e.printStackTrace();
    24. } finally {
    25. SQL.SQLConnection.closeConnection();
    26. }
    27. }
     
    MCCoding likes this.
  14. Offline

    MCCoding

    d49s7
    Ah okay thanks, just one question what does this do
    Code:java
    1. Object rank = 0;
    ? Also it's still wanting me to change the the return type on rankIntermediate(..) to `Boolean`
     
  15. Offline

    d49s7

    The Object rank = 0; just has to be there to make lines 12 and 14 work. I couldn't really tell you more as I'm just learning this stuff. It wont work without that though. When I pasted the code it didnt ask me to change the return type to Boolean so I don't know on that. Try changing it and seeing if it works I guess.
     
  16. Offline

    MCCoding

    d49s7
    It's sort of working here is when i check if the player is in the rankIntermediate code, In the Database i made it if the player has no rank it will set it to noob in the Rank column but when i do the check it says that yo already have a rank if it does not equal Intermediate for some reason.

    Code:java
    1.  
    2.  
    3. SQL.SQLConnection.openConnection();
    4.  
    5. try {
    6.  
    7. if(SQL.SQLMethods.rankIntermediate (player)) {
    8. player.sendMessage(ChatColor.RED + "You already have a rank.");
    9.  
    10. } else {
    11.  
    12. if(SQL.SQLConnection.playerDataContainsPlayer(player)) {
    13. PreparedStatement sql = SQL.SQLConnection.connection.prepareStatement("UPDATE `Player_Data` SET Rank=? WHERE Username = '" + player.getName() + "';");
    14. sql.setString(1, "Intermediate ");
    15.  
    16. sql.executeUpdate();
    17. sql.close();
    18.  
    19. } else {
    20.  
    21. PreparedStatement newPlayer = SQL.SQLConnection.connection.prepareStatement("INSERT INTO `Player_Data` VALUES (?,?,?);");
    22. newPlayer.setString(1, player.getName());
    23. newPlayer.setString(2, player.getAddress().getHostName());
    24. newPlayer.setString(3, "noob");
    25. newPlayer.execute();
    26. newPlayer.close();
    27. }
    28. }
    29.  
    30. } catch (Exception exc) {
    31. exc.printStackTrace();
    32. System.out.println("Error setting mySQL data.");
    33.  
    34. } finally {
    35. SQL.SQLConnection.closeConnection();
    36. }
     
  17. Offline

    d49s7

    it looks like you insert the players into the DB when they first login, and set their rank to "beginner". So whenever you check to see if a player has any rank it is always going to say the "You already have a rank". This coming from your code on post #9
     
  18. Offline

    MCCoding

    d49s7
    So how would i fix? so i have to make another method to check if is beginner then set it?
     
  19. Offline

    d49s7

    You'll probably need to be able to check which rank anybody is for each rank I think. What are you checking the rank for? and what lets players get a higher rank? I believe you would only need one method to check what the players rank is. then depending on the result you'd have different actions.
     
  20. Offline

    MCCoding

    d49s7
    I'm checking the players rank so they can do certain things in Events, i have made multiple methods that check for all of the ranks but how would i make it set over the Beginner? as it keeps saying You already have a rank
     
  21. Offline

    d49s7

    Whenever you want to change a players rank you just need to make an UPDATE line to change the rank in the DB under whatever conditions call for changing the rank. It looks like you're trying to Update the player rank to intermediate if they don't already have a rank? But since you assign the beginner rank on login, no matter what everytime you do the check for if there is a rank, it will always say You already have a rank.
     
  22. Offline

    MCCoding

    d49s7
    So don't set the rank on the login? and then when they select the rank thats when i update
     
  23. Offline

    d49s7

    Or set the rank as null instead of beginner on login. So whenever you want to change a players rank just check if rank == null. If it is then update to the rank you want. Then if the rank isn't null then the rank wont be changed.
     
  24. Offline

    MCCoding

    d49s7
    Thanks for the help, bu how would i check if its null? i have made it so when a player joins it sets the Rank to null, and also i'm getting a error when a players signs in it says Data too long for for column `IP` at row 1
     
  25. Offline

    d49s7

    Same way you would have checked if the rank was intermediate. In your database, you might need to change the length of the IP column. The column type should to int(x) where x is some number. you can change that number to be larger which means it will be able to fit more data into the column.

    Basically, instead of having a bunch of methods to check what a players rank is, you just need 1 method which checks if the rank is null or not. If the rank is null then you can change it to intermediate or any other rank. If the check says the rank is not null, then you wont be able to change the rank. If thats what you want.

    If you still need some help tomorrow pm me your skype info and we can chat there. Should be able to fix it quicker that way. I'll check back in tomorrow evening. Or we can just use an IRC if you'd rather not use skype
     
    MCCoding likes this.
  26. Offline

    Coelho

    FYI, you should be using SQL like this:
    Code:
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
      connection = ...
      statement = connection.prepareStatement("SELECT 1 as blah FROM otherstuff WHERE 1 ORDER BY blah ASEC LIMIT 1;");
      resultSet = statement.executeQuery();
      if(resultSet.next()) {
        return resultSet.getInt("blah");
      }
    } catch(Exception exception) {
      exception.printStackTrace();
    } finally {
      if(connection != null) {
        try {
          ...
        } catch(Exception exception) {
          // ignore
        }
      }
      if(statement != null) {
        try {
          statement.close();
        } catch(Exception exception) {
          // ignore
        }
      }
      if(resultSet != null) {
        try {
          resultSet.close();
        } catch(Exception exception) {
          // ignore
        }
      }
    }
    return -1;
    
    EDIT: Error handling, gotta love it.
     
Thread Status:
Not open for further replies.

Share This Page