mySQL Database issues

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

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

    MCCoding

    I have made myself a mySQL database with is working fine when i save the players name in when they join the server, but what i'm also wanting to add the players IP in the database as well. When i add sql.setInt(1, event.getPlayer.getAddress().getHostNAme()); it's a string but i need it to be an int so it keeps telling me to change sql.setInt to sql.setSting which wont work. Here is my code i may have some things wrong as this is my first time saving things to databases.

    Code:java
    1. @EventHandler
    2. public void onSQLConnection(PlayerJoinEvent event) {
    3. openConnection();
    4.  
    5. try {
    6.  
    7. if(playerDataContainsPlayer(event.getPlayer())) {
    8. PreparedStatement sql = connection.prepareStatement("SELECT Username FROM `Player_Data` WHERE Username=?;");
    9. sql.setString(1, event.getPlayer().getName());
    10.  
    11. ResultSet result = sql.executeQuery();
    12. result.next();
    13.  
    14. PreparedStatement IP = connection.prepareStatement("SELECT IP FROM `Player_Data` WHERE IP=?;");
    15. IP.setInt(1, event.getPlayer().getAddress().getHostName());
    16.  
    17. result.close();
    18. sql.close();
    19.  
    20.  
    21.  
    22.  
    23. } else {
    24.  
    25. PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO `Player_Data` values(?,?);");
    26. newPlayer.setString(1, event.getPlayer().getName());
    27. newPlayer.execute();
    28. newPlayer.close();
    29.  
    30. }
    31.  
    32. } catch (Exception e) {
    33. e.printStackTrace();
    34.  
    35. } finally {
    36. closeConnection();
    37. }
    38. }
     
  2. Offline

    MineDoubleSpace

    MCCoding
    First of all, IP is a String.
    And your MySQL Query isn't correct.
     
  3. Offline

    MCCoding

    MineDoubleSpace
    Yeah i got it to work, whats wrong with the MySQL Query?
     
  4. Offline

    MineDoubleSpace

    @MCCodingn "SELECT IP FROM `Player_Data` WHERE IP=?;" because you are updating the ip where ip == to that ip. You should check for the player
     
  5. Offline

    MCCoding

    MineDoubleSpace
    How would I do that I'm not to sure with databases, but I have got it working with no errors

    MineDoubleSpace

    This is my updated code, It's setting the IP fine also i'm making a item menu where i want to set a sting depending on which item they choose i have started the code of that but it does not work at all here is that code as well. If you can help with any of this its much appreciated!

    Updated code:

    Code:java
    1. @EventHandler
    2. public void onSQLConnection(PlayerJoinEvent event) {
    3. openConnection();
    4.  
    5. try {
    6.  
    7. if(playerDataContainsPlayer(event.getPlayer())) {
    8. PreparedStatement sql = connection.prepareStatement("SELECT Username FROM `Player_Data` WHERE Username=?;");
    9. sql.setString(1, event.getPlayer().getName());
    10.  
    11. ResultSet result = sql.executeQuery();
    12. result.next();
    13.  
    14. PreparedStatement IP = connection.prepareStatement("UPDATE `Player_Data` SET IP=? WHERE Username=?");
    15. IP.setString(1, event.getPlayer().getAddress().getHostName());
    16. IP.setString(2, event.getPlayer().getName());
    17. IP.executeUpdate();
    18.  
    19. IP.close();
    20. result.close();
    21. sql.close();
    22.  
    23. } else {
    24.  
    25. PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO `Player_Data` values(?,?,?);");
    26. newPlayer.setString(1, event.getPlayer().getName());
    27. newPlayer.execute();
    28. newPlayer.close();
    29. }
    30.  
    31. } catch (Exception e) {
    32. e.printStackTrace();
    33.  
    34. } finally {
    35. closeConnection();
    36. }
    37. }


    Item Menu code:

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent e) {
    3. Player player = (Player)e.getWhoClicked();
    4.  
    5. if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
    6. if (e.getCurrentItem().getItemMeta() == null) return;
    7. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(ChatColor.GREEN+ "Villager")) {
    8. e.setCancelled(true);
    9.  
    10. SQL.SQLConnection.openConnection();
    11.  
    12. try {
    13.  
    14. if(SQL.SQLConnection.playerDataContainsPlayer(player)) {
    15. PreparedStatement sql = SQL.SQLConnection.connection.prepareStatement("SELECT Username FROM `Player_Data` WHERE Class=?;");
    16. sql.setString(1, "Villager");
    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.execute();
    24. newPlayer.close();
    25. }
    26.  
    27. } catch (Exception ex) {
    28. ex.printStackTrace();
    29.  
    30. } finally {
    31. SQL.SQLConnection.closeConnection();
    32. }


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

    MineDoubleSpace

    MCCoding
    Code:java
    1. PreparedStatement sql = SQL.SQLConnection.connection.prepareStatement("UPDATE `Player_Data` SET Class=? WHERE Username=?");
    2. sql.setString(1, "Villager");
    3. sql.setString(2, event.getPlayer().getName());
    4. sql.executeUpdate();
    5. sql.close();
     
  7. Offline

    Neilnet

    1. PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO `Player_Data` values(?,?,?);");
    2. newPlayer.setString(1, event.getPlayer().getName());

      You only have 1 setstring when there are 3 '?' characters.
     
  8. Offline

    MCCoding

    Neilnet

    I have 3 strings one is the players name another is the players IP and another is the players rank.
     
Thread Status:
Not open for further replies.

Share This Page