11.23

<%@ 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>

posted @   new菜鸟  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示