设置预准备语句参数,其中NOT NULL java

时间:2017-10-23 03:49:24

标签: java sql jdbc prepared-statement

要使用预准备语句在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语句的值的顺序?登录。

现在我要做的是从用户表中选择,其中role is NOT NULL 。像这样:

Connection con = UserModel.getConnection();  
PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE role=?");
ps.setString(1,not_null); // ***here***

如何选择角色不为空的位置?这是我的问题。

2 个答案:

答案 0 :(得分:1)

将sql与where子句:

一起使用
where role is not null 

使用=将某些内容与null进行比较将不起作用。 PreparedStatement上的setNull - 请参阅JDBC:Inserting null to Integer column - 替换null,但这对此没有帮助,因为where子句条件始终为false。为什么,请参阅Why does NULL = NULL evaluate to false in SQL server

答案 1 :(得分:0)

Connection con = UserModel.getConnection();
PreparedStatement ps = con.prepareStatement(" SELECT * FROM users WHERE role is NOT NULL"); ps.executeQuery();