简体   繁体   English

如何检查mysql条目是否存在

[英]How to check whether or not a mysql entry exists

i tried to check whether or not a Player is in a MySQL database.我试图检查玩家是否在 MySQL 数据库中。

My code for that was this:我的代码是这样的:

    public boolean existUUID() {
        List<String> list = new ArrayList<>();

        try {
            PreparedStatement state = MySQL.c.prepareStatement("SELECT * FROM players");
            state.setString(1, this.uuid.toString());

            ResultSet result = state.executeQuery();

            while (result.next()) {
                list.add(result.getString("uuid"));
            }
            result.close();
            state.close();


        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list.contains(this.uuid);
    }

uuid is setted in the Class and a player with the uuid does exist in the database uuid 设置在 Class 中,并且数据库中确实存在具有 uuid 的玩家

Creating a list every time you call the method (a singleton ArrayList) is a waste of memory, even if you don't notice it much.每次调用该方法(一个单例 ArrayList)时都创建一个列表是一种内存浪费,即使您没有注意到它。 You also don't need to pass an index or value in your statement if there are no values to insert in your query.如果查询中没有要插入的值,则您也不需要在语句中传递索引或值。 If you try to check only one uuid you don't have to use a while-loop.如果您尝试只检查一个 uuid,则不必使用 while 循环。 You can also use a try-with-resource to close your statements when they are no longer needed.您还可以使用 try-with-resource 关闭不再需要的语句。 If you want to read values from a SQL database, I would always recommend using a consumer.如果你想从 SQL 数据库中读取值,我总是建议使用消费者。 I have attached an example:我附上了一个例子:

public void registeredUniqueId(UUID uniqueId, Consumer<Boolean> registered) {
    try (PreparedStatement statement =  connection.prepareStatement(String.format("SELECT * FROM players WHERE UUID='%s'", uniqueId.toString())); ResultSet resultSet = statement.executeQuery()) {

    if (resultSet.next() && resultSet.getString("UUID") != null) {
      registered.accept(true);
    }
  } catch (SQLException ex) {
    ex.printStackTrace();
    registered.accept(false);
  }
}

I recommend to always run MySQL queries asynchronously.我建议始终异步运行 MySQL 查询。 In my example everything is executed synchronously.在我的示例中,所有内容都是同步执行的。

I would suggest the following code & query我建议使用以下代码和查询

 public boolean existUUID() {
        List<String> list = new ArrayList<>();

        try {
            PreparedStatement state = MySQL.c.prepareStatement("SELECT * FROM players where uuid=?");
            state.setString(1, this.uuid.toString());

            ResultSet result = state.executeQuery();

            while (result.next()) {
                return true
            }
            return false


        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally
        {
            result.close();
            state.close();
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM