Dear MySQL...

Discussion in 'Plugin Development' started by Adriani6, Feb 4, 2014.

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

    Adriani6

    Hi,

    Need some help. I am using a MySQL database in one of my plugins, I have acknowledged that the way I use it is a bit wrong as the MySQL won't be quite correct. Basically, when a player joins I want the Database to be checked if the playername of the one who joined already exists in database and if not add a new record.

    Right now, that's what I am using (I understand what's wrong with this and why it won't be accurate)
    Code:java
    1. public void onPlayerJoinEvent(PlayerJoinEvent event) throws SQLException{
    2. Player p = event.getPlayer();
    3. p.getName();
    4. if(!p.hasPlayedBefore()) {


    Anyone able to help me check the database if the username already exists in the there while onPlayerJoinEvent ? ;p
     
  2. Offline

    Desle

    Adriani6

    Code:java
    1. public synchronized static boolean containsPlayer(Player p) {
    2. try {
    3. java.sql.PreparedStatement sql = connection.prepareStatement("SELECT * FROM `Players` WHERE Name = '" + p.getName() + "';");
    4. ResultSet resultSet = sql.executeQuery();
    5. boolean containsPlayer = resultSet.next();
    6. sql.close();
    7. resultSet.close();
    8. return containsPlayer;
    9. } catch(Exception e) {
    10. e.printStackTrace();
    11. return false;
    12. }
    13. }


    With this you can now do;
    Code:java
    1. if (containsPlayer(p)) {
    2. //does contain
    3. } else {
    4. //doesnt contain
    5. }
     
  3. Offline

    1Rogue

    If you don't care about the value afterwards:

    Code:sql
    1. INSERT INTO `yourTable` (`player`) VALUES ('somePlayerName') ON DUPLICATE KEY UPDATE `player`=`player`


    This would return a 0 if the player was already in the table, and a 1 if they were a new player. This is more efficient in the fact that you would only need one query.
     
  4. Offline

    Adriani6

    Thanks a lot. I've got another question however, I log the usernames into database obviously, but when I check the records I get CraftPlayer{name=Adriani6}. Anyway just to get the raw username ? .getName(); obviously doesn't do the trick :(
     
  5. Offline

    1Rogue

    Uh, it is .getName().

    Can you show the code you are using? Alternatively, don't depend on craftbukkit (use Bukkit instead)
     
  6. Offline

    Adriani6

    Code:java
    1.  
    2. package adriani6.co.uk.ai6ms;
    3.  
    4. import org.apache.commons.lang.time.FastDateFormat;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.entity.EntityType;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.PlayerDeathEvent;
    11. import org.bukkit.event.player.PlayerJoinEvent;
    12. import org.bukkit.event.player.PlayerQuitEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import java.sql.Connection;
    15. import java.sql.SQLException;
    16. import java.sql.Statement;
    17. import java.util.Date;
    18. import java.util.HashMap;
    19. import java.util.HashSet;
    20.  
    21.  
    22. public class main extends JavaPlugin implements Listener{
    23.  
    24.  
    25.  
    26.  
    27. @EventHandler
    28. public void onPlayerJoinEvent(PlayerJoinEvent event) throws SQLException{
    29. Player p = event.getPlayer();
    30. p.getName();
    31. if(!p.hasPlayedBefore()) {
    32. p.sendMessage("You've been added to the database ");
    33. Statement statement = c.createStatement();
    34. statement.executeUpdate("INSERT INTO `stats` (`playername`) VALUES ('" + p + "') ON DUPLICATE KEY UPDATE `playername`=`playername`");
    35. p.sendMessage("Finished Adding to Database !");
    36. }


    In here, when it enters the name of the player (value of 'p') it keeps coming as that... which is not what I want ;p
     
  7. Offline

    1Rogue

    right, instead of:

    Code:java
    1. "sometext" + p + "othertext"


    Try this:

    Code:java
    1. "sometext" + p.getName() + "othertext"


    You did it above with the second line of the method (though you didn't set the returned value to anything, so that line of code is essentially pointless).
     
  8. Offline

    Adriani6

    That was quite silly haha. Thank you for your help sir
     
Thread Status:
Not open for further replies.

Share This Page