String sql="DELETE FROM test.tb_stu WHERE (id =?)"; if(conn!=null) { pstm=conn.prepareStatement(sql); pstm.setInt(1, user.getId()); int tf=pstm.executeUpdate();为什么tf永远等于0
时间: 2024-02-16 09:01:49 浏览: 24
根据你提供的代码,我看不出具体的原因,但是 `tf` 永远等于0 的情况一般是因为没有匹配到需要删除的数据。你可以检查一下数据库中是否存在 `id` 等于 `user.getId()` 的记录,以及 `conn` 是否成功连接到数据库。如果没有匹配到需要删除的数据,那么 `executeUpdate()` 方法会返回0。
相关问题
String sql="DELETE FROM test.tb_stu WHERE (id =?)"; if(conn!=null) { pstm=conn.prepareStatement(sql); pstm.setInt(1, user.getId()); int tf=pstm.executeUpdate(); if(tf!=0) { flag=true; } }这段代码哪里出错了
这段代码看起来没有明显的错误,但是需要注意以下几点:
1. 确保数据库连接(conn)已经正确地建立。如果没有建立连接或连接已经关闭,会抛出NullPointerException或SQLException。
2. 确保表名(tb_stu)和字段名(id)正确无误。如果表名或字段名错误,会抛出SQLException。
3. 确保参数绑定(pstm.setInt(1, user.getId()))正确无误。如果参数绑定错误,会抛出SQLException。
4. 确保SQL语句(DELETE FROM test.tb_stu WHERE (id =?))语法正确无误。如果SQL语句出现语法错误,会抛出SQLException。
5. 确保user对象的getId()方法能够正确地返回id值。如果user对象为null或getId()方法出现异常,会抛出NullPointerException或其他异常。
如果以上几点都没有问题,那么这段代码应该可以正常执行删除操作。如果还有问题,可以查看异常信息或调试代码以找出错误的原因。
public int updateStu(Student stu) throws Exception { Connection conn = null; PreparedStatement pstm = null; ResultSet rs = null; try { conn = dataSource.getConnection(); String sql = "update wn81.student set sname=? ,ssex=?,class=? where sno=?" ; pstm = conn.prepareStatement(sql); pstm.setString(1,stu.getSno()); pstm.setString(2,stu.getSname()); pstm.setString(3,stu.getSsex()); pstm.setString(4,stu.getCalss()); int i = pstm.executeUpdate(); Student s = null; return i ;这个方法有什么问题么
这个方法存在一些问题:
1. 在设置参数时,使用了错误的参数顺序。应该按照 SQL 语句中参数的顺序设置参数,而不是按照 `stu.getSno()`、`stu.getSname()`、`stu.getSsex()`、`stu.getCalss()` 的顺序设置参数。
2. 在 SQL 语句中,将 `sname` 和 `ssex` 的位置颠倒了。应该按照表结构中字段的顺序编写 SQL 语句。
3. 在执行更新操作后,没有对连接、语句和结果集进行关闭。应该使用 try-with-resources 或者 finally 块来关闭这些对象,以释放资源。
下面是修改后的代码:
```java
public int updateStu(Student stu) throws Exception {
try (Connection conn = dataSource.getConnection();
PreparedStatement pstm = conn.prepareStatement(
"UPDATE wn81.student SET sname=?, ssex=?, class=? WHERE sno=?")) {
pstm.setString(1, stu.getSname());
pstm.setString(2, stu.getSsex());
pstm.setString(3, stu.getCalss());
pstm.setString(4, stu.getSno());
int rowCount = pstm.executeUpdate();
return rowCount;
}
}
```
这个方法的参数是一个 `Student` 对象,但是在更新操作中没有使用 `stu` 的 `sno` 属性,因此可能导致更新错误。应该根据实际需求修改 SQL 语句,确保更新操作正确。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)