MySQL creating multiple columns with same name

Discussion in 'Plugin Development' started by elementalgodz11, Jun 9, 2014.

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

    elementalgodz11

    I am trying to create a column for a player uuid that logs their kills, deaths and joins:

    I am having trouble with this as it creating a different column for each type (kills/deaths, etc) as shown below in this screenshot:
    Screenshot_1.png


    The following is my code:

    Updating the statistic of a player:
    Code:java
    1. public void updatePlayer(Player player, StatType type) throws SQLException {
    2.  
    3. String player_uuid = player.getUniqueId().toString();
    4.  
    5. if (!(this.plugin.getMySql().checkConnection())) {
    6.  
    7. this.plugin.getMySql().openConnection();
    8.  
    9. }
    10.  
    11. Statement statement = plugin.getMySql().getConnection().createStatement();
    12.  
    13. int amount = this.getData(player, type);
    14.  
    15. if (amount != 0) {
    16.  
    17. statement.executeUpdate("UPDATE `player_data` SET `" + type.getName() + "`='" + (amount + 1) + "' WHERE `Name`='" + player_uuid + "';");
    18.  
    19. } else {
    20.  
    21. statement.executeUpdate("INSERT INTO `player_data` (`Name`, `" + type.getName() + "`) VALUES ('" + player_uuid + "', '1');");
    22.  
    23. }
    24.  
    25. }


    getData() method used for updatePlayer():
    Code:java
    1. public int getData(Player player, StatType type) throws SQLException {
    2.  
    3. String player_uuid = player.getUniqueId().toString();
    4.  
    5. if (!(this.plugin.getMySql().checkConnection())) {
    6.  
    7. this.plugin.getMySql().openConnection();
    8.  
    9. }
    10.  
    11. Statement statement = plugin.getMySql().getConnection().createStatement();
    12.  
    13. ResultSet rs = statement.executeQuery("SELECT * FROM `player_data` WHERE `Name`='" + player_uuid + "';");
    14.  
    15. if (!(rs.next())) {
    16.  
    17. return 0;
    18.  
    19. }
    20.  
    21. return rs.getInt(type.getName());
    22.  
    23. }


    Setting up the database onEnable():
    Code:java
    1. public void setupDatabase() throws SQLException {
    2.  
    3. Statement statement = plugin.getMySql().getConnection().createStatement();
    4. statement.executeUpdate("CREATE TABLE IF NOT EXISTS `player_data` (`Name` varchar(36), `" + StatType.JOINS.getName() + "` int);");
    5. statement.executeUpdate("CREATE TABLE IF NOT EXISTS `player_data` (`Name` varchar(36), `" + StatType.DEATHS.getName() + "` int);");
    6. statement.executeUpdate("CREATE TABLE IF NOT EXISTS `player_data` (`Name` varchar(36), `" + StatType.KILLS.getName() + "` int);");
    7.  
    8. }


    And then finally I set the values onPlayerJoin() printing them to the player debugging if the values are updated:

    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent event) {
    3.  
    4. Player player = event.getPlayer();
    5.  
    6. try {
    7.  
    8. updatePlayer(player, StatType.DEATHS);
    9. player.sendMessage("{" + getData(player, StatType.DEATHS) + "=" + StatType.DEATHS.getName() + "};");
    10.  
    11. updatePlayer(player, StatType.KILLS);
    12. player.sendMessage("{" + getData(player, StatType.DEATHS) + "=" + StatType.KILLS.getName() + "};");
    13.  
    14. } catch (SQLException e) {
    15.  
    16. e.printStackTrace();
    17.  
    18. }
    19.  
    20. }


    If anyone could help me this, that would be really appreciated.

    Thanks.
     
  2. Offline

    Nghtmr9999

    I don't have a ton of experience with this (and I could be totally wrong), but I believe that the ResultSet is originally pointing before the very first record. Hence, it will return false.

    Try this:
    Code:
    while(rs.next())
    {
        // get column values/do whatever you need with the record
    }
    

    Again, if I'm wrong feel free to yell at me :)
     
    kizepy17 likes this.
Thread Status:
Not open for further replies.

Share This Page