0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Oliver Huth
  • 53
  • 1
  • 8
  • 1
    `pUUID.toString() == rs.toString()`. Objects should be compared with `equals` method – sidgate May 09 '19 at 12:51
  • Do you have any id named `playerId` in your database? If not, `"SELECT playerID FROM playerInfo;"` will not be the way to insert a parameter/variable in your sql `String`. It will try to find a record with the id `playerId`. That means this query will always return 0 rows and that leads to another attempt to insert an already existing player. – deHaar May 09 '19 at 12:53
  • You don't have to check `rs != null`, `rs` will never be null. `executeQuery` either returns a non-null `ResultSet` or it throws an exception. The problem however is that you need to call `rs.next()` **before** you retrieve a value. But you should post the full stacktrace of your exception. – Mark Rotteveel May 09 '19 at 15:14
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – rkosegi May 10 '19 at 12:46

2 Answers2

0

You are telling us that

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");
}

outputs "There was errors in trying to add you to our database".

That is because the pCheck.hasPlayer() will be false due to

UUID pUUID = p.getUniqueId();
String query = "SELECT playerID FROM playerInfo;";

not considering the pUUID. Instead, it looks for an entry having "playerId" as its UUID. The result is an attempt to insert this player again, which will not work due to some database constraint (unique primary key or anything like that).

You may want to re-write the query to something like

String query = "SELECT " + pUUID + " FROM playerInfo;";

and try again. I hope this helps...

deHaar
  • 17,687
  • 10
  • 38
  • 51
0

The problem is with your SQLCheckPlayer#hasPlayer() method, because it allways return false because of the string comparison that is never will be true, since you're trying to convert the ResultSet object into a string. I would also change the query string. You could use the SQL EXISTS Operator to only return a boolean whether the player is in the database or not.
So the new query string looks like this:

String query = "select exists (select null from playerInfo where `playerID` = '"+p.getUniqueId().toString()+"');"

And the SQLCheckPlayer#hasPlayer() will be this:

String query = "select exists (select null from playerInfo where `playerID` = '" + p.getUniqueId().toString() + "');";
try {
    if (sqlConnection.getDatabaseConnection() != null) {
        Statement st = sqlConnection.getDatabaseConnection().createStatement();
        ResultSet rs = st.executeQuery(query);
        if (rs.next()) {
            if (rs.getBoolean(1)) {
                st.close();
                Bukkit.broadcastMessage("Player successfully added");
                return true;
            }
        }
        st.close();
    } else {
        Bukkit.broadcastMessage("Database connection is null");
        return false;
    }
    return false;
} catch (SQLException e) {
    e.printStackTrace();
    Bukkit.broadcastMessage("Sql Exception");
    return false;
}
Noel Nemeth
  • 646
  • 11
  • 21