I am writing a bukkit plugin for my minecraft server and my code either has an error in how I have written the classes and methods or as I presume, in the Result set part.
It isn't detecting that my player is already in the SQL table. This is in the hasPlayer method.
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player p = event.getPlayer();
SQLCheckPlayer pCheck = new SQLCheckPlayer();
pCheck.p = p;
boolean i = pCheck.hasPlayer();
if (i == false) {
boolean n = pCheck.addPlayer();
if (n == true) {
p.sendMessage("You have been added to our database");
} else {
p.sendMessage("There was errors in trying to add you to our database");
}
} else {
p.sendMessage("You are in our database");
}
}
the SQLCheckPlayer class
public class SQLCheckPlayer {
Player p;
public boolean hasPlayer() {
UUID pUUID = p.getUniqueId();
String query = "SELECT playerID FROM playerInfo;";
try {
if (sqlConnection.getDatabaseConnection() != null) {
Statement st = sqlConnection.getDatabaseConnection().createStatement();
ResultSet rs = st.executeQuery(query);
if (rs != null) {
do{
if(pUUID.toString() == rs.toString()) {
st.close();
Bukkit.broadcastMessage("Player successfully added");
return true;
}
} while(rs.next());
}
st.close();
} else {
Bukkit.broadcastMessage("Database connection is null");
return false;
}
return false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Bukkit.broadcastMessage("Sql Exception");
return false;
}
}
public boolean addPlayer() {
UUID pUUID = p.getUniqueId();
try {
PreparedStatement st = sqlConnection.getDatabaseConnection().prepareStatement("INSERT INTO playerInfo (playerID, playerName, playerDiamonds) VALUES (?, ?, ?)");
st.setString(1, pUUID.toString());
st.setString(2, p.getDisplayName());
st.setInt(3, 0);
st.executeUpdate();
st.close();
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
After I have been added to the database (which it can successfully do) whenever I join back it should say "You are in our database" but it always goes to "There was errors in trying to add you to our database". On the print the print stack trace it said the code was trying to add me into the database again, which it couldn't because I had put a unique tab on the playerUUID column.