pStatement.setString(3, dobString);
时间: 2024-04-14 12:29:01 浏览: 81
pStatement.setString(3, dobString) 是一个用于设置准备语句(PreparedStatement)中参数的方法。在这个例子中,pStatement 是一个准备语句对象,setString(3, dobString) 将会设置该准备语句的第三个参数的值为 dobString。这个方法通常在数据库操作中使用,用于设置准备语句的参数值,以便执行数据库操作时传递参数。在这个例子中,它可能用于将一个字符串类型的日期值(dobString)设置为准备语句的第三个参数的值。
相关问题
public String revisePassword(Admin admin,String newPassword) { String resultStr="用户名或密码错误"; String sqlStr="update s_admin set password=?where id=? and name=? and password=?"; try { this.pStatement=this.con.prepareStatement(sqlStr); this.pStatement.setString(1, newPassword); this.pStatement.setInt(2, admin.getId()); this.pStatement.setString(3,admin.getName()); this.pStatement.setString(4,admin.getPassword()); if(this.pStatement.executeUpdate()>0) { resultStr="操作成功"; IndexFrame.admin.setPassword(newPassword); } }catch (SQLException e) { e.printStackTrace(); }finally { this.close(); } return resultStr; }
根据代码来看,这是一个修改管理员密码的方法。方法接收两个参数,一个是 Admin 类型的对象,另一个是新密码。在方法中,首先会判断传入的 Admin 对象的 id、name 和 password 是否与数据库中的数据匹配,如果匹配,则会执行 SQL 语句更新该管理员的密码,并将方法返回值设为 "操作成功",否则返回 "用户名或密码错误"。另外,代码中的 IndexFrame.admin.setPassword(newPassword) 表示将静态变量 IndexFrame.admin 的密码修改为新密码。
检查以下语法错误:<body> <a href="javascript:history.go(-1);">返回 </a> <a href="Main.jsp">主页</a> <br> <br> 编写人姓名: 胡莲 学号:20212310930228 <br> <% request.setCharacterEncoding("UTF-8"); String sxh = request.getParameter("xh").trim(); String sname = request.getParameter("name").trim(); String sclassname = request.getParameter("classname").trim(); int sage = Integer.parseInt(request.getParameter("age")); String saddress = request.getParameter("address").trim(); String sphone = request.getParameter("phone").trim(); String scity = request.getParameter("city").trim(); String snotes = request.getParameter("notes").trim(); String sql = "update student set name='" + sname + "'" + ",classname='" + sclassname + "'" + ",age='" + sage + "'"+ "'" + ",address='" + saddress + "'"+ "'" + ",phone='" + sphone + "'"+ "'" + ",city='" + scity + "'"+ "'" + ",notes='" + snotes + "'"+ "where xh='" + sxh + "'"; try { java.sql.Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); //加载驱动程序 conn = java.sql.DriverManager .getConnection( "jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=utf8", "root", "123456"); //URL指向数据库student PreparedStatement pstatement = conn.prepareStatement(sql); //建立SQL语句 pstatement.execute(); //执行SQL语句 out.print("学生(" + sxh + "," + sname + "," + sclassname + "," + sage + ")");//打印sql out.println("信息更新成功!"); pstatement.close(); conn.close(); } catch (SQLException e1) { System.out.print("sql语句失败!"); e1.printStackTrace(); } catch (Exception e2) { System.out.print("sql语句失败!"); e2.printStackTrace(); } %> </body>
代码存在以下语法错误:
1. 第8行缺少一个空格,应该是"返回 "而不是"返回";
2. 第14行多了一个单引号,应该是",age='" + sage + "'"而不是",age='" + sage + "''";
3. 第15行多了一个单引号,应该是",address='" + saddress + "'"而不是",address='" + saddress + "''";
4. 第16行多了一个单引号,应该是",phone='" + sphone + "'"而不是",phone='" + sphone + "''";
5. 第17行多了一个单引号,应该是",city='" + scity + "'"而不是",city='" + scity + "''";
6. 第18行多了一个单引号,应该是",notes='" + snotes + "'"而不是",notes='" + snotes + "''";
7. 第25行应该是out.print而不是out.println,因为后面没有换行符。
正确的代码如下所示:
```html
<body>
<a href="javascript:history.go(-1);">返回 </a>
<a href="Main.jsp">主页</a>
<br>
<br>
<%
request.setCharacterEncoding("UTF-8");
String sxh = request.getParameter("xh").trim();
String sname = request.getParameter("name").trim();
String sclassname = request.getParameter("classname").trim();
int sage = Integer.parseInt(request.getParameter("age"));
String saddress = request.getParameter("address").trim();
String sphone = request.getParameter("phone").trim();
String scity = request.getParameter("city").trim();
String snotes = request.getParameter("notes").trim();
String sql = "update student set name='" + sname + "'"
+ ",classname='" + sclassname + "'"
+ ",age='" + sage + "'"
+ ",address='" + saddress + "'"
+ ",phone='" + sphone + "'"
+ ",city='" + scity + "'"
+ ",notes='" + snotes + "'"
+ "where xh='" + sxh + "'";
try {
java.sql.Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance(); //加载驱动程序
conn = java.sql.DriverManager
.getConnection(
"jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=utf8",
"root", "123456"); //URL指向数据库student
PreparedStatement pstatement = conn.prepareStatement(sql); //建立SQL语句
pstatement.execute(); //执行SQL语句
out.print("学生(" + sxh + "," + sname + "," + sclassname + "," + sage
+ ")");//打印sql
out.print("信息更新成功!");
pstatement.close();
conn.close();
} catch (SQLException e1) {
System.out.print("sql语句失败!");
e1.printStackTrace();
} catch (Exception e2) {
System.out.print("sql语句失败!");
e2.printStackTrace();
}
%>
</body>
```
阅读全文