pstmt.setString(1, "John");pstmt.setInt(2, 30);pstmt.setString(3, "john@example.com");
时间: 2024-06-04 09:13:58 浏览: 10
These three lines of code are setting values for the parameters of a prepared statement object, which will be used to execute a SQL query.
The first line sets the value of the first parameter to the string "John". The second line sets the value of the second parameter to the integer 30. The third line sets the value of the third parameter to the string "john@example.com".
These values will be used by the prepared statement object when it executes the SQL query, which will likely involve inserting or updating data in a database. By using a prepared statement, the values are safely and efficiently incorporated into the query, without the risk of SQL injection attacks.
相关问题
pstmt.setInt(1,book. getId()); pstmt.setString(2, book.getName ());
这是 PreparedStatement 对象的方法调用,用于将具体的参数值设置到 SQL 语句中。例如,`pstmt.setInt(1, book.getId())` 将第一个问号的值设置为 `book.getId()`,`pstmt.setString(2, book.getName())` 将第二个问号的值设置为 `book.getName()`。
具体来说,`setInt(int parameterIndex, int x)` 方法将 SQL 语句中指定位置的占位符替换为整数类型的值 `x`,`setString(int parameterIndex, String x)` 方法将 SQL 语句中指定位置的占位符替换为字符串类型的值 `x`。其中,`parameterIndex` 表示占位符的位置,从 1 开始计数。
通过使用 PreparedStatement 对象的参数设置方法,可以灵活地设置 SQL 语句中的参数值,而不需要拼接 SQL 字符串,避免了 SQL 注入攻击,同时也提高了 SQL 语句的执行效率。
JSP文件:[/testYuYue.jsp] 的第 [39] 行发生了一个错误 The method setString(int, String) in the type PreparedStatement is not applicable for the arguments (int, int) 36: PreparedStatement pstmt = conn.prepareStatement(INSERT_SQL); { 37: pstmt.setString(1, usernum); 38: pstmt.setString(2, stime); 39: pstmt.setString(3,0); 40: pstmt.setString(4, people); 41: count = pstmt.executeUpdate(); 42: }} catch (SQLException e)
根据错误提示,第39行的问题是setString方法的参数类型不匹配。setString方法需要接收一个String类型的参数,但是你传入了一个int类型的参数0。因此,你需要将第39行的代码改为:
```
pstmt.setInt(3, 0);
```
或者将0改为字符串类型的"0":
```
pstmt.setString(3, "0");
```
这样就可以解决这个错误了。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)