public void updateStudent(Student student){ String sql = "update student set stuname = '"+student.getStuname()+"',stuage = '"+student.getStuage()+"'"; try { queryRunner.update(connection,sql,new ScalarHandler()); } catch (SQLException e) { e.printStackTrace(); } }显示java.sql.SQLException: Wrong number of parameters: expected 0, was given 1 Query: update student set stuname = '张三',stuage = '21' Parameters:
时间: 2024-03-28 07:41:41 浏览: 17
即使你使用了 `ScalarHandler`,也无法解决参数数量不匹配的问题,因为 `ScalarHandler` 并不是用来处理参数的。你仍然需要使用参数化查询的方式来构建 SQL 语句,并将参数传递给 `queryRunner.update()` 方法,例如:
```
public void updateStudent(Student student) {
String sql = "update student set stuname = ?, stuage = ?";
try {
queryRunner.update(connection, sql, student.getStuname(), student.getStuage());
} catch (SQLException e) {
e.printStackTrace();
}
}
```
在这个代码中,我们使用了 `?` 占位符来代替 SQL 语句中的具体值,然后在调用 `queryRunner.update()` 方法时,将具体的值作为参数传入。这样就能够避免参数数量不匹配的问题。
相关问题
public void updateStudent(Student student){ String sql = "update student set stuname = '"+student.getStuname()+"',stuage = '"+student.getStuage()+"'"; try { queryRunner.update(connection,sql,new ScalarHandler()); } catch (SQLException e) { e.printStackTrace(); } }改正确
你的代码存在 SQL 注入的风险,因为它直接将用户输入的数据嵌入到 SQL 语句中,而不是使用参数化查询的方式来处理。为了避免 SQL 注入,你应该改用参数化查询的方式来构建 SQL 语句。修改后的代码如下:
```
public void updateStudent(Student student) {
String sql = "update student set stuname = ?, stuage = ?";
try {
queryRunner.update(connection, sql, student.getStuname(), student.getStuage());
} catch (SQLException e) {
e.printStackTrace();
}
}
```
在这个代码中,我们使用了 `?` 占位符来代替 SQL 语句中的具体值,然后在调用 `queryRunner.update()` 方法时,将具体的值作为参数传入。这样就能够避免 SQL 注入的问题。
java.sql.SQLException: Can not issue data manipulation statements with executeQuery(). Query: update student set stuname = '张三',stuage = '21' Parameters: []
这个异常的意思是你正在尝试使用 `executeQuery()` 方法来执行一个数据更新的 SQL 语句,这是不合法的。`executeQuery()` 方法只能用于执行查询语句,而不能用于执行数据更新操作。如果你需要执行数据更新操作,应该使用 `executeUpdate()` 方法。例如,对于这个 SQL 语句,你应该这样执行:
```
String sql = "update student set stuname = '张三', stuage = '21'";
Statement stmt = conn.createStatement();
int rows = stmt.executeUpdate(sql);
```
其中,`conn` 是一个已经建立好连接的 `Connection` 对象,`stmt` 是通过 `conn` 创建的一个 `Statement` 对象。`executeUpdate()` 方法将会返回受影响的行数。
相关推荐
![mdb](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)