要使用准备好的语句在java中执行sql查询,我知道你是这样做的:
Connection con=UserModel.getConnection();
String sql = "UPDATE users SET firstname=?,lastname=? WHERE id=?";
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1,u.getFirstname());
ps.setString(2,u.getLastname());
ps.setString(3,u.getId());
setString 方法中的第一个参数表示在有 ? 的地方进入 sql 语句的值的顺序。标志。
现在我想做的是从用户表中选择其中角色不为空。像这样的东西:
Connection con = UserModel.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE role=?");
ps.setString(1,not_null); // ***here***
如何选择角色不为空的情况?这是我的问题。
使用带有以下 where 子句的 sql:
where role is not null
使用 = 将某些内容与 null 进行比较是行不通的。 PreparedStatement 上的 setNull - 请参阅JDBC:将 null 插入到整数列 - 替换 null,但这在这里没有帮助,因为 where 子句条件始终为 false。有关原因,请参阅为什么 NULL = NULL 在 SQL Server 中计算结果为 false
Connection con = UserModel.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE role is NOT NULL");
ps.executeQuery();
将此 not null 用于相应的列名称角色