<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.*" %>
<html>
<body>
<%
request.setCharacterEncoding("UTF-8");
String UserID = (String) session.getAttribute("UserID");
String password = request.getParameter("password");
String password1 = request.getParameter("password1");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet existingRecord = null;
String message = ""; // 初始化message变量
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/fangchan?useSSL=false";
String name1 = "root";
String password2 = "123456";
conn = DriverManager.getConnection(url, name1, password2);
String query = "SELECT * FROM class3 WHERE UserID=? AND password=?";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, UserID);
pstmt.setString(2, password);
existingRecord = pstmt.executeQuery();
if (existingRecord.next()) {
// 更新密码
String sql = "UPDATE class3 SET password = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, password1);
pstmt.executeUpdate(); // 执行更新查询
message = "修改成功!"; // 更新message变量
} else {
out.println("编号 " + UserID + " 不存在。");
}
} catch (SQLException | ClassNotFoundException e) {
System.out.println("发生错误: " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (existingRecord != null) {
existingRecord.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
System.out.println("关闭资源时发生错误: " + e.getMessage());
}
}
if (!message.isEmpty()) {
// 显示成功消息
out.println(message);
}
%>
</html>
<body>