java去除不为null,java – 选择特定字段不为NULL的所有记录
我有一個(gè)名為stars的MySQL表,其中一個(gè)字段是id_num,默認(rèn)值為NULL.我想通過java中的PreparedStatement選擇id_num不為NULL的所有記錄.
現(xiàn)在我正在嘗試這個(gè):
private final String idStarsSQL = "select * from `stars` where id_num is not ?";
...
preparedStatement = (PreparedStatement) connection.prepareStatement(idStarsSQL);
preparedStatement.setString(1, NULL);
set = preparedStatement.executeQuery();
但它不起作用.
解決方法:
private final String idStarsSQL = "select * from `stars` where id_num is not NULL";
你不需要PreparedStatement.
從我的角度來看,PreparedStatement應(yīng)該用于帶參數(shù)的SQL語句.使用帶參數(shù)的SQL語句的優(yōu)點(diǎn)是,您可以使用相同的語句,并在每次執(zhí)行時(shí)為其提供不同的值.
Statement statement = con.createStatement();
ResultSet result = statement.executeQuery("select username, age, nickname from user where nickname is not NULL");
List allUserNullNickname = new ArrayList<>();
while(result.next()){
User user = new User();
user.setUsername(result.getString("username"));
user.setAge(result.getInt("age"));
allUserNullNickname.add(user);
}
System.out.println("All user without nickname:");
allUserNullNickname.stream().forEach(user -> System.out.println("username: "+user.getUsername()+" Age: "+user.getAge()));
標(biāo)簽:java,mysql,select,prepared-statement,notnull,java
來源: https://codeday.me/bug/20190622/1265765.html
總結(jié)
以上是生活随笔為你收集整理的java去除不为null,java – 选择特定字段不为NULL的所有记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ionic tab显示到顶部去了
- 下一篇: ORA-00980与PL/SQL程序编译