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 public void onPlayerJoinEvent(PlayerJoinEvent event) throws SQLException{ Player p = event.getPlayer(); p.getName(); if(!p.hasPlayedBefore()) { Anyone able to help me check the database if the username already exists in the there while onPlayerJoinEvent ? ;p
Adriani6 Code:java public synchronized static boolean containsPlayer(Player p) { try { java.sql.PreparedStatement sql = connection.prepareStatement("SELECT * FROM `Players` WHERE Name = '" + p.getName() + "';"); ResultSet resultSet = sql.executeQuery(); boolean containsPlayer = resultSet.next(); sql.close(); resultSet.close(); return containsPlayer; } catch(Exception e) { e.printStackTrace(); return false; } } With this you can now do; Code:java if (containsPlayer(p)) { //does contain} else {//doesnt contain}
If you don't care about the value afterwards: Code:sql 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.
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
Uh, it is .getName(). Can you show the code you are using? Alternatively, don't depend on craftbukkit (use Bukkit instead)
Code:java package adriani6.co.uk.ai6ms; import org.apache.commons.lang.time.FastDateFormat;import org.bukkit.Bukkit;import org.bukkit.entity.EntityType;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.entity.PlayerDeathEvent;import org.bukkit.event.player.PlayerJoinEvent;import org.bukkit.event.player.PlayerQuitEvent;import org.bukkit.plugin.java.JavaPlugin;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;import java.util.Date;import java.util.HashMap;import java.util.HashSet; public class main extends JavaPlugin implements Listener{ @EventHandler public void onPlayerJoinEvent(PlayerJoinEvent event) throws SQLException{ Player p = event.getPlayer(); p.getName(); if(!p.hasPlayedBefore()) { p.sendMessage("You've been added to the database "); Statement statement = c.createStatement(); statement.executeUpdate("INSERT INTO `stats` (`playername`) VALUES ('" + p + "') ON DUPLICATE KEY UPDATE `playername`=`playername`"); p.sendMessage("Finished Adding to Database !"); } 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
right, instead of: Code:java "sometext" + p + "othertext" Try this: Code:java "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).