MySQL Help

Discussion in 'Plugin Development' started by thepaperboy99, Dec 11, 2013.

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

    thepaperboy99

    Hello Bukkit, I just wanted to know if there is a way to check if a player exists in a table. I know how to put the player in the table and everything, but I need to check if he is already there or else it will keep adding the player to the table when he joins. Thanks :)
     
  2. Offline

    Blingdaddy1

    Paper.. didnt you make the pvp plugin for Divine? Your name sounds familiar...
     
  3. Offline

    slayr288

    thepaperboy99
    Something like this
    Code:
                        final Connection conn = DriverManager.getConnection(url, user, pass);
                        final Statement statement = conn.createStatement();
                        final ResultSet res = statement.executeQuery("SELECT EXISTS(SELECT 1 FROM table WHERE player = '" + playerName + "' LIMIT 1);");
                        PreparedStatement state = null;
                        if (res.next()) {
                            if (res.getInt(1) == 1) {
                                state = conn.prepareStatement("UPDATE table SET player = '" + playerName + "', entry = '" + randomEntry + "' WHERE player = '" + playerName + "';");
                            } else {
                                state = conn.prepareStatement("INSERT INTO table (player, entry) VALUES('" + playerName + "', '" + randomEntry + "');");
                            }
                        }
                        state.executeUpdate();
                        state.close();
                        conn.close();
     
  4. Offline

    thepaperboy99

    Thanks, il yell you tommorow how it goes. :)

    Mhm.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page