这个代码为什么报错<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@ page import="java.sql.*" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>修改密码</title> </head> <body> <% // 获取表单参数 String sno = request.getParameter("sno"); String oldPassword = request.getParameter("oldPassword"); String newPassword = request.getParameter("newPassword"); String confirmPassword = request.getParameter("confirmPassword"); // 验证新密码是否一致 if (!newPassword.equals(confirmPassword)) { out.println("新密码和确认密码不一致!"); return; } // 获取当前登录用户的信息 String sno = (String) session.getAttribute("sno"); try { // 创建数据库连接 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=CJ"; String username = "sa"; String password = "Sa123456"; Connection conn = DriverManager.getConnection(url, username, password); // 查询当前用户的密码 PreparedStatement pstmt = conn.prepareStatement("SELECT Password FROM Admin2 WHERE sno = ?"); pstmt.setString(1, sno); ResultSet rs = pstmt.executeQuery(); rs.next(); String dbPassword = rs.getString("Password"); // 验证原密码是否正确 if (!dbPassword.equals(oldPassword)) { out.println("原密码不正确!"); return; } // 更新用户密码 pstmt = conn.prepareStatement("update Admin2 set Password = ? WHERE sno = ?"); pstmt.setString(1, newPassword); pstmt.setString(2, sno); pstmt.executeUpdate(); out.println("密码修改成功!"); // 关闭数据库连接 rs.close(); pstmt.close(); conn.close(); } catch (Exception e) { out.println("数据库操作出现异常:" + e.getMessage()); } %> </body> </html>
时间: 2024-03-16 13:44:55 浏览: 67
这段代码报错的原因是因为在获取表单参数时,定义了两个同名的变量,即第6行和第14行都定义了变量sno。这会导致后面的代码无法识别正确的变量,从而报错。
解决方法是去掉第6行的定义,只保留第14行的定义即可。修改后的代码如下:
```
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改密码</title>
</head>
<body>
<%
// 获取表单参数
String oldPassword = request.getParameter("oldPassword");
String newPassword = request.getParameter("newPassword");
String confirmPassword = request.getParameter("confirmPassword");
// 验证新密码是否一致
if (!newPassword.equals(confirmPassword)) {
out.println("新密码和确认密码不一致!");
return;
}
// 获取当前登录用户的信息
String sno = (String) session.getAttribute("sno");
try {
// 创建数据库连接
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=CJ";
String username = "sa";
String password = "Sa123456";
Connection conn = DriverManager.getConnection(url, username, password);
// 查询当前用户的密码
PreparedStatement pstmt = conn.prepareStatement("SELECT Password FROM Admin2 WHERE sno = ?");
pstmt.setString(1, sno);
ResultSet rs = pstmt.executeQuery();
rs.next();
String dbPassword = rs.getString("Password");
// 验证原密码是否正确
if (!dbPassword.equals(oldPassword)) {
out.println("原密码不正确!");
return;
}
// 更新用户密码
pstmt = conn.prepareStatement("update Admin2 set Password = ? WHERE sno = ?");
pstmt.setString(1, newPassword);
pstmt.setString(2, sno);
pstmt.executeUpdate();
out.println("密码修改成功!");
// 关闭数据库连接
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
out.println("数据库操作出现异常:" + e.getMessage());
}
%>
</body>
</html>
```
阅读全文