如果位值为1或0,如何返回true / false值?

时间:2011-12-22 03:17:39

标签: java mysql jdbc

如果位值为1或0,我如何返回true / false值?该表有一个用户名列表,位值为1或0.我想检查位值是1还是0。

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:1)

只做

boolean value = resultSet.getInt("columnName") != 0;

false时为0,否则为true

答案 1 :(得分:0)

public class ValueDao {
    public static final String SELECT_VALUE = "SELECT VALUE FROM MY_TABLE"; // WHERE clause?

    private Connection connection;

    public ValueDao(Connection connection) {
        this.connection = connection;
    }

    public boolean hasValue(String value) throws SQLException {
        boolean hasValue = false;

        if (value != null) {
            Statement st = null;
            ResultSet rs = null;
            try {
                st = connection.createStatement();
                rs = st.executeQuery();
                while (rs.next()) {
                    String x = rs.getString(1);
                    if (value.equals(x)) {
                        hasValue = true;
                        break;
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DatabaseUtils.close(rs);  // you write these cleanup methods.
            DatabaseUtils.close(st);
        }

        return hasValue;
    }
}

答案 2 :(得分:0)

你可以在连接上的java中运行查询,如:

//Say you have a Connection conn
//Say column for bit value is USER_BIT 
//and say 1st bit we are looking for
PreparedStatement stmt = conn.prepareStatement("select USER_BIT & 1 from USERS where USER_ID ='123'");

注意:以上代码仅供参考。您可能需要根据需要进行更改。