|
|
package servlet.admin;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.sql.PreparedStatement;
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
|
import javabean.Base;
|
|
|
import javabean.Util;
|
|
|
|
|
|
/**
|
|
|
* Servlet 实现类,用于处理管理员删除操作
|
|
|
*
|
|
|
* 该 Servlet 处理管理员删除请求,根据管理员的 ID 删除对应的记录。
|
|
|
*/
|
|
|
@WebServlet("/admin/managerDel")
|
|
|
public class ManagerDel extends HttpServlet {
|
|
|
|
|
|
/**
|
|
|
* 处理 GET 请求,删除指定 ID 的管理员
|
|
|
*
|
|
|
* 该方法会接收管理员的 ID,删除数据库中对应的管理员记录,并返回删除结果。
|
|
|
*
|
|
|
* @param req 请求对象
|
|
|
* @param resp 响应对象
|
|
|
* @throws ServletException 如果发生 Servlet 错误
|
|
|
* @throws IOException 如果发生 I/O 错误
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置响应内容类型为 JSON
|
|
|
resp.setContentType("application/json; charset=utf8");
|
|
|
|
|
|
// 获取请求中的 ID 参数
|
|
|
String id = req.getParameter("id");
|
|
|
|
|
|
// SQL 语句及数据库连接准备
|
|
|
String sql = "";
|
|
|
Connection connection = null;
|
|
|
PreparedStatement pstmt = null;
|
|
|
ResultSet resultSet = null;
|
|
|
int result = 0;
|
|
|
|
|
|
// 返回的响应数据
|
|
|
int code = 1;
|
|
|
String msg = "";
|
|
|
PrintWriter out = resp.getWriter();
|
|
|
|
|
|
try {
|
|
|
// 获取数据库连接
|
|
|
connection = (Connection) Base.getConnection();
|
|
|
|
|
|
// SQL 删除语句
|
|
|
sql = "delete from manager where id=?";
|
|
|
pstmt = connection.prepareStatement(sql);
|
|
|
pstmt.setString(1, id); // 设置 ID 参数
|
|
|
result = pstmt.executeUpdate(); // 执行删除操作
|
|
|
|
|
|
// 根据操作结果返回响应
|
|
|
if (result == 1) {
|
|
|
code = 0;
|
|
|
msg = "删除成功";
|
|
|
} else {
|
|
|
msg = "删除失败";
|
|
|
}
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
msg = "class not found";
|
|
|
} catch (SQLException e) {
|
|
|
msg = "sql错误";
|
|
|
} finally {
|
|
|
// 关闭数据库资源
|
|
|
try {
|
|
|
Base.closeResource(connection, pstmt, resultSet);
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 返回 JSON 格式的响应
|
|
|
out.print(Util.jsonResponse(code, msg, null));
|
|
|
}
|
|
|
}
|