ResultSet in Java



我需要帮助为我所做的方法创建布尔检查。如果他们有指定的EnumRank,我基本上希望它返回true,但它不起作用。有什么建议吗?

编辑:两个值都存储在VARCHARS 中

代码:

    public boolean hasRank(Player player, EnumRanks rank){
    if (!MySql.checkConnection()){
        return false;
    }
    try{
        String query = "SELECT RANK FROM `user_ranks` WHERE UUID= '" + player.getUniqueId() + "';";
        PreparedStatement statement = MySql.getConnection().prepareStatement(query);
        ResultSet result = statement.executeQuery();
        result.next();
        return result.getBoolean(rank.getSQLName());
        //return true - So I can use this method
    }
    catch (SQLException e){
        //Nothing
    }
    return false;
}

将秩添加到查询中,您使用的是PreparedStatement,因此使用绑定参数。您可以使用result.next()来检查查询是否返回结果,并且应该使用finally块来关闭StatementResultSet(以及Connection)。不要默默吞下Exception。类似的东西

public boolean hasRank(Player player, EnumRanks rank) {
    if (!MySql.checkConnection()) {
        return false;
    }
    Connection conn = null;
    PreparedStatement statement = null;
    ResultSet result = null;
    String query = "SELECT RANK FROM `user_ranks` WHERE UUID=? AND RANK=?";
    try {
        conn = MySql.getConnection();
        statement = conn.prepareStatement(query);
        statement.setString(1, player.getUniqueId());
        statement.setString(2, rank.getSQLName());
        result = statement.executeQuery();
        return result.next();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (result != null) {
            try {
                result.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

相关内容

  • 没有找到相关文章

最新更新