0

使用 oracle 数据库,我需要执行检查以查看我的“用户”表中的用户是否被禁止。如果他的列“banned”的值为“1”,则该用户被禁止,否则为“0”。

我在这里有以下工作代码:

public boolean banUser(String username)
    {//TODO check if user is banned already
        try 
        {
            pstmnt = conn.prepareStatement("UPDATE users SET banned = 1 WHERE username = ?");
            pstmnt.setString(1, username);
            pstmnt.execute();
            logger.info("Banned User : " + username);
            return true;
        } catch ( SQLException e ) { e.getMessage(); }
        return false;
    }

我不确定如何在准备好的语句之上执行 if 语句。有任何想法吗?

4

5 回答 5

2

更改您的更新声明:

conn.prepareStatement("UPDATE users SET banned = 1 WHERE username = ?");

至:

conn.prepareStatement("UPDATE users SET banned = 1 WHERE username = ? and banned = 0");

executeUpdate()并使用而不是执行它execute()。不同之处在于executeUpdate返回受更新影响的行数,因此您无需执行 SQL 语句来检索用户是否已被禁止。

//...
pstmnt = conn.prepareStatement("UPDATE users SET banned = 1 WHERE username = ? and banned = 0");
pstmnt.setString(1, username);
int affectedRows = pstmnt.executeUpdate();
boolean wasUserBanned = affectedRows > 0;
//...
于 2013-06-24T13:36:52.347 回答
1

这是我对@BackSlash 版本的变体,假设username有一个 UNIQUE 约束:

pstmnt = conn.prepareStatement(
    "SELECT COUNT(*) FROM users WHERE username = ? AND banned = 1");
pstmnt.setString(1, username);
ResultSet rs = pstmnt.executeQuery();  // Always returns exactly one row.
rs.next();
return rs.getBoolean(1);               // Converts 0, 1 to false, true.

你可以让数据库做更多的工作

CREATE VIEW BANNED_USERS AS
    SELECT username FROM USERS
    WHERE banned = 1;
于 2013-06-24T14:20:36.710 回答
1

使用此查询,并将结果存储在ResultSet

SELECT * FROM users WHERE username = ? AND banned = 1

这将返回具有特定用户名的所有被禁止用户。

pstmnt = conn.prepareStatement("SELECT * FROM users WHERE username = ? AND banned = 1");
pstmnt.setString(1, username);
ResultSet rs = pstmnt.executeQuery();
if(rs.next()) {
    //The user is banned
} else {
    //The user is not banned
}

如果你想要一个方法版本:

public boolean isUserBanned(String user) throws SQLException {
    pstmnt = conn.prepareStatement("SELECT * FROM users WHERE username = ? AND banned = 1");
    pstmnt.setString(1, username);
    ResultSet rs = pstmnt.executeQuery();
    return rs.next();
}
于 2013-06-24T13:31:39.370 回答
0

更新后运行选择:

    pstmnt = conn.prepareStatement("SELECT banned FROM users WHERE username = ?");
    Resultset rs = pstmnt.execute();
    pstmnt.setString(1, username);
    int isBanned = 0;
    while(rs.next()) {
        isBanned = rs.getInt(1);
    }
    if(isBanned == 1) {
        System.out.println("User is banned")
    }
于 2013-06-24T13:33:07.197 回答
-1

重用您的代码:(假设用户已经存在)

public boolean isUserBanned(String username)
    {//TODO check if user is banned already
        try 
        {
            pstmnt = conn.prepareStatement("select username WHERE username = ? and banned = 1");
            pstmnt.setString(1, username);
            Resultset rs = pstmnt.executeQuery();
            //logger.info("Banned User : " + username);
            return rs.next();
        } catch ( SQLException e ) { e.getMessage(); }
        return false;
    }
于 2013-06-24T13:34:44.920 回答