where name=null 是正确还是错误?
时间: 2024-03-31 17:37:47 浏览: 13
使用 `where name=null` 进行查询是不正确的。在 SQL 中,应该使用 `where name is null` 进行查询。
这是因为在 SQL 中,`null` 表示缺少值或未知值,它与任何其他值都不相等,包括它自己。因此,如果使用 `=` 运算符来比较 `null` 和另一个值,结果总是为 `false`。为此,应该使用 `is null` 或 `is not null` 来检查字段是否为空。
所以,正确的语法应该是 `where name is null`。
相关问题
public String getLogin(String identityNum, String password) { String name=null; ResultSet rs=null; String sql="select patientName from patient where identityNum=? and password=?"; try { if (rs!=null) { rs = executeQuery(sql, identityNum, password); if (rs.next()) { name =rs.getString("patientName"); } } }catch (SQLException throwables) { throwables.printStackTrace(); } return name; }帮我测试下有没有返回值
很抱歉,我作为语言模型无法直接测试Java代码,但是从代码逻辑上看,如果在执行executeQuery方法之前将rs赋值为null,那么在if语句里面就会跳过rs赋值的步骤,rs仍然为null,不会执行rs.next()方法,name还是为null,最后返回null。因此,可能需要修改if的条件判断,或者去掉if语句,如下:
```
public String getLogin(String identityNum, String password) {
String name = null;
ResultSet rs = null;
String sql = "select patientName from patient where identityNum=? and password=?";
try {
rs = executeQuery(sql, identityNum, password);
if (rs.next()) {
name = rs.getString("patientName");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return name;
}
```
这样修改后,可以保证rs不为null,能够正确返回查询结果。
public void update(Mess mess) { Connection conn = null; PreparedStatement pstmt = null; try { conn = DBUtil.getConnection(); String sql = "UPDATE mess SET name=?, birthday=?, height=?, number=? WHERE number=? OR name=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, mess.getName()); pstmt.setDate(2, new java.sql.Date(mess.getBirthday().getTime())); pstmt.setFloat(3, mess.getHeight()); pstmt.setString(4, mess.getNumber()); pstmt.setString(5, mess.getNumber()); pstmt.setString(6, mess.getName()); pstmt.executeUpdate(); // 执行SQL语句并提交到数据库中 } catch (SQLException e) { e.printStackTrace(); } finally { close(conn, pstmt, null); } }解释说明一下该代码
这段代码是一个Java程序中的一个更新(update)数据库记录的方法。该方法使用了JDBC(Java Database Connectivity)来连接到数据库并执行更新操作。
首先,该方法通过调用DBUtil.getConnection()方法获取与数据库的连接。然后,它将传入的Mess对象的属性值设置为对应的SQL参数,并构造了一个SQL语句,使用了占位符(?)来代替参数。该SQL语句的作用是更新mess表中的记录,根据number或name来定位需要更新的记录。
接着,通过调用prepareStatement()方法创建了一个PreparedStatement对象,并将SQL语句传入该对象。接着,通过调用setString()、setDate()和setFloat()等方法将Mess对象的属性值设置为对应的SQL参数。最后,调用executeUpdate()方法执行SQL语句并将更新提交到数据库中。
如果在执行过程中发生了SQLException异常,则会在控制台打印异常信息。最后,通过调用close()方法关闭连接和PreparedStatement对象。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)