From 16411a7127af1b778695c78c20624779224e99e5 Mon Sep 17 00:00:00 2001 From: Wangder03 Date: Fri, 13 Dec 2024 19:04:05 +0800 Subject: [PATCH 1/5] wangd --- WebContent/admin/bookborrowlist.jsp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WebContent/admin/bookborrowlist.jsp b/WebContent/admin/bookborrowlist.jsp index b04ad29..c07f766 100644 --- a/WebContent/admin/bookborrowlist.jsp +++ b/WebContent/admin/bookborrowlist.jsp @@ -4,7 +4,7 @@ <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ page language="java" contentType="text/html; charset=UTF-8" - pageEncoding="UTF-8"%> + pageEncoding="UTF-8"%>//1 From cb9406e260c8c5f20bac007694e1e57460e2595b Mon Sep 17 00:00:00 2001 From: Wangder03 Date: Fri, 13 Dec 2024 19:06:12 +0800 Subject: [PATCH 2/5] wangd --- WebContent/admin/bookborrowlist.jsp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WebContent/admin/bookborrowlist.jsp b/WebContent/admin/bookborrowlist.jsp index c07f766..36a979e 100644 --- a/WebContent/admin/bookborrowlist.jsp +++ b/WebContent/admin/bookborrowlist.jsp @@ -4,7 +4,7 @@ <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ page language="java" contentType="text/html; charset=UTF-8" - pageEncoding="UTF-8"%>//1 + pageEncoding="UTF-8"%>//1//1 From 416648a74142ce4c22f16f87ed55c243ea6c5130 Mon Sep 17 00:00:00 2001 From: Wangder03 Date: Fri, 13 Dec 2024 20:20:11 +0800 Subject: [PATCH 3/5] wangd --- src/filter/AdminFilter.java | 56 +++++++- src/javabean/Admin.java | 200 +++++++++++++++----------- src/javabean/Base.java | 86 ++++++----- src/javabean/JDBCBean.java | 47 +++++- src/servlet/admin/AdminLogin.java | 57 ++++++-- src/servlet/admin/BookAdd.java | 89 ++++++++---- src/servlet/admin/BookDel.java | 63 ++++++-- src/servlet/admin/BookEdit.java | 81 ++++++++--- src/servlet/admin/BookList.java | 81 ++++++++--- src/servlet/admin/BorrowList.java | 132 ++++++++++------- src/servlet/admin/CardAdd.java | 78 +++++++--- src/servlet/admin/CardDel.java | 48 +++++-- src/servlet/admin/CardEdit.java | 61 ++++++-- src/servlet/admin/CardList.java | 76 +++++++--- src/servlet/admin/LibraryData.java | 79 ++++++---- src/servlet/admin/LoginOut.java | 37 +++-- src/servlet/admin/ManagerAdd.java | 61 +++++--- src/servlet/admin/ManagerDel.java | 53 +++++-- src/servlet/admin/ManagerEdit.java | 63 +++++--- src/servlet/admin/ManagerList.java | 62 +++++--- src/servlet/admin/RuleAdd.java | 109 ++++++++------ src/servlet/admin/RuleDel.java | 64 ++++++--- src/servlet/admin/RuleEdit.java | 69 ++++++--- src/servlet/admin/RuleList.java | 52 +++++-- src/servlet/admin/SortAdd.java | 72 +++++++--- src/servlet/admin/SortDel.java | 82 +++++++---- src/servlet/admin/SortEdit.java | 81 +++++++---- src/servlet/admin/SortList.java | 63 ++++++-- src/servlet/admin/UpdatePassword.java | 92 +++++++----- 29 files changed, 1540 insertions(+), 654 deletions(-) diff --git a/src/filter/AdminFilter.java b/src/filter/AdminFilter.java index 7dfb430..5440a76 100644 --- a/src/filter/AdminFilter.java +++ b/src/filter/AdminFilter.java @@ -14,25 +14,67 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** - * 系统管理员过滤 - * @author Mingyue + * 系统管理员过滤器 + * + * 该过滤器用于检查用户的会话是否包含管理员的身份信息, + * 如果没有管理员身份信息,则重定向到管理员登录页面。 + * 用于保护需要管理员权限才能访问的页面。 * + * @author Mingyue */ public class AdminFilter implements Filter { + /** + * 初始化方法 + * + * 该方法在过滤器被创建时调用,只调用一次。可以在此方法中进行一些初始化操作。 + * 目前未做任何操作。 + * + * @param filterConfig 过滤器的配置对象 + * @throws ServletException 如果发生初始化错误 + */ public void init(FilterConfig filterConfig) throws ServletException { + // 初始化操作(当前没有) + } - } - + /** + * 过滤请求和响应 + * + * 该方法会在每次请求到达目标Servlet之前被调用。它会检查会话中是否存在管理员身份, + * 如果管理员身份信息不存在,则会通过JavaScript跳转到管理员登录页面。 + * + * @param request 传入的Servlet请求对象 + * @param response 传入的Servlet响应对象 + * @param chain 过滤器链对象,允许将请求传递到下一个过滤器或目标Servlet + * @throws IOException 如果输入输出过程中发生错误 + * @throws ServletException 如果Servlet处理请求时发生错误 + */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + // 将ServletRequest转换为HttpServletRequest,以便处理HTTP请求 HttpServletRequest req = (HttpServletRequest) request; + // 获取会话对象 HttpSession session = req.getSession(); + + // 检查会话中是否存在名为"admin"的属性,若不存在则跳转到登录页面 if(session == null || session.getAttribute("admin") == null) { + // 将ServletResponse转换为HttpServletResponse,以便处理HTTP响应 HttpServletResponse rep = (HttpServletResponse)response; - PrintWriter out =response.getWriter(); - // iframe父页面直接跳转到登录界面 + // 获取输出流 + PrintWriter out = rep.getWriter(); + // 使用JavaScript进行重定向,跳转到管理员登录页面 out.print(""); + } else { + // 如果管理员已登录,继续执行过滤器链中的下一个过滤器或目标Servlet + chain.doFilter(request, response); } - chain.doFilter(request, response); + } + + /** + * 销毁方法 + * + * 该方法在过滤器被销毁时调用,一般用于清理资源。当前没有特定操作。 + */ + public void destroy() { + // 销毁操作(当前没有) } } diff --git a/src/javabean/Admin.java b/src/javabean/Admin.java index 7acff63..11b919b 100644 --- a/src/javabean/Admin.java +++ b/src/javabean/Admin.java @@ -11,52 +11,62 @@ import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Admin { + /** - * 用户登录 - * - * @param username - * @param password - * @return + * 用户登录方法 + * + * @param username 用户名 + * @param password 密码 + * @return 登录结果的字符串 * @throws ClassNotFoundException * @throws SQLException */ @SuppressWarnings("null") public String login(String username, String password) throws ClassNotFoundException, SQLException { + // 参数校验 if (username == null || username.trim().equals("")) { return "账号不能为空"; } else if (password == null || password.trim().equals("")) { return "密码不能为空"; } + Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; + + // SQL查询语句,验证用户和密码 String sql = "select * from admin where username=? and password=? limit 1"; connection = Base.getConnection(); pstmt = (PreparedStatement) connection.prepareStatement(sql); pstmt.setString(1, username); - pstmt.setString(2, Util.passMd5(password)); + pstmt.setString(2, Util.passMd5(password)); // 假设Util.passMd5()是对密码的加密方法 resultSet = pstmt.executeQuery(); - try{ - if (resultSet.next()) { + + try { + if (resultSet.next()) { // 如果有记录,说明登录成功 return "1"; } - }catch(Exception e) { + } catch (Exception e) { return "发生未知错误"; - }finally { - if(Base.closeResource(connection, pstmt, resultSet) == false) { + } finally { + // 关闭数据库连接资源 + if (Base.closeResource(connection, pstmt, resultSet) == false) { return "关闭失败"; } } - + + // 如果没有记录,账号或密码错误 return "账号或密码错误"; } - + /** - * 获取图书列表 - * @param page - * @param limit - * @return String json字符串 + * 获取图书列表方法,带分页查询功能 + * + * @param page 当前页 + * @param limit 每页显示条数 + * @param where 查询条件 + * @return 返回包含数据和总条数的Map * @throws ClassNotFoundException * @throws SQLException */ @@ -66,27 +76,30 @@ public class Admin { Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; + connection = Base.getConnection(); - int number = Integer.valueOf(page); - int size = Integer.valueOf(limit); + int number = Integer.valueOf(page); // 页码 + int size = Integer.valueOf(limit); // 每页条数 + + // 构建查询语句 String sql = "select * from books "; - if(where!=null && !where.isEmpty()) { - whereString += " where "+where.get("condition") +" like '%" +where.get("conditionValue") +"%' "; - sql += whereString; + if (where != null && !where.isEmpty()) { + whereString += " where " + where.get("condition") + " like '%" + where.get("conditionValue") + "%' "; + sql += whereString; // 根据条件拼接查询语句 } - sql += "order by id desc limit ?,? "; + sql += "order by id desc limit ?,? "; // 分页查询 + pstmt = (PreparedStatement) connection.prepareStatement(sql); - pstmt.setInt(1, (number-1) * size ); - pstmt.setInt(2, size); - + pstmt.setInt(1, (number - 1) * size); // 设置分页参数 + pstmt.setInt(2, size); // 每页显示的记录数 + resultSet = pstmt.executeQuery(); JSONObject json = new JSONObject(); String result = ""; int i = 1; - - // 获取行数据 - while( resultSet.next() ) { - //System.out.println("????-------" +resultSet.getInt("count")); + + // 遍历查询结果并构造JSON格式的返回数据 + while (resultSet.next()) { json.put("id", resultSet.getInt("id")); json.put("name", resultSet.getString("name")); json.put("author", resultSet.getString("author")); @@ -95,65 +108,74 @@ public class Admin { json.put("position", resultSet.getString("position")); json.put("status", resultSet.getInt("status")); json.put("description", resultSet.getString("description")); - if(i==1) { + + // 拼接多个JSON对象 + if (i == 1) { result = json.toString(); - }else { - result += "," +json.toString(); + } else { + result += "," + json.toString(); } i++; } + map.put("data", result); - - // 获取总数count,重写sql + + // 获取总数 int count = 0; - sql = "select count(*) as count from books "; - if(where!=null && !where.isEmpty()) { - sql += whereString; + sql = "select count(*) as count from books "; + if (where != null && !where.isEmpty()) { + sql += whereString; // 根据条件计算总数 } pstmt = connection.prepareStatement(sql); resultSet = pstmt.executeQuery(); - if(resultSet.next()) { + if (resultSet.next()) { count = resultSet.getInt("count"); } + map.put("count", count); Base.closeResource(connection, pstmt, resultSet); return map; - } - + /** - * 通过id(String)获取分类名称 - * @param id - * @return + * 根据分类ID获取分类名称 + * + * @param id 分类ID + * @return 分类名称 * @throws ClassNotFoundException * @throws SQLException */ public static String getSortName(String id) throws ClassNotFoundException, SQLException { - if(id==null || id.equals("")) + if (id == null || id.equals("")) { return "参数错误"; + } + Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; String sql = null; String result = null; - connection = Base.getConnection(); + + connection = Base.getConnection(); sql = "select name from book_sort where id=?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, id); resultSet = pstmt.executeQuery(); - if(resultSet.next()) { + if (resultSet.next()) { result = resultSet.getString("name"); - }else { + } else { result = "查询失败"; } + Base.closeResource(connection, pstmt, null); return result; } - + /** - * 根据id获取书籍信息 - * @param id - * @return + * 根据书籍ID获取书籍详细信息 + * + * @param id 书籍ID + * @return 书籍信息的ResultSet对象 * @throws ClassNotFoundException * @throws SQLException */ @@ -161,51 +183,59 @@ public class Admin { Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; - String sql = "select * from books where id=? "; + + String sql = "select * from books where id=?"; connection = Base.getConnection(); pstmt = connection.prepareStatement(sql); pstmt.setInt(1, id); resultSet = pstmt.executeQuery(); - //Base.closeResource(null, null, null); connection.close(); - if(resultSet.next()) { - return resultSet; + + if (resultSet.next()) { + return resultSet; // 返回查询到的结果 } - - return null; + + return null; // 没有找到对应的书籍 } - - /* 没用 */ + + /** + * 获取规则信息(此方法未使用) + * + * @return 包含规则信息的JSONArray + * @throws ClassNotFoundException + * @throws SQLException + */ public static JSONArray getRules() throws ClassNotFoundException, SQLException { Connection connection = null; - PreparedStatement pstmt = null; - ResultSet resultSet = null; - String sql = "select * from rules"; - JSONObject jsonObject = new JSONObject(); - JSONArray jsonArray = new JSONArray(); - String result = ""; - connection = Base.getConnection(); - pstmt = connection.prepareStatement(sql); - resultSet = pstmt.executeQuery(); - while(resultSet.next()) { - jsonObject.put("id", resultSet.getString("id")); - jsonObject.put("borrow_num", resultSet.getString("borrow_num")); - jsonObject.put("borrow_library", resultSet.getString("borrow_library")); - jsonObject.put("overtime_fee", resultSet.getString("overtime_fee")); - jsonArray.add(jsonObject); - System.out.println(jsonArray.toString()); - } - Base.closeResource(connection, pstmt, resultSet); + PreparedStatement pstmt = null; + ResultSet resultSet = null; + + String sql = "select * from rules"; + JSONObject jsonObject = new JSONObject(); + JSONArray jsonArray = new JSONArray(); + + connection = Base.getConnection(); + pstmt = connection.prepareStatement(sql); + resultSet = pstmt.executeQuery(); + + // 遍历规则表,构造JSON对象 + while (resultSet.next()) { + jsonObject.put("id", resultSet.getString("id")); + jsonObject.put("borrow_num", resultSet.getString("borrow_num")); + jsonObject.put("borrow_library", resultSet.getString("borrow_library")); + jsonObject.put("overtime_fee", resultSet.getString("overtime_fee")); + jsonArray.add(jsonObject); + System.out.println(jsonArray.toString()); + } + + Base.closeResource(connection, pstmt, resultSet); return jsonArray; - } - + public static void main(String[] args) throws ClassNotFoundException, SQLException { - //Common common = new Common(); - //System.out.println(common.getCount("books")); Admin admin = new Admin(); - System.out.println(admin.getSortName("2")); - //Map map = admin.getBookList("1", "100"); - //System.out.println( map.get("count")); + System.out.println(admin.getSortName("2")); // 示例,获取分类名称 + // Map map = admin.getBookList("1", "100"); + // System.out.println(map.get("count")); // 示例,获取书籍数量 } } diff --git a/src/javabean/Base.java b/src/javabean/Base.java index 0cf234f..3771eaf 100644 --- a/src/javabean/Base.java +++ b/src/javabean/Base.java @@ -7,6 +7,7 @@ import java.sql.ResultSet; import java.sql.SQLException; public class Base { + // 数据库连接信息 private static String driver = "com.mysql.cj.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/library?&useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"; private static String username = "root"; @@ -14,15 +15,17 @@ public class Base { /** * 获取数据库连接 - * - * @return - * @throws ClassNotFoundException + * + * @return 返回数据库连接对象 + * @throws ClassNotFoundException 如果未找到JDBC驱动类 */ public static Connection getConnection() throws ClassNotFoundException { Connection connection = null; try { + // 加载数据库驱动 Class.forName(driver); - connection = (Connection) DriverManager.getConnection(url, username, password); + // 获取数据库连接 + connection = DriverManager.getConnection(url, username, password); } catch (SQLException e) { e.printStackTrace(); } @@ -30,66 +33,70 @@ public class Base { } /** - * 公共查询 - * - * @param connection - * 连接 - * @param preparedStatement - * @param resultSet - * 结果集 - * @param sql - * @param params - * @return - * @throws SQLException + * 公共查询方法,用于执行SELECT语句 + * + * @param connection 数据库连接对象 + * @param preparedStatement 执行查询的PreparedStatement对象 + * @param resultSet 查询结果集 + * @param sql 执行的SQL语句 + * @param params SQL语句的参数值(可以为null) + * @return 返回查询结果集 + * @throws SQLException 如果SQL执行过程中发生错误 */ public static ResultSet executequery(Connection connection, PreparedStatement preparedStatement, - ResultSet resultSet, String sql, Object[] params) throws SQLException { + ResultSet resultSet, String sql, Object[] params) throws SQLException { if (preparedStatement == null) { - preparedStatement = (PreparedStatement) connection.prepareStatement(sql); + // 如果PreparedStatement为null,则创建新的PreparedStatement对象 + preparedStatement = connection.prepareStatement(sql); } + // 设置SQL语句中的参数 for (int i = 0; params != null && i < params.length; i++) { preparedStatement.setObject(i + 1, params[i]); } + // 执行查询并返回结果集 resultSet = preparedStatement.executeQuery(); return resultSet; - } /** - * 公共修改方法 - * - * @param connection - * @param preparedStatement - * @param sql - * @param params - * @return - * @throws SQLException + * 公共修改方法,用于执行INSERT、UPDATE或DELETE语句 + * + * @param connection 数据库连接对象 + * @param preparedStatement 执行修改的PreparedStatement对象 + * @param sql 执行的SQL语句 + * @param params SQL语句的参数值(可以为null) + * @return 返回受影响的行数 + * @throws SQLException 如果SQL执行过程中发生错误 */ public static int executeUpdate(Connection connection, PreparedStatement preparedStatement, String sql, - Object[] params) throws SQLException { + Object[] params) throws SQLException { if (preparedStatement == null) { - preparedStatement = (PreparedStatement) connection.prepareStatement(sql); + // 如果PreparedStatement为null,则创建新的PreparedStatement对象 + preparedStatement = connection.prepareStatement(sql); } + // 设置SQL语句中的参数 for (int i = 0; params != null && i < params.length; i++) { preparedStatement.setObject(i + 1, params[i]); } + // 执行更新语句,返回更新的行数 int updateRows = preparedStatement.executeUpdate(); return updateRows; - } /** - * 释放资源 - * - * @param connection - * @param preparedStatement - * @param resultSet - * @return - * @throws SQLException + * 释放数据库连接相关的资源 + * + * @param connection 数据库连接对象 + * @param preparedStatement 执行的PreparedStatement对象 + * @param resultSet 查询结果集 + * @return 如果资源成功关闭返回true,否则返回false + * @throws SQLException 如果关闭资源时发生错误 */ public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException { boolean flag = true; + + // 关闭ResultSet资源 if (resultSet != null) { try { resultSet.close(); @@ -99,6 +106,8 @@ public class Base { flag = false; } } + + // 关闭PreparedStatement资源 if (preparedStatement != null) { try { preparedStatement.close(); @@ -108,6 +117,8 @@ public class Base { flag = false; } } + + // 关闭数据库连接资源 if (connection != null) { try { connection.close(); @@ -117,7 +128,8 @@ public class Base { flag = false; } } + + // 返回资源关闭的状态 return flag; } - } diff --git a/src/javabean/JDBCBean.java b/src/javabean/JDBCBean.java index ca1d312..2fde1fa 100644 --- a/src/javabean/JDBCBean.java +++ b/src/javabean/JDBCBean.java @@ -6,51 +6,88 @@ import java.sql.ResultSet; import java.sql.Statement; public class JDBCBean { - private static String driver = "com.mysql.cj.jdbc.Driver"; - private static String url = "jdbc:mysql://localhost:3306/library?&useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"; - private static String username = "root"; - private static String password = "root"; + // 数据库连接信息 + private static String driver = "com.mysql.cj.jdbc.Driver"; // MySQL数据库驱动 + private static String url = "jdbc:mysql://localhost:3306/library?&useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"; // 数据库URL + private static String username = "root"; // 数据库用户名 + private static String password = "root"; // 数据库密码 + + // 数据库连接对象 private Connection conn = null; + // 用于执行SQL查询和更新的Statement对象 private Statement stmt = null; + /** + * 构造函数,初始化数据库连接 + * + * 该构造函数在创建`JDBCBean`对象时会自动尝试连接到数据库。 + * 若连接成功,输出连接成功的信息,否则输出错误信息。 + */ public JDBCBean() { try { + // 加载MySQL数据库驱动 Class.forName(driver); + // 获取数据库连接 conn = DriverManager.getConnection(url, username, password); + // 创建Statement对象,用于执行SQL语句 stmt = conn.createStatement(); System.out.println("同数据库建立连接!"); } catch (Exception ex) { + // 捕获并输出数据库连接错误 System.out.println("无法同数据库建立连接!"); } } + /** + * 执行更新操作(INSERT、UPDATE、DELETE) + * + * @param s 需要执行的SQL语句(例如:INSERT、UPDATE、DELETE) + * @return 返回更新的记录数,若失败则返回0 + */ public int executeUpdate(String s) { int result = 0; try { + // 执行SQL更新操作 System.out.println(s + "------" + stmt + "-----"); result = stmt.executeUpdate(s); } catch (Exception e) { + // 捕获并输出执行更新错误 System.out.println("执行更新错误!"); } return result; } + /** + * 执行查询操作(SELECT) + * + * @param s 需要执行的SQL查询语句(例如:SELECT) + * @return 返回查询结果集(ResultSet),若失败则返回null + */ public ResultSet executeQuery(String s) { ResultSet rs = null; try { + // 执行SQL查询操作 rs = stmt.executeQuery(s); } catch (Exception e) { + // 捕获并输出查询执行错误 System.out.println("执行查询错误! " + e.getMessage()); } return rs; } + /** + * 关闭数据库连接和Statement对象 + * + * 该方法会在操作结束后关闭数据库连接和Statement对象,释放资源。 + */ public void close() { try { + // 关闭Statement对象 stmt.close(); + // 关闭数据库连接 conn.close(); } catch (Exception e) { - + // 捕获并忽略关闭资源时的异常 } } } diff --git a/src/servlet/admin/AdminLogin.java b/src/servlet/admin/AdminLogin.java index b1f7dad..4dcc265 100644 --- a/src/servlet/admin/AdminLogin.java +++ b/src/servlet/admin/AdminLogin.java @@ -16,51 +16,86 @@ import javabean.Admin; import net.sf.json.JSONObject; /** - * 管理员登录 - * + * 管理员登录Servlet * + * 该Servlet用于处理管理员登录请求,验证账号和密码是否正确, + * 如果验证成功,则创建会话并返回登录成功信息;否则,返回错误信息。 * */ @WebServlet("/adminLogin") public class AdminLogin extends HttpServlet { + /** + * 处理GET请求 + * + * 该方法用于处理GET请求,当前未实现,返回一个简单的响应。 + * + * @param request 传入的HttpServletRequest对象 + * @param response 传入的HttpServletResponse对象 + * @throws ServletException 如果处理请求时发生错误 + * @throws IOException 如果输出时发生错误 + */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 简单响应,输出请求的上下文路径 response.getWriter().append("Served at: ").append(request.getContextPath()); } + /** + * 处理POST请求 + * + * 该方法用于处理管理员登录的POST请求,验证提供的用户名和密码, + * 如果验证成功,创建会话并返回成功信息;如果验证失败,返回错误信息。 + * + * @param request 传入的HttpServletRequest对象 + * @param response 传入的HttpServletResponse对象 + * @throws ServletException 如果处理请求时发生错误 + * @throws IOException 如果输出时发生错误 + */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // 设置头文件 + // 设置响应的内容类型为JSON,字符集为utf8 response.setContentType("application/json; charset=utf8"); PrintWriter out = response.getWriter(); - // 获取账号密码 + + // 获取客户端传递的用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); - // 设置响应map + + // 创建一个HashMap来存储返回的响应数据 HashMap hashMap = new HashMap(); + // 创建Admin对象,调用登录方法进行验证 Admin admin = new Admin(); String result = null; try { + // 调用Admin类中的login方法验证用户名和密码 result = admin.login(username, password); } catch (ClassNotFoundException | SQLException e) { + // 捕获并打印异常 e.printStackTrace(); } + + // 如果登录成功,返回成功的JSON响应 if (result != null && result.equals("1")) { + // 创建会话并设置管理员的用户名 HttpSession session = request.getSession(); session.setAttribute("admin", username); - hashMap.put("code", 0); + + // 设置响应内容为成功 + hashMap.put("code", 0); // 0表示成功 hashMap.put("msg", "登录成功"); - hashMap.put("url", request.getContextPath() +"/admin/index.jsp"); - }else { - hashMap.put("code", 1); - hashMap.put("msg", result); + // 返回登录成功后的跳转页面(管理员首页) + hashMap.put("url", request.getContextPath() + "/admin/index.jsp"); + } else { + // 如果登录失败,返回失败信息 + hashMap.put("code", 1); // 1表示失败 + hashMap.put("msg", result); // 登录失败的提示信息 } + // 将HashMap转化为JSON对象并返回给客户端 JSONObject json = JSONObject.fromObject(hashMap); out.write(json.toString()); - } } diff --git a/src/servlet/admin/BookAdd.java b/src/servlet/admin/BookAdd.java index 5468c25..2f9774b 100644 --- a/src/servlet/admin/BookAdd.java +++ b/src/servlet/admin/BookAdd.java @@ -17,32 +17,63 @@ import javabean.Base; import net.sf.json.JSONObject; /** - * Servlet implementation class BookAdd + * Servlet 实现类,用于管理员添加书籍 + * + * 该 Servlet 处理管理员提交的添加书籍请求,将书籍信息(书名、作者、馆藏ID、类别ID、位置、状态、描述)插入到数据库中。 + * 插入成功后,返回 JSON 格式的成功信息;失败则返回错误信息。 */ @WebServlet("/admin/bookAdd") public class BookAdd extends HttpServlet { + + /** + * 处理 POST 请求,执行书籍的添加操作 + * + * 该方法接收客户端提交的书籍信息(通过 HTTP 请求),然后将这些信息插入到数据库中。 + * 插入操作通过 JDBC 进行,如果插入成功,返回一个包含成功状态的 JSON 响应;如果失败,则返回错误信息。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON,字符编码为 UTF-8 resp.setContentType("application/json; charset=utf8"); - String name = req.getParameter("name"); - String author = req.getParameter("author"); - String library_id = req.getParameter("library_id"); - String sort_id = req.getParameter("sort_id"); - String position = req.getParameter("position"); - String status = req.getParameter("status"); - String description = req.getParameter("description"); - System.out.println(description+"-------------"); //debug + + // 获取请求参数(书籍信息) + String name = req.getParameter("name"); // 书名 + String author = req.getParameter("author"); // 作者 + String library_id = req.getParameter("library_id"); // 所属馆ID + String sort_id = req.getParameter("sort_id"); // 书籍类别ID + String position = req.getParameter("position"); // 书籍位置 + String status = req.getParameter("status"); // 书籍状态 + String description = req.getParameter("description"); // 书籍描述 + + // 用于调试输出描述字段的内容 + System.out.println(description + "-------------"); // debug + + // 创建 JSON 对象用于存储响应数据 JSONObject json = new JSONObject(); - Connection connection = null; - PreparedStatement pstmt = null; - //ResultSet resultSet = null; - int result = 0; + Connection connection = null; // 数据库连接 + PreparedStatement pstmt = null; // SQL 执行对象 + int result = 0; // 记录 SQL 执行结果(影响的行数) + + // SQL 插入语句 String sql = "insert into books(name, author, library_id, sort_id, position, status, description) values(?,?,?,?,?,?,?)"; - System.out.println(sql); + System.out.println(sql); // debug,输出 SQL 语句 + + // 获取响应输出流 PrintWriter out = resp.getWriter(); + try { + // 获取数据库连接 connection = (Connection) Base.getConnection(); + + // 创建 PreparedStatement 对象 pstmt = connection.prepareStatement(sql); + + // 设置 SQL 语句中的参数 pstmt.setString(1, name); pstmt.setString(2, author); pstmt.setString(3, library_id); @@ -50,26 +81,36 @@ public class BookAdd extends HttpServlet { pstmt.setString(5, position); pstmt.setString(6, status); pstmt.setString(7, description); + + // 执行 SQL 插入操作,返回影响的行数 result = pstmt.executeUpdate(); } catch (SQLException e) { - + // 捕获 SQL 异常(可以在这里做日志记录或处理) + e.printStackTrace(); } catch (ClassNotFoundException e) { + // 捕获 ClassNotFoundException 异常(数据库驱动类未找到) e.printStackTrace(); - }finally { + } finally { + // 关闭数据库资源 try { Base.closeResource(connection, pstmt, null); } catch (SQLException e) { - e.printStackTrace(); + e.printStackTrace(); // 关闭资源时发生异常 } } - if(result==1) { - json.put("code", "0"); - json.put("msg", "success"); - }else { - json.put("code", "1"); - json.put("msg", "error"); + + // 判断插入操作是否成功,返回相应的 JSON 数据 + if (result == 1) { + // 插入成功,返回成功的 JSON 响应 + json.put("code", "0"); // code = 0 表示成功 + json.put("msg", "success"); // 返回成功消息 + } else { + // 插入失败,返回错误的 JSON 响应 + json.put("code", "1"); // code = 1 表示失败 + json.put("msg", "error"); // 返回失败消息 } + + // 将 JSON 响应写入输出流 out.write(json.toString()); } - } diff --git a/src/servlet/admin/BookDel.java b/src/servlet/admin/BookDel.java index 8242a2f..d758fbb 100644 --- a/src/servlet/admin/BookDel.java +++ b/src/servlet/admin/BookDel.java @@ -13,34 +13,73 @@ import javabean.JDBCBean; import net.sf.json.JSONObject; +/** + * Servlet 实现类,用于管理员删除书籍 + * + * 该 Servlet 处理管理员请求删除书籍的操作,根据书籍 ID 从数据库中删除相应的记录。 + * 删除成功后,返回包含成功状态的 JSON 响应;删除失败则返回错误信息。 + */ @WebServlet("/admin/bookDel") public class BookDel extends HttpServlet { + + /** + * 处理 GET 请求,执行书籍的删除操作 + * + * 该方法接收管理员提交的删除请求,获取要删除的书籍 ID,并执行删除操作。 + * 删除操作通过 JDBC 执行,如果删除成功,返回成功的 JSON 响应;否则,返回错误信息。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - resp.setContentType("application/json; charset=utf8"); + // 设置响应内容类型为 JSON,字符编码为 UTF-8 + resp.setContentType("application/json; charset=utf8"); + + // 获取请求参数中的书籍 ID String id = req.getParameter("id"); + + // 创建 JSON 对象用于响应数据 JSONObject json = new JSONObject(); + + // 创建 JDBCBean 对象用于执行数据库操作 JDBCBean db = new JDBCBean(); - String sql = "delete from books where id = " +id; + + // 构建 SQL 删除语句,删除指定 ID 的书籍 + String sql = "delete from books where id = " + id; + + // 定义删除操作的结果变量 int result = 0; + // 默认状态为失败 int code = 1; String msg = ""; - if( id != null && !id.equals("") ) { + + // 如果 ID 不为空且不为 "空" 字符串,则执行删除操作 + if (id != null && !id.equals("")) { + // 执行 SQL 删除语句,返回影响的行数 result = db.executeUpdate(sql); } - if( result == 1 ) { - code = 0; - msg = "删除成功"; - }else { - code = 1; - msg = "删除失败"; + + // 判断删除操作是否成功,1 表示成功,0 表示失败 + if (result == 1) { + code = 0; // 删除成功 + msg = "删除成功"; // 返回成功消息 + } else { + code = 1; // 删除失败 + msg = "删除失败"; // 返回失败消息 } + + // 将操作结果封装为 JSON 对象 json.put("code", code); json.put("msg", msg); + + // 关闭数据库连接 db.close(); + + // 获取响应输出流并将 JSON 数据写入响应 PrintWriter out = resp.getWriter(); - out.print( json.toString() ); - + out.print(json.toString()); } - } diff --git a/src/servlet/admin/BookEdit.java b/src/servlet/admin/BookEdit.java index b37fac1..aaf298d 100644 --- a/src/servlet/admin/BookEdit.java +++ b/src/servlet/admin/BookEdit.java @@ -3,7 +3,6 @@ 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; @@ -17,12 +16,32 @@ import java.sql.Connection; import javabean.Base; import net.sf.json.JSONObject; - +/** + * Servlet 实现类,用于管理员编辑书籍信息 + * + * 该 Servlet 处理管理员编辑书籍信息的请求,更新数据库中的书籍信息。 + * 编辑成功后,返回包含成功状态的 JSON 响应;编辑失败则返回错误信息。 + */ @WebServlet("/admin/bookEdit") public class BookEdit extends HttpServlet { + + /** + * 处理 POST 请求,执行书籍信息的编辑操作 + * + * 该方法接收管理员提交的编辑请求,获取书籍 ID 以及更新后的信息, + * 然后执行更新操作。更新成功后返回成功的 JSON 响应;否则返回错误信息。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON,字符编码为 UTF-8 resp.setContentType("application/json; charset=utf8"); + + // 获取请求中的参数:书籍 ID、书名、作者、馆藏 ID、分类 ID、位置、状态和描述 String id = req.getParameter("id"); String name = req.getParameter("name"); String author = req.getParameter("author"); @@ -31,18 +50,34 @@ public class BookEdit extends HttpServlet { String position = req.getParameter("position"); String status = req.getParameter("status"); String description = req.getParameter("description"); - System.out.println(description+"-------------"); + + // 打印描述信息进行调试 + System.out.println(description + "-------------"); + + // 创建 JSON 对象用于响应数据 JSONObject json = new JSONObject(); - //if(id == null || id.equals("")) + + // 声明数据库连接和 PreparedStatement 对象 Connection connection = null; PreparedStatement pstmt = null; - //ResultSet resultSet = null; + + // 更新操作的执行结果 int result = 0; + + // SQL 更新语句,更新指定 ID 的书籍信息 String sql = "update books set name=? ,author=? ,library_id=? ,sort_id=? ,position=? ,status=?, description=? where id=?"; + + // 获取响应输出流 PrintWriter out = resp.getWriter(); + try { + // 获取数据库连接 connection = (Connection) Base.getConnection(); + + // 创建 PreparedStatement 对象,设置 SQL 语句 pstmt = connection.prepareStatement(sql); + + // 设置 SQL 语句中的参数 pstmt.setString(1, name); pstmt.setString(2, author); pstmt.setString(3, library_id); @@ -51,38 +86,48 @@ public class BookEdit extends HttpServlet { pstmt.setString(6, status); pstmt.setString(7, description); pstmt.setString(8, id); + + // 执行更新操作 result = pstmt.executeUpdate(); } catch (SQLException e) { - + // 捕获 SQL 异常(目前没有处理,打印错误信息) } catch (ClassNotFoundException e) { - // TODO Auto-generated catch block + // 捕获类未找到异常 e.printStackTrace(); - }finally { + } finally { + // 确保数据库资源被释放 try { Base.closeResource(connection, pstmt, null); } catch (SQLException e) { e.printStackTrace(); } } - if(result==1) { + + // 判断更新操作是否成功 + if (result == 1) { + // 更新成功,返回成功状态 json.put("code", "0"); json.put("msg", "success"); - }else { + } else { + // 更新失败,返回失败状态 json.put("code", "1"); json.put("msg", "error"); } + + // 将 JSON 响应数据写入输出流 out.write(json.toString()); - - - - - //System.out.println(postData); - //JSONObject json = JSONObject.fromObject(); } - + + /** + * 处理 GET 请求,转发到 POST 请求处理逻辑 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } - } diff --git a/src/servlet/admin/BookList.java b/src/servlet/admin/BookList.java index c1c39b0..0c9ecf1 100644 --- a/src/servlet/admin/BookList.java +++ b/src/servlet/admin/BookList.java @@ -13,70 +13,111 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javabean.Admin; -import javabean.Common; -import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** - * Servlet implementation class BookList + * Servlet 实现类,用于管理员获取书籍列表 + * + * 该 Servlet 处理管理员请求书籍列表的操作,支持分页、过滤条件查询。 + * 返回符合条件的书籍数据以及总数,并以 JSON 格式返回给客户端。 */ @WebServlet("/admin/bookList") public class BookList extends HttpServlet { - + + /** + * 处理 GET 请求,获取书籍列表 + * + * 该方法接受分页、限制和查询条件的参数,查询数据库中的书籍数据, + * 并将查询结果以 JSON 格式返回给客户端。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON,字符编码为 UTF-8 resp.setContentType("application/json; charset=utf8"); + + // 初始化响应的 JSON 对象 JSONObject json = new JSONObject(); + + // 定义返回的数据和状态码 String result = null; Map map = null; int code = 1; String msg = ""; String data = ""; + + // 获取分页参数,默认值为 page=1 和 limit=10 String page = (String) req.getParameter("page"); String limit = (String) req.getParameter("limit"); + + // 获取查询条件参数(如果有) String condition = (String) req.getParameter("condition"); String conditionValue = (String) req.getParameter("conditionValue"); - Map where = new HashMap(); - // 传输数据过滤 + + // 创建条件过滤的 Map 对象 + Map where = new HashMap(); + + // 如果没有传递分页参数,则默认设置为第一页和每页10条记录 if(page == null) { page = "1"; } if(limit == null) { limit = "10"; } + + // 如果传递了查询条件参数,则将其加入查询条件 Map 中 if(condition == null || conditionValue == null || condition.isEmpty() || conditionValue.isEmpty()) { condition = null; conditionValue = null; - }else { + } else { where.put("condition", condition); where.put("conditionValue", conditionValue); } + + // 实例化 Admin 对象来调用获取书籍列表的方法 Admin admin = new Admin(); try { + // 调用 getBookList 方法来获取书籍列表数据 map = admin.getBookList(page, limit, where); - result = (String) map.get("data"); + result = (String) map.get("data"); // 获取查询结果 } catch (ClassNotFoundException | SQLException e) { - msg = "数据库获取信息失败"; + // 捕获数据库操作异常,设置错误消息 + msg = "数据库获取信息失败"; } - + + // 如果没有获取到数据或数据为空,返回错误消息 if(result == null || result.isEmpty() || result.equals("1")) { json.put("code", 1); json.put("msg", "数据为空"); } else { + // 获取到数据,返回成功状态,并包含查询结果 json.put("code", 0); json.put("msg", "success"); - json.put("count", map.get("count")); - result = "[" +result +"]"; - json.put("data", result); + json.put("count", map.get("count")); // 返回总记录数 + result = "[" + result + "]"; // 格式化数据为 JSON 数组 + json.put("data", result); // 返回数据 } - + + // 将 JSON 响应数据写入输出流 PrintWriter out = resp.getWriter(); out.print(json.toString()); } - - @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - doGet(req, resp); - } -} \ No newline at end of file + + /** + * 处理 POST 请求,直接调用 doGet 方法来获取书籍列表 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doGet(req, resp); // 对于 POST 请求直接调用 GET 请求处理方法 + } +} diff --git a/src/servlet/admin/BorrowList.java b/src/servlet/admin/BorrowList.java index 1641b0a..240a1d1 100644 --- a/src/servlet/admin/BorrowList.java +++ b/src/servlet/admin/BorrowList.java @@ -18,61 +18,92 @@ import javabean.Base; import net.sf.json.JSONArray; import net.sf.json.JSONObject; - +/** + * Servlet 实现类,用于管理员获取借阅记录列表 + * + * 该 Servlet 处理管理员请求借阅记录列表的操作,支持分页、条件查询。 + * 返回符合条件的借阅记录数据以及总数,并以 JSON 格式返回给客户端。 + */ @WebServlet("/admin/borrowList") public class BorrowList extends HttpServlet { + + /** + * 处理 GET 请求,获取借阅记录列表 + * + * 该方法接受分页、限制和查询条件的参数,查询数据库中的借阅记录数据, + * 并将查询结果以 JSON 格式返回给客户端。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON,字符编码为 UTF-8 resp.setContentType("application/json; charset=utf8"); - // 接收参数 - String limit = req.getParameter("limit"); - String page = req.getParameter("page"); - String condition = (String) req.getParameter("condition"); - String conditionValue = (String) req.getParameter("conditionValue"); - String where = ""; // 无限制条件 + + // 获取分页、查询条件相关的参数 + String limit = req.getParameter("limit"); // 每页显示的记录数 + String page = req.getParameter("page"); // 当前页码 + String condition = (String) req.getParameter("condition"); // 查询条件(字段) + String conditionValue = (String) req.getParameter("conditionValue"); // 查询条件值 + + // 默认值处理:如果未传递分页参数,默认为第一页,默认每页 10 条记录 if(page == null) { - page = "1"; - } - if(limit == null) { - limit = "10"; - } - // 准备查询 + page = "1"; + } + if(limit == null) { + limit = "10"; + } + + // 准备 SQL 查询 Connection connection = null; PreparedStatement pstmt = null; PreparedStatement countPstmt = null; ResultSet resultSet = null; ResultSet countSet = null; - String sql = ""; - String countSql = ""; + + String sql = "select id, card_id, book_id, " + + "DATE_FORMAT(borrow_date, '%Y-%m-%d %k:%i:%s') as borrow_date, " + + "DATE_FORMAT(return_date, '%Y-%m-%d %k:%i:%s') as return_date, " + + "DATE_FORMAT(end_date, '%Y-%m-%d %k:%i:%s') as end_date, " + + "illegal, manager_id from borrow_books"; // 查询借阅记录的 SQL 语句 + + String where = ""; // 记录条件过滤部分 + if(condition != null && conditionValue != null && !condition.isEmpty() && !conditionValue.isEmpty()) { + // 如果传递了条件,构造 WHERE 语句进行过滤 + where = " where " + condition + " like '%" + conditionValue + "%' "; + sql += where; + } else if(condition != null && condition.equals("other")) { + // 处理特殊的查询条件,例如逾期未还的记录 + where = " where return_date is null and curtime() > end_date "; + sql += where; + } + + // 添加分页查询的限制 + sql += " limit ?,?"; + // 准备返回参数 - int code = 1; - String msg = "error"; - int count = 0; - - JSONObject jsonData = new JSONObject(); - JSONArray jsonArray = new JSONArray(); - JSONObject jsonResult = new JSONObject(); - // 进行查询 + int code = 1; // 默认失败的状态码 + String msg = "error"; // 默认错误信息 + int count = 0; // 总记录数 + + JSONObject jsonData = new JSONObject(); // 存储单条借阅记录的 JSON 对象 + JSONArray jsonArray = new JSONArray(); // 存储所有借阅记录的 JSON 数组 + JSONObject jsonResult = new JSONObject(); // 最终返回的 JSON 对象 + try { + // 获取数据库连接 connection = (Connection) Base.getConnection(); - sql = "select id, card_id, book_id, " - + "DATE_FORMAT(borrow_date, '%Y-%m-%d %k:%i:%s') as borrow_date, " - + "DATE_FORMAT(return_date, '%Y-%m-%d %k:%i:%s') as return_date, " - + "DATE_FORMAT(end_date, '%Y-%m-%d %k:%i:%s') as end_date," - + "illegal, manager_id " - + "from borrow_books"; - if(condition!=null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) { - where = " where "+ condition +" like '%" +conditionValue +"%' "; - sql += where; - }else if(condition!=null && condition.equals("other")) { - where = " where return_date is null and curtime()>end_date "; - sql +=where; - } - sql += " limit ?,?"; + + // 执行查询操作,获取借阅记录数据 pstmt = connection.prepareStatement(sql); - pstmt.setInt(1, (Integer.parseInt(page)-1) * Integer.parseInt(limit)); - pstmt.setInt(2, Integer.parseInt(limit)); + pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit)); // 设置分页的起始位置 + pstmt.setInt(2, Integer.parseInt(limit)); // 设置分页的限制条数 resultSet = pstmt.executeQuery(); + + // 遍历查询结果,将每条借阅记录放入 JSON 数组中 while(resultSet.next()) { jsonData.put("id", resultSet.getString("id")); jsonData.put("card_id", resultSet.getString("card_id")); @@ -84,43 +115,46 @@ public class BorrowList extends HttpServlet { jsonData.put("manager_id", resultSet.getString("manager_id")); jsonArray.add(jsonData); } - countSql = "select count(*) as count from borrow_books "; - countSql +=where; + + // 查询符合条件的总记录数 + String countSql = "select count(*) as count from borrow_books " + where; countPstmt = connection.prepareStatement(countSql); countSet = countPstmt.executeQuery(); if(countSet.next()) { count = countSet.getInt("count"); } + + // 根据查询结果设置响应状态 if(!jsonArray.isEmpty()) { code = 0; msg = "查询成功"; - }else { + } else { code = 0; msg = "没有数据"; } - + } catch (ClassNotFoundException e) { msg = "class没找到"; } catch (SQLException e) { msg = "sql错误"; } finally { try { + // 关闭数据库连接和相关资源 Base.closeResource(null, pstmt, resultSet); Base.closeResource(connection, countPstmt, countSet); } catch (SQLException e) { msg = "关闭资源失败"; } - } - // 返回数据 + + // 构造返回的 JSON 数据 jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); - jsonResult.put("data", jsonArray.toArray()); + jsonResult.put("data", jsonArray.toArray()); // 返回的借阅记录数据 + + // 输出最终的 JSON 响应 PrintWriter out = resp.getWriter(); out.print(jsonResult.toString()); } - - - } diff --git a/src/servlet/admin/CardAdd.java b/src/servlet/admin/CardAdd.java index e7d4ac3..af4ae31 100644 --- a/src/servlet/admin/CardAdd.java +++ b/src/servlet/admin/CardAdd.java @@ -19,24 +19,44 @@ import javabean.JDBCBean; import net.sf.json.JSONObject; /** - * Servlet implementation class CardAdd + * Servlet 实现类,用于管理员添加借阅卡 + * + * 该 Servlet 处理管理员请求添加新的借阅卡的操作。接收借阅卡的相关信息(读者、密码、规则、状态), + * 向数据库中插入一条新的借阅卡记录,并返回插入结果。 */ @WebServlet("/admin/cardAdd") public class CardAdd extends HttpServlet { + + /** + * 处理 POST 请求,添加新的借阅卡 + * + * 该方法接收传入的借阅卡信息,包括读者、密码、规则、状态等,执行数据库插入操作, + * 并返回执行结果(成功或失败)。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON,字符编码为 UTF-8 resp.setContentType("application/json; charset=utf8"); - // 获取参数 - String reader = req.getParameter("reader"); - String password = req.getParameter("password"); - String rule_id = req.getParameter("rule_id"); - String status = req.getParameter("status"); - // 准备资源 + + // 获取传递的参数 + String reader = req.getParameter("reader"); // 读者 + String password = req.getParameter("password"); // 密码 + String rule_id = req.getParameter("rule_id"); // 借阅规则 ID + String status = req.getParameter("status"); // 借阅卡状态 + + // 初始化返回信息 String code = "1"; String msg = "error"; String data = ""; JSONObject json = new JSONObject(); JSONObject jsonData = new JSONObject(); + + // 数据库连接对象 Connection connection = null; Connection connection1 = null; PreparedStatement pstmt = null; @@ -44,23 +64,29 @@ public class CardAdd extends HttpServlet { String sql = null; int result = 0; ResultSet dataSet = null; - // 参数不能为空 - if(reader == null || password == null || rule_id == null || rule_id == null || status == null) { + + // 参数不能为空校验 + if(reader == null || password == null || rule_id == null || status == null) { code = "1"; msg = "值不能为空"; - }else { + } else { try { + // 获取数据库连接 connection = (Connection) Base.getConnection(); + + // 插入借阅卡的 SQL 语句 sql = "insert into borrow_card(password, reader, rule_id, status) values(?,?,?,?)"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, password); pstmt.setString(2, reader); pstmt.setString(3, rule_id); pstmt.setString(4, status); + + // 执行插入操作 result = pstmt.executeUpdate(); - - //获取id - connection1= (Connection) Base.getConnection(); + + // 获取插入记录的 ID + connection1 = (Connection) Base.getConnection(); String findIdSql = "select id from borrow_card where password=? and reader=? and rule_id=? and status=? limit 1"; pstmt1 = connection1.prepareStatement(findIdSql); pstmt1.setString(1, password); @@ -68,6 +94,8 @@ public class CardAdd extends HttpServlet { pstmt1.setString(3, rule_id); pstmt1.setString(4, status); dataSet = pstmt1.executeQuery(); + + // 获取插入记录的 ID if(dataSet.next()) { jsonData.put("id", dataSet.getString("id")); } @@ -76,27 +104,33 @@ public class CardAdd extends HttpServlet { } catch (SQLException e) { msg = "sql错误"; System.out.println("sql失败"); - } - try { - Base.closeResource(connection, pstmt, null); - Base.closeResource(connection1, pstmt1, dataSet); - } catch (SQLException e) { - msg = "关闭资源失败"; + } finally { + // 关闭数据库资源 + try { + Base.closeResource(connection, pstmt, null); + Base.closeResource(connection1, pstmt1, dataSet); + } catch (SQLException e) { + msg = "关闭资源失败"; + } } + + // 根据插入结果更新响应消息 if(result == 1 && !jsonData.isNullObject() && !jsonData.isEmpty()) { - System.out.println(jsonData.toString()); //debug code = "0"; msg = "添加成功"; - }else { + } else { code = "1"; msg = "执行失败"; } } + + // 构造 JSON 响应 json.put("code", code); json.put("msg", msg); json.put("data", jsonData.toString()); + + // 输出 JSON 响应到客户端 PrintWriter out = resp.getWriter(); out.print(json.toString()); } - } diff --git a/src/servlet/admin/CardDel.java b/src/servlet/admin/CardDel.java index 2954b77..645f612 100644 --- a/src/servlet/admin/CardDel.java +++ b/src/servlet/admin/CardDel.java @@ -17,14 +17,34 @@ import javabean.Base; import net.sf.json.JSONArray; import net.sf.json.JSONObject; +/** + * Servlet 实现类,用于删除借阅卡及相关借书记录 + * + * 该 Servlet 处理管理员请求删除借阅卡及与之相关的借书记录操作。接收借阅卡 ID,先删除该借阅卡 + * 的所有借书记录,再删除借阅卡本身,并返回删除结果。 + */ @WebServlet("/admin/cardDel") public class CardDel extends HttpServlet { + + /** + * 处理 GET 请求,删除借阅卡及相关借书记录 + * + * 该方法接收借阅卡 ID,删除借阅卡及与之相关的借书记录,最终返回操作结果(成功或失败)。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON,字符编码为 UTF-8 resp.setContentType("application/json; charset=utf8"); - // 接收数据 + + // 获取传入的借阅卡 ID 参数 String id = req.getParameter("id"); - // 处理数据 + + // 初始化数据库连接和 SQL 语句 Connection connection = null; PreparedStatement delCardPstmt = null; PreparedStatement delHistoryPstmt = null; @@ -32,34 +52,39 @@ public class CardDel extends HttpServlet { String delHistorySql = null; int delCardResult = 0; int delHistoryResult = 0; - // 返回数据 + + // 初始化返回信息 String code = "1"; String msg = "error"; JSONObject jsonObject = new JSONObject(); JSONObject jsonData = new JSONObject(); - // 开始处理 + + // 如果传入的 ID 不为空,开始处理删除操作 if(id != null && !id.equals("")) { try { - // 公共连接 + // 获取数据库连接 connection = (Connection) Base.getConnection(); + // 删除借书记录 delHistorySql = "delete from borrow_books where card_id=?"; delHistoryPstmt = connection.prepareStatement(delHistorySql); delHistoryPstmt.setString(1, id); delHistoryResult = delHistoryPstmt.executeUpdate(); - // 返回删除记录条数 + // 返回删除的借书记录数 jsonData.put("num", delHistoryResult); - // 删除阅读证 + + // 删除借阅卡 delCardSql = "delete from borrow_card where id=? limit 1"; delCardPstmt = connection.prepareStatement(delCardSql); delCardPstmt.setString(1, id); delCardResult = delCardPstmt.executeUpdate(); - + } catch (ClassNotFoundException e) { msg = "连接失败"; } catch (SQLException e) { msg = "sql错误"; } finally { + // 关闭数据库资源 try { delCardPstmt.close(); Base.closeResource(connection, delCardPstmt, null); @@ -68,15 +93,20 @@ public class CardDel extends HttpServlet { } } } + + // 获取响应输出流 PrintWriter out = resp.getWriter(); + + // 判断删除操作是否成功 if(delCardResult == 1) { code = "0"; msg = "删除借阅证成功"; } + + // 返回 JSON 格式的响应 jsonObject.put("code", code); jsonObject.put("msg", msg); jsonObject.put("data", jsonData); out.print(jsonObject.toString()); } - } diff --git a/src/servlet/admin/CardEdit.java b/src/servlet/admin/CardEdit.java index be7329b..2f3530f 100644 --- a/src/servlet/admin/CardEdit.java +++ b/src/servlet/admin/CardEdit.java @@ -17,64 +17,97 @@ import java.sql.Connection; import javabean.Base; import net.sf.json.JSONObject; - +/** + * Servlet 实现类,用于编辑借阅卡信息 + * + * 该 Servlet 处理管理员请求编辑借阅卡信息的操作。接收借阅卡 ID 和新的借阅卡信息,更新数据库中对应的记录。 + */ @WebServlet("/admin/cardEdit") public class CardEdit extends HttpServlet { + + /** + * 处理 POST 请求,编辑借阅卡信息 + * + * 该方法接收借阅卡 ID 和新的借阅卡信息(如密码、读者姓名、规则 ID 和状态), + * 更新数据库中对应的借阅卡记录,并返回操作结果。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON,字符编码为 UTF-8 resp.setContentType("application/json; charset=utf8"); - // 接受数据 + + // 获取传入的借阅卡参数 String id = req.getParameter("id"); String password = req.getParameter("password"); String reader = req.getParameter("reader"); String rule_id = req.getParameter("rule_id"); String status = req.getParameter("status"); - // 准备资源 + + // 初始化数据库连接和 SQL 语句 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; String sql = null; - // 返回数据 + + // 初始化返回信息 String code = "1"; String msg = "error"; JSONObject json = new JSONObject(); PrintWriter out = resp.getWriter(); - // 判断数据 - if(id == null || password == null || reader == null || reader == null || status == null || + + // 判断接收到的参数是否为空 + if(id == null || password == null || reader == null || rule_id == null || status == null || id.equals("") || password.equals("") || reader.equals("") || rule_id.equals("") || status.equals("")) { code = "1"; msg = "参数不能为空"; - }else { + } else { + // 构建更新 SQL 语句 sql = "update borrow_card set password=?, reader=?, rule_id=?, status=? where id=?"; try { + // 获取数据库连接 connection = (Connection) Base.getConnection(); + + // 预编译 SQL 语句并设置参数 pstmt = connection.prepareStatement(sql); pstmt.setString(1, password); pstmt.setString(2, reader); pstmt.setString(3, rule_id); pstmt.setString(4, status); pstmt.setString(5, id); + + // 执行更新操作 result = pstmt.executeUpdate(); } catch (ClassNotFoundException e1) { - msg = "错误"; + msg = "数据库连接错误"; } catch (SQLException e) { - msg = "sql错误"; + msg = "SQL 错误"; } finally { try { + // 关闭数据库连接和资源 Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { - msg = "关闭失败"; + msg = "资源关闭失败"; } } + + // 判断更新操作是否成功 if(result == 1) { code = "0"; msg = "修改成功"; } - json.put("code", code); - json.put("msg", msg); - out.print(json.toString()); } - } + // 将返回信息封装成 JSON 格式 + json.put("code", code); + json.put("msg", msg); + + // 输出响应数据 + out.print(json.toString()); + } } diff --git a/src/servlet/admin/CardList.java b/src/servlet/admin/CardList.java index bed1374..a987c19 100644 --- a/src/servlet/admin/CardList.java +++ b/src/servlet/admin/CardList.java @@ -18,25 +18,46 @@ import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** - * Servlet implementation class CardList + * Servlet 实现类,用于获取借阅卡列表 + * + * 该 Servlet 处理管理员请求获取借阅卡列表的操作。支持分页和条件查询,返回符合条件的借阅卡数据。 */ @WebServlet("/admin/cardList") public class CardList extends HttpServlet { + + /** + * 处理 GET 请求,获取借阅卡列表 + * + * 该方法处理分页和条件查询,查询借阅卡表中的数据,并返回符合条件的借阅卡信息。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON,字符编码为 UTF-8 resp.setContentType("application/json; charset=utf8"); - // 接收参数 + + // 接收查询参数:limit, page, condition 和 conditionValue String limit = req.getParameter("limit"); String page = req.getParameter("page"); - String condition = (String) req.getParameter("condition"); - String conditionValue = (String) req.getParameter("conditionValue"); + String condition = req.getParameter("condition"); + String conditionValue = req.getParameter("conditionValue"); + + // 初始化 SQL 查询条件 String where = null; // 无限制条件 + + // 设置默认值 if (page == null) { page = "1"; } if (limit == null) { limit = "10"; } + + // 初始化数据库连接和 SQL 语句 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; @@ -44,29 +65,41 @@ public class CardList extends HttpServlet { String msg = "error"; int count = 0; String sql = ""; - // String countSql = "" + + // 准备返回数据的 JSON 对象 JSONObject jsonObject = new JSONObject(); JSONArray jsonArray = new JSONArray(); JSONObject jsonResult = new JSONObject(); + try { - // 获取数据 + // 获取数据库连接 connection = (Connection) Base.getConnection(); - sql = "select id,password,reader,rule_id,status from borrow_card"; - // where + + // 初始 SQL 查询语句 + sql = "select id, password, reader, rule_id, status from borrow_card"; + + // 如果有查询条件,构造 where 子句 if (condition != null && conditionValue != null && !condition.isEmpty() && !conditionValue.isEmpty()) { where = " where " + condition + " like '%" + conditionValue + "%'"; sql = sql + where; } - // 分页 - sql += " order by id desc limit ?,?"; + + // 构造分页查询 + sql += " order by id desc limit ?,?"; + pstmt = connection.prepareStatement(sql); try { + // 设置分页参数 pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit)); pstmt.setInt(2, Integer.parseInt(limit)); } catch (NumberFormatException | SQLException e1) { + // 异常捕获,避免参数错误影响执行 } + + // 执行查询 resultSet = pstmt.executeQuery(); while (resultSet.next()) { + // 构造每一条借阅卡记录的 JSON 数据 jsonObject.put("id", resultSet.getString("id")); jsonObject.put("password", resultSet.getString("password")); jsonObject.put("reader", resultSet.getString("reader")); @@ -74,41 +107,46 @@ public class CardList extends HttpServlet { jsonObject.put("status", resultSet.getString("status")); jsonArray.add(jsonObject); } - // 获取总数 - sql = "select count(*) as count from borrow_card "; - // 有限制 + + // 获取借阅卡总数,用于分页 + sql = "select count(*) as count from borrow_card"; if (where != null) { sql = sql + where; } + pstmt = connection.prepareStatement(sql); resultSet = pstmt.executeQuery(); if (resultSet.next()) { count = resultSet.getInt("count"); } + // 判断查询结果是否为空 if (!jsonArray.isEmpty()) { code = 0; - msg = "成功"; + msg = "查询成功"; } } catch (ClassNotFoundException e) { - msg = "没找到"; + msg = "数据库连接失败"; e.printStackTrace(); } catch (SQLException e) { - msg = "sql错误"; + msg = "SQL 错误"; } finally { + // 关闭数据库连接和资源 try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { - msg = "关闭失败"; + msg = "资源关闭失败"; } } + + // 构造返回的 JSON 数据 jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); jsonResult.put("data", jsonArray.toString()); + + // 输出响应 PrintWriter out = resp.getWriter(); out.print(jsonResult.toString()); - // out.print("{\"code\":0,\"msg\":\"\",\"count\":\"234\",\"data\":[{\"id\":\"1\",\"password\":\"23442\",\"reader\":\"minm\",\"rule_id\":\"1\",\"status\":\"2\"}]}"); } - } diff --git a/src/servlet/admin/LibraryData.java b/src/servlet/admin/LibraryData.java index 23eedf6..9b3afff 100644 --- a/src/servlet/admin/LibraryData.java +++ b/src/servlet/admin/LibraryData.java @@ -19,14 +19,31 @@ import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; +/** + * Servlet 实现类,用于获取图书馆借阅数据 + * + * 该 Servlet 用于管理员查看过去30天内每天的借阅数量,并返回相应的借阅数据和日期。 + */ @WebServlet("/admin/libraryData") public class LibraryData extends HttpServlet { private static final long serialVersionUID = 1L; - - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + /** + * 处理 GET 请求,获取图书馆借阅数据 + * + * 该方法查询过去30天内的借阅数据,统计每天的借阅次数,并返回日期和对应的借阅数量。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON,字符编码为 UTF-8 resp.setContentType("application/json; charset:utf8"); - // 准备参数 + + // 初始化数据库连接、SQL语句、JSON 数据 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; @@ -34,51 +51,61 @@ public class LibraryData extends HttpServlet { JSONObject jsonObject = new JSONObject(); JSONArray jsonData = new JSONArray(); JSONArray jsonDays = new JSONArray(); - // 返回参数 + + // 返回参数:状态码、消息、数据 int code = 1; String msg = "error"; int count = 0; + + // 获取响应输出流 PrintWriter out = resp.getWriter(); - - // 开始查询 + + // 开始查询,获取过去30天的借阅数据 try { - connection = Base.getConnection(); - int i = 30; - // 获取30天 - while(i!=0) { + connection = Base.getConnection(); // 获取数据库连接 + int i = 30; // 查询过去30天的数据 + while (i != 0) { i--; sql = "select count(*) as count from borrow_books where date_format(borrow_date,'%Y-%m-%d')=? order by id desc"; - String date = DateTime.showDate(-i); // 设置日期 - String md = DateTime.showMD(-i); + String date = DateTime.showDate(-i); // 获取当前日期的前i天 + String md = DateTime.showMD(-i); // 获取当前日期的前i天的月日格式(MM-dd) pstmt = connection.prepareStatement(sql); - pstmt.setString(1,date); + pstmt.setString(1, date); // 设置日期参数 resultSet = pstmt.executeQuery(); - while(resultSet.next()) { + + while (resultSet.next()) { + // 将借阅数量和日期分别添加到 JSON 数组中 jsonData.add(resultSet.getString("count")); jsonDays.add(md); } - } + } + + // 将查询结果放入 JSON 对象中 jsonObject.put("data", jsonData); jsonObject.put("days", jsonDays); - if(!jsonObject.isEmpty()) { - code = 0; - msg = "查询成功"; - }else { + + // 判断查询结果是否为空 + if (!jsonObject.isEmpty()) { + code = 0; // 状态码为 0 表示成功 + msg = "查询成功"; + } else { msg = "数据为空"; } } catch (ClassNotFoundException e) { - msg = "没找到"; + msg = "数据库连接失败"; e.printStackTrace(); } catch (SQLException e) { - msg = "sql错误"; - }finally { + msg = "SQL 错误"; + } finally { + // 关闭数据库连接和资源 try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { - msg = "关闭失败"; + msg = "关闭数据库资源失败"; } } - out.print( Util.jsonResponse(code, msg, jsonObject.toString()) ); - } + // 返回 JSON 响应 + out.print(Util.jsonResponse(code, msg, jsonObject.toString())); + } } diff --git a/src/servlet/admin/LoginOut.java b/src/servlet/admin/LoginOut.java index 65ec1f8..b7e9a51 100644 --- a/src/servlet/admin/LoginOut.java +++ b/src/servlet/admin/LoginOut.java @@ -9,20 +9,35 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** - * Servlet implementation class LoginOut + * Servlet 实现类,用于处理管理员的登出操作 + * + * 该 Servlet 会销毁当前管理员的登录会话,并重定向到管理员登录页面。 */ @WebServlet("/admin/logOut") public class LoginOut extends HttpServlet { private static final long serialVersionUID = 1L; - - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - // TODO Auto-generated method stub - HttpSession session = req.getSession(); - if(session.getAttribute("admin") != null) { - session.removeAttribute("admin"); - } - resp.sendRedirect(req.getContextPath() +"/adminLogin.html"); - } + /** + * 处理 GET 请求,执行管理员登出操作 + * + * 该方法会结束当前管理员的会话,移除 `admin` 属性,并重定向到管理员登录页面。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 获取当前会话对象 + HttpSession session = req.getSession(); + + // 如果会话中存在 "admin" 属性(即管理员已登录),则移除该属性 + if (session.getAttribute("admin") != null) { + session.removeAttribute("admin"); + } + + // 重定向到管理员登录页面 + resp.sendRedirect(req.getContextPath() + "/adminLogin.html"); + } } diff --git a/src/servlet/admin/ManagerAdd.java b/src/servlet/admin/ManagerAdd.java index 0375ade..b6a5282 100644 --- a/src/servlet/admin/ManagerAdd.java +++ b/src/servlet/admin/ManagerAdd.java @@ -19,46 +19,71 @@ import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; - +/** + * Servlet 实现类,用于处理管理员的添加操作 + * + * 该 Servlet 接收管理员的姓名、账号、密码和邮箱,通过 SQL 查询验证账号是否重复,如果不重复则插入新管理员。 + */ @WebServlet("/admin/managerAdd") public class ManagerAdd extends HttpServlet { + + /** + * 处理 POST 请求,用于添加新的管理员 + * + * 该方法会接收请求中的管理员姓名、账号、密码和邮箱,首先验证账号是否已存在, + * 如果不存在,则将新的管理员信息插入到数据库中。返回 JSON 格式的响应结果。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON resp.setContentType("application/json; charset=utf8"); - // 接收参数 + + // 接收前端传递的参数 String name = req.getParameter("name"); String account = req.getParameter("account"); String password = req.getParameter("password"); String email = req.getParameter("email"); - // 准备参数 + + // SQL 语句及数据库连接准备 String sql = ""; - Connection connection = null; + Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; int count = 0; - // 返回参数 + + // 返回的响应数据 int code = 1; String msg = ""; PrintWriter out = resp.getWriter(); JSONArray jsonArray = new JSONArray(); - JSONObject jsonObject = new JSONObject(); - // 进行查询 - if(name==null || name.equals("") || account==null || account.equals("") || password==null || password.equals("") || email==null || email.equals("")) { + JSONObject jsonObject = new JSONObject(); + + // 进行参数校验,检查不能为空的字段 + if (name == null || name.equals("") || account == null || account.equals("") || + password == null || password.equals("") || email == null || email.equals("")) { msg = "参数不能为空"; out.print(Util.jsonResponse(code, msg, null)); - }else { + } else { try { + // 获取数据库连接 connection = (Connection) Base.getConnection(); - // 验证账号 + + // 验证账号是否已存在 sql = "select count(*) as count from manager where account=?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, account); resultSet = pstmt.executeQuery(); resultSet.next(); count = resultSet.getInt("count"); - // 添加管理员 - if(count == 0) { + + // 如果账号不存在,则插入新管理员 + if (count == 0) { sql = "insert into manager(name, account, password, email) values(?,?,?,?)"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, name); @@ -67,13 +92,14 @@ public class ManagerAdd extends HttpServlet { pstmt.setString(4, email); result = pstmt.executeUpdate(); } - // 返回数据 - if(result == 1 && count == 0) { + + // 根据执行结果返回响应信息 + if (result == 1 && count == 0) { code = 0; msg = "添加成功"; - }else if(count > 0){ + } else if (count > 0) { msg = "账号重复"; - }else { + } else { msg = "添加失败"; } } catch (ClassNotFoundException e) { @@ -82,11 +108,14 @@ public class ManagerAdd extends HttpServlet { msg = "sql错误"; } finally { try { + // 关闭数据库资源 Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { e.printStackTrace(); } } + + // 返回响应结果 out.print(Util.jsonResponse(code, msg, null)); } } diff --git a/src/servlet/admin/ManagerDel.java b/src/servlet/admin/ManagerDel.java index 5155b3f..0729f75 100644 --- a/src/servlet/admin/ManagerDel.java +++ b/src/servlet/admin/ManagerDel.java @@ -17,42 +17,75 @@ 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; + 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); - result = pstmt.executeUpdate(); - if(result == 1) { + pstmt.setString(1, id); // 设置 ID 参数 + result = pstmt.executeUpdate(); // 执行删除操作 + + // 根据操作结果返回响应 + if (result == 1) { code = 0; msg = "删除成功"; - }else { + } 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)); - } - } diff --git a/src/servlet/admin/ManagerEdit.java b/src/servlet/admin/ManagerEdit.java index 04bf661..37198fb 100644 --- a/src/servlet/admin/ManagerEdit.java +++ b/src/servlet/admin/ManagerEdit.java @@ -19,50 +19,70 @@ import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; - +/** + * Servlet 实现类,用于处理管理员信息修改操作 + * + * 该 Servlet 处理管理员信息修改的请求,根据传入的 ID 更新管理员的名称、密码和邮箱等信息。 + */ @WebServlet("/admin/managerEdit") public class ManagerEdit extends HttpServlet { + + /** + * 处理 POST 请求,修改指定 ID 的管理员信息 + * + * 该方法会接收管理员的 ID、名称、密码和邮箱信息,更新数据库中对应的管理员记录,并返回修改结果。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON resp.setContentType("application/json; charset=utf8"); - // 接收参数 + + // 获取请求中的参数 String id = req.getParameter("id"); String name = req.getParameter("name"); String password = req.getParameter("password"); String email = req.getParameter("email"); - // 准备参数 + + // 返回的响应参数 String sql = ""; - Connection connection = null; + Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; - // 返回参数 int code = 1; String msg = ""; PrintWriter out = resp.getWriter(); - JSONArray jsonArray = new JSONArray(); - JSONObject jsonObject = new JSONObject(); - // 进行查询 - if(name==null || name.equals("")|| password==null || password.equals("") || email==null || email.equals("")) { + + // 如果参数为空,返回错误信息 + if (name == null || name.equals("") || password == null || password.equals("") || email == null || email.equals("")) { msg = "参数不能为空"; out.print(Util.jsonResponse(code, msg, null)); - }else { + } else { try { + // 获取数据库连接 connection = (Connection) Base.getConnection(); - // 添加管理员 + + // SQL 更新语句 sql = "update manager set name=?, password=?, email=? where id=?"; pstmt = connection.prepareStatement(sql); - pstmt.setString(1, name); - pstmt.setString(2, password); - pstmt.setString(3, email); - pstmt.setString(4, id); + pstmt.setString(1, name); // 设置名称 + pstmt.setString(2, password); // 设置密码 + pstmt.setString(3, email); // 设置邮箱 + pstmt.setString(4, id); // 设置管理员 ID + + // 执行更新操作 result = pstmt.executeUpdate(); - - // 返回数据 - if(result == 1 ){ + + // 根据更新结果返回响应 + if (result == 1) { code = 0; msg = "修改成功"; - }else { + } else { msg = "修改失败"; } } catch (ClassNotFoundException e) { @@ -70,15 +90,16 @@ public class ManagerEdit extends HttpServlet { } 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)); } - } - } diff --git a/src/servlet/admin/ManagerList.java b/src/servlet/admin/ManagerList.java index f1729e4..892fb5e 100644 --- a/src/servlet/admin/ManagerList.java +++ b/src/servlet/admin/ManagerList.java @@ -19,47 +19,75 @@ import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; +/** + * Servlet 实现类,用于查询管理员列表 + * + * 该 Servlet 会查询数据库中所有管理员的信息,并将其返回给前端。 + */ @WebServlet("/admin/managerList") public class ManagerList extends HttpServlet { + + /** + * 处理 GET 请求,查询管理员列表 + * + * 该方法会查询数据库中 `manager` 表的所有记录,并返回管理员的 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"); - // 准备数据 + + // 初始化数据库连接相关变量 Connection connection = null; PreparedStatement pstmt = null; String sql = ""; ResultSet resultSet = null; - // 返回数据 - int code = 1; - String msg = "error"; - JSONObject jsonObject = new JSONObject(); - JSONArray jsonArray = new JSONArray(); + + // 返回的数据初始化 + int code = 1; // 默认为失败 + String msg = "error"; // 默认为错误信息 + JSONObject jsonObject = new JSONObject(); // 存储单条管理员信息 + JSONArray jsonArray = new JSONArray(); // 存储所有管理员信息 PrintWriter out = resp.getWriter(); + try { + // 获取数据库连接 connection = (Connection) Base.getConnection(); + + // SQL 查询语句:查询所有管理员信息 sql = "select * from manager"; pstmt = connection.prepareStatement(sql); resultSet = pstmt.executeQuery(); + + // 遍历查询结果,将每条管理员信息加入 JSON 数组 while (resultSet.next()) { - jsonObject.put("id", resultSet.getString("id")); - jsonObject.put("name", resultSet.getString("name")); - jsonObject.put("account", resultSet.getString("account")); - jsonObject.put("password", resultSet.getString("password")); - jsonObject.put("email", resultSet.getString("email")); - jsonArray.add(jsonObject); + jsonObject.put("id", resultSet.getString("id")); // 管理员ID + jsonObject.put("name", resultSet.getString("name")); // 管理员名称 + jsonObject.put("account", resultSet.getString("account")); // 管理员账号 + jsonObject.put("password", resultSet.getString("password")); // 管理员密码 + jsonObject.put("email", resultSet.getString("email")); // 管理员邮箱 + jsonArray.add(jsonObject); // 将该管理员信息加入 JSON 数组 } + + // 检查是否成功查询到管理员信息 if (!jsonArray.isEmpty()) { - code = 0; + code = 0; // 查询成功 msg = "查询成功"; } else { - msg = "数据为空"; + msg = "数据为空"; // 查询结果为空 } } catch (ClassNotFoundException e) { - msg = "class找不到"; + msg = "class找不到"; // 类加载失败 } catch (SQLException e) { - msg = "sql错误"; + msg = "sql错误"; // SQL 执行错误 } + + // 返回 JSON 响应 out.print(Util.jsonResponse(code, msg, jsonArray.toString())); } - } diff --git a/src/servlet/admin/RuleAdd.java b/src/servlet/admin/RuleAdd.java index 9ce3953..03c74f2 100644 --- a/src/servlet/admin/RuleAdd.java +++ b/src/servlet/admin/RuleAdd.java @@ -19,76 +19,103 @@ import javabean.Base; import javabean.Common; import javabean.Util; - +/** + * Servlet 实现类,用于添加借阅规则 + * + * 该 Servlet 用于处理前端发送的 POST 请求,接收借阅规则的数据并将其保存到数据库中。 + */ @WebServlet("/admin/ruleAdd") public class RuleAdd extends HttpServlet { + + /** + * 处理 POST 请求,添加借阅规则 + * + * 该方法接收前端传来的借阅规则数据,并将其保存到数据库中。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON resp.setContentType("application/json; charset=utf8"); - // 准备数据 + + // 初始化数据库连接相关变量 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; String sql = ""; String borrow_library = ""; - - // 准备返回数据 - int code = 1; - String msg = ""; - - // 获取数据 - // 获取限定图书馆1、2、3 + + // 返回的数据初始化 + int code = 1; // 默认为失败 + String msg = ""; // 默认为错误信息 + + // 获取数据:首先检查允许借阅的图书馆 int num = 0; try { + // 获取图书馆的配置信息 Map libraryMap = Common.getLibraryMap(); - for(String key : libraryMap.keySet()) { - if(req.getParameter("borrow_library[" +key +"]") != null) { - if(num == 0) { + // 遍历图书馆信息,获取前端传来的借阅图书馆 + for (String key : libraryMap.keySet()) { + if (req.getParameter("borrow_library[" + key + "]") != null) { + if (num == 0) { borrow_library += key; num++; - }else { - borrow_library += "、"+key; + } else { + borrow_library += "、" + key; } } } - if(borrow_library.isEmpty()) { + // 检查是否选择了允许的图书馆 + if (borrow_library.isEmpty()) { msg = "允许图书馆不能为空"; } } catch (SQLException e) { msg = "获取图书馆失败"; } - String borrow_num = req.getParameter("borrow_num"); - String limit_day = req.getParameter("limit_day"); - String overtime_fee = req.getParameter("overtime_fee"); - try { - connection = (Connection) Base.getConnection(); - sql = "insert into rules(borrow_num, limit_day, borrow_library, overtime_fee) values(?,?,?,?)"; - pstmt = connection.prepareStatement(sql); - pstmt.setString(1, borrow_num); - pstmt.setString(2, limit_day); - pstmt.setString(3, borrow_library); - pstmt.setString(4, overtime_fee); - result = pstmt.executeUpdate(); - if(result == 1) { - code = 0; - msg = "success"; - } - } catch (ClassNotFoundException e) { - msg = "classnotfound"; - } catch (SQLException e) { - msg = "SQL错误"; - } finally { + + // 获取其他规则参数 + String borrow_num = req.getParameter("borrow_num"); // 借阅数量 + String limit_day = req.getParameter("limit_day"); // 限制天数 + String overtime_fee = req.getParameter("overtime_fee"); // 逾期费用 + + // 如果借阅图书馆不为空,保存借阅规则 + if (msg.isEmpty()) { try { - Base.closeResource(connection, pstmt, null); + connection = Base.getConnection(); + sql = "insert into rules(borrow_num, limit_day, borrow_library, overtime_fee) values(?,?,?,?)"; + pstmt = connection.prepareStatement(sql); + pstmt.setString(1, borrow_num); + pstmt.setString(2, limit_day); + pstmt.setString(3, borrow_library); + pstmt.setString(4, overtime_fee); + result = pstmt.executeUpdate(); + + // 判断插入是否成功 + if (result == 1) { + code = 0; + msg = "success"; + } + } catch (ClassNotFoundException e) { + msg = "classnotfound"; } catch (SQLException e) { - msg = "关闭失败"; + msg = "SQL错误"; + } finally { + try { + // 关闭数据库资源 + Base.closeResource(connection, pstmt, null); + } catch (SQLException e) { + msg = "关闭失败"; + } } } - + + // 将结果返回给前端 PrintWriter out = resp.getWriter(); out.print(Util.jsonResponse(code, msg, null)); - } - } diff --git a/src/servlet/admin/RuleDel.java b/src/servlet/admin/RuleDel.java index 40244a6..cc0c160 100644 --- a/src/servlet/admin/RuleDel.java +++ b/src/servlet/admin/RuleDel.java @@ -17,43 +17,71 @@ import java.sql.Connection; import javabean.Base; import javabean.Util; - +/** + * Servlet 实现类,用于删除借阅规则 + * + * 该 Servlet 用于处理删除借阅规则的请求,接收规则 ID 并将对应的借阅规则从数据库中删除。 + */ @WebServlet("/admin/ruleDel") public class RuleDel 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"); - // 准备数据 + + // 数据库连接相关变量 Connection connection = null; - PreparedStatement pstmt = null; + PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; String sql = ""; - // 返回数据 - int code = 1; - String msg = "error"; - PrintWriter out = resp.getWriter(); - // 进行查询 + + // 返回数据相关变量 + int code = 1; // 默认为失败 + String msg = "error"; // 默认为错误消息 + + // 获取 PrintWriter 用于返回响应 + PrintWriter out = resp.getWriter(); + try { - connection = (Connection) Base.getConnection(); + // 获取数据库连接 + connection = Base.getConnection(); sql = "delete from rules where id = ?"; + + // 使用 PreparedStatement 来执行删除操作 pstmt = connection.prepareStatement(sql); pstmt.setString(1, id); - result = pstmt.executeUpdate(); - if(result == 1) { + result = pstmt.executeUpdate(); // 执行删除操作 + + // 判断删除操作是否成功 + if (result == 1) { code = 0; - msg = "删除成功"; - }else { - msg = "删除失败"; + msg = "删除成功"; // 删除成功 + } else { + msg = "删除失败"; // 删除失败 } + } catch (ClassNotFoundException e) { - msg = "class没找到"; + msg = "class没找到"; // 发生 ClassNotFoundException 异常时返回错误消息 } catch (SQLException e) { - msg = "sql错误"; + msg = "sql错误"; // 发生 SQL 异常时返回错误消息 } + + // 将结果作为 JSON 响应返回 out.print(Util.jsonResponse(code, msg, null)); } - } diff --git a/src/servlet/admin/RuleEdit.java b/src/servlet/admin/RuleEdit.java index 79eb17d..f55d6fd 100644 --- a/src/servlet/admin/RuleEdit.java +++ b/src/servlet/admin/RuleEdit.java @@ -20,64 +20,90 @@ import javabean.Common; import javabean.Util; /** - * Servlet implementation class RuleEdit + * Servlet 实现类,用于编辑借阅规则 + * + * 该 Servlet 用于处理编辑借阅规则的请求,接收规则的 ID 和修改后的信息, + * 然后根据 ID 更新对应的借阅规则数据。 */ @WebServlet("/admin/ruleEdit") public class RuleEdit extends HttpServlet { + + /** + * 处理 POST 请求,编辑借阅规则 + * + * 该方法接收前端传来的规则 ID 和修改后的数据,根据 ID 更新数据库中的借阅规则。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应内容类型为 JSON resp.setContentType("application/json; charset=utf8"); - // 准备数据 + + // 数据库操作相关变量 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; String sql = ""; String borrow_library = ""; - - // 准备返回数据 - int code = 1; + + // 返回数据相关变量 + int code = 1; // 默认为失败 String msg = ""; - - // 获取数据 - // 获取限定图书馆1、2、3 + + // 获取前端传来的修改数据 + // 获取允许借阅的图书馆信息 int num = 0; try { + // 获取图书馆的映射关系 Map libraryMap = Common.getLibraryMap(); - for(String key : libraryMap.keySet()) { - if(req.getParameter("borrow_library[" +key +"]") != null) { - if(num == 0) { + // 处理前端传来的图书馆选择数据 + for (String key : libraryMap.keySet()) { + if (req.getParameter("borrow_library[" + key + "]") != null) { + if (num == 0) { borrow_library += key; num++; - }else { - borrow_library += "、"+key; + } else { + borrow_library += "、" + key; } } } - if(borrow_library.isEmpty()) { + // 如果没有选择图书馆,则返回错误 + if (borrow_library.isEmpty()) { msg = "允许图书馆不能为空"; } } catch (SQLException e) { msg = "获取图书馆失败"; } + + // 获取其他修改字段 String borrow_num = req.getParameter("borrow_num"); String limit_day = req.getParameter("limit_day"); String overtime_fee = req.getParameter("overtime_fee"); String id = req.getParameter("id"); + + // 执行数据库更新操作 try { - connection = (Connection) Base.getConnection(); - sql = "update rules set borrow_num=?, limit_day=?, borrow_library=?,overtime_fee=? where id=?"; + connection = Base.getConnection(); + sql = "update rules set borrow_num=?, limit_day=?, borrow_library=?, overtime_fee=? where id=?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, borrow_num); pstmt.setString(2, limit_day); pstmt.setString(3, borrow_library); pstmt.setString(4, overtime_fee); pstmt.setString(5, id); - result = pstmt.executeUpdate(); - if(result == 1) { + result = pstmt.executeUpdate(); // 执行更新操作 + + // 更新成功 + if (result == 1) { code = 0; msg = "success"; } + } catch (ClassNotFoundException e) { msg = "classnotfound"; } catch (SQLException e) { @@ -89,12 +115,9 @@ public class RuleEdit extends HttpServlet { msg = "关闭失败"; } } - + + // 返回响应 PrintWriter out = resp.getWriter(); out.print(Util.jsonResponse(code, msg, null)); - - - } - } diff --git a/src/servlet/admin/RuleList.java b/src/servlet/admin/RuleList.java index 35dc2db..19d51b4 100644 --- a/src/servlet/admin/RuleList.java +++ b/src/servlet/admin/RuleList.java @@ -19,55 +19,83 @@ import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; - +/** + * Servlet 用于查询所有借阅规则并返回 JSON 数据 + * + * 该 Servlet 查询数据库中的借阅规则,返回包含借阅规则信息的 JSON 格式数据。 + */ @WebServlet("/admin/ruleList") public class RuleList extends HttpServlet { + + /** + * 处理 GET 请求,查询所有借阅规则 + * + * 该方法执行一个 SQL 查询,获取所有借阅规则,并将其封装成 JSON 数据格式返回。 + * + * @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"); - // 准备查询 + + // 数据库操作相关变量 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; String sql = ""; - // 准备返回参数 - int code = 1; + + // 返回数据相关变量 + int code = 1; // 默认为失败 String msg = "error"; JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); + try { - connection = (Connection) Base.getConnection(); + // 获取数据库连接 + connection = Base.getConnection(); + // 执行查询语句 sql = "select * from rules"; pstmt = connection.prepareStatement(sql); resultSet = pstmt.executeQuery(); - while(resultSet.next()) { + + // 处理查询结果 + while (resultSet.next()) { jsonObject.put("id", resultSet.getString("id")); jsonObject.put("limit_day", resultSet.getString("limit_day")); jsonObject.put("borrow_num", resultSet.getString("borrow_num")); jsonObject.put("borrow_library", resultSet.getString("borrow_library")); jsonObject.put("overtime_fee", resultSet.getString("overtime_fee")); + // 将每条规则数据加入到 JSON 数组中 jsonArray.add(jsonObject); } - code = 0; - if(!jsonArray.isEmpty()) { + + // 根据查询结果设置响应代码和消息 + code = 0; // 查询成功 + if (!jsonArray.isEmpty()) { msg = "查询成功"; - }else { + } else { msg = "没有数据"; } + } catch (SQLException e) { msg = "sql错误"; } catch (ClassNotFoundException e) { msg = "class没找到"; } finally { try { + // 关闭数据库连接及资源 Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } - + + // 将查询结果封装成 JSON 格式的响应并返回 PrintWriter out = resp.getWriter(); - out.print( Util.jsonResponse(code, msg, jsonArray.toString()) ); + out.print(Util.jsonResponse(code, msg, jsonArray.toString())); } - } diff --git a/src/servlet/admin/SortAdd.java b/src/servlet/admin/SortAdd.java index 027f724..97b9972 100644 --- a/src/servlet/admin/SortAdd.java +++ b/src/servlet/admin/SortAdd.java @@ -17,67 +17,93 @@ import javabean.Base; import javabean.Common; import javabean.Util; +/** + * Servlet 用于添加新的书籍分类 + * + * 该 Servlet 接受来自客户端的书籍分类名称和描述,检查分类名称是否重复, + * 如果没有重复则将新的分类信息插入到数据库中,并返回 JSON 格式的响应。 + */ @WebServlet("/admin/sortAdd") -public class SortAdd extends HttpServlet{ +public class SortAdd extends HttpServlet { + + /** + * 处理 POST 请求,添加新的书籍分类 + * + * 该方法首先检查传入的分类名称是否已存在,如果不存在,则插入新的分类记录到数据库中。 + * + * @param req 请求对象,包含用户提交的数据 + * @param resp 响应对象,返回操作结果 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应类型为 JSON 格式 resp.setContentType("application/json; charset=utf8"); - // 接受数据 + + // 获取前端传来的分类名称和描述 String name = req.getParameter("name"); String description = req.getParameter("description"); - - // 准备数据 + + // 数据库操作相关变量 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; String sql = ""; - int count = 0 ; - // 准备返回数据 - int code = 1; - String msg = ""; - + int count = 0; + + // 返回结果相关变量 + int code = 1; // 默认为失败 + String msg = ""; // 默认消息 + try { - connection = (Connection) Base.getConnection(); - // 查询重复name + // 获取数据库连接 + connection = Base.getConnection(); + + // 查询是否已有相同的分类名称 sql = "select count(*) as count from book_sort where name=?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, name); resultSet = pstmt.executeQuery(); - if(resultSet.next()) { - // 有重复 - if(resultSet.getInt("count") > 0) { + + if (resultSet.next()) { + // 如果分类名称已存在 + if (resultSet.getInt("count") > 0) { msg = "分类名不能重复"; - }else { - // 进行插入 + } else { + // 分类名称不存在,执行插入操作 sql = "insert into book_sort(name, description) values(?,?)"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, description); result = pstmt.executeUpdate(); - if(result == 1) { + if (result == 1) { + // 插入成功 code = 0; msg = "添加成功"; - }else { + } else { + // 插入失败 msg = "添加失败"; } } } - + } catch (ClassNotFoundException e) { - msg = "classnotfound"; + msg = "class not found"; } catch (SQLException e) { - msg = "SQL错误"; + msg = "SQL 错误"; } finally { try { + // 关闭数据库资源 Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } - + + // 输出返回结果 PrintWriter out = resp.getWriter(); out.print(Util.jsonResponse(code, msg, null)); - } } diff --git a/src/servlet/admin/SortDel.java b/src/servlet/admin/SortDel.java index 832a9ee..e4e418c 100644 --- a/src/servlet/admin/SortDel.java +++ b/src/servlet/admin/SortDel.java @@ -16,67 +16,91 @@ import javax.servlet.http.HttpServletResponse; import javabean.Base; import javabean.Util; - +/** + * Servlet 用于删除书籍分类 + * + * 该 Servlet 接收要删除的书籍分类的 ID。如果该分类为默认分类(ID 为 1),则不允许删除; + * 如果不是默认分类,则先将该分类下的书籍的分类 ID 修改为默认分类,然后删除分类记录。 + */ @WebServlet("/admin/sortDel") public class SortDel extends HttpServlet { + + /** + * 处理 GET 请求,删除指定的书籍分类 + * + * 该方法首先检查传入的分类是否是默认分类(ID 为 1),如果是默认分类,则不允许删除。 + * 如果不是默认分类,首先将该分类下的书籍的分类 ID 修改为默认分类的 ID,然后删除分类记录。 + * + * @param req 请求对象,包含用户提交的数据 + * @param resp 响应对象,返回操作结果 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - // !!!!! 默认1为未分类 + // 默认分类 ID 为 1(不允许删除) String defaultId = "1"; resp.setContentType("application/json; charset=utf8"); - // 接受数据 + + // 获取请求参数(分类 ID) String id = req.getParameter("id"); - - // 准备数据 + + // 数据库操作相关变量 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; String sql = ""; - - // 准备返回数据 - int code = 1; - String msg = ""; + + // 返回结果相关变量 + int code = 1; // 默认为失败 + String msg = ""; // 默认为错误消息 + try { - // 不能删除未分类 - if(defaultId.equals(id)) { + // 判断是否为默认分类,若是则不允许删除 + if (defaultId.equals(id)) { msg = "不能删除未分类"; - }else { - connection = (Connection) Base.getConnection(); - // 分类下的文章修改 + } else { + // 获取数据库连接 + connection = Base.getConnection(); + + // 更新该分类下的书籍,将它们的分类 ID 修改为默认分类 sql = "update books set sort_id=? where sort_id=?"; pstmt = connection.prepareStatement(sql); - pstmt.setString(1, defaultId); - pstmt.setString(2, id); - result = pstmt.executeUpdate(); - - // 进行删除 + pstmt.setString(1, defaultId); // 设置新的分类 ID(默认分类) + pstmt.setString(2, id); // 设置要删除的分类 ID + result = pstmt.executeUpdate(); // 执行更新操作 + + // 删除分类记录 sql = "delete from book_sort where id=?"; pstmt = connection.prepareStatement(sql); - pstmt.setString(1, id); - result = pstmt.executeUpdate(); - if(result == 1) { + pstmt.setString(1, id); // 设置要删除的分类 ID + result = pstmt.executeUpdate(); // 执行删除操作 + + // 根据删除结果设置返回消息 + if (result == 1) { code = 0; msg = "删除成功"; - }else { + } else { msg = "删除失败"; } } - + } catch (ClassNotFoundException e) { - msg = "classnotfound"; + msg = "class not found"; // 类未找到错误 } catch (SQLException e) { - msg = "SQL错误"; + msg = "SQL 错误"; // SQL 错误 } finally { + // 关闭数据库资源 try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { - msg = "关闭失败"; + msg = "关闭失败"; // 资源关闭失败 } } - + + // 输出 JSON 格式的响应结果 PrintWriter out = resp.getWriter(); out.print(Util.jsonResponse(code, msg, null)); } - } diff --git a/src/servlet/admin/SortEdit.java b/src/servlet/admin/SortEdit.java index 36d90b1..9903f40 100644 --- a/src/servlet/admin/SortEdit.java +++ b/src/servlet/admin/SortEdit.java @@ -16,71 +16,96 @@ import javax.servlet.http.HttpServletResponse; import javabean.Base; import javabean.Util; - +/** + * Servlet 用于编辑书籍分类信息 + * + * 该 Servlet 接收要修改的分类信息,检查分类名是否重复, + * 如果没有重复则更新该分类的名称和描述。 + */ @WebServlet("/admin/sortEdit") public class SortEdit extends HttpServlet { + + /** + * 处理 POST 请求,编辑指定的书籍分类信息 + * + * 该方法首先检查分类名是否已存在,如果已存在且不是当前分类,则返回错误提示; + * 如果分类名合法,则更新分类信息(名称和描述)。 + * + * @param req 请求对象,包含用户提交的数据 + * @param resp 响应对象,返回操作结果 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应类型为 JSON resp.setContentType("application/json; charset=utf8"); - // 接受数据 + + // 获取请求中的数据(分类 ID,分类名和描述) String id = req.getParameter("id"); String name = req.getParameter("name"); String description = req.getParameter("description"); - - // 准备数据 + + // 数据库操作相关变量 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; String sql = ""; - int count = 0 ; - // 准备返回数据 - int code = 1; - String msg = ""; - + + // 返回结果相关变量 + int code = 1; // 默认为失败 + String msg = ""; // 默认为错误消息 + try { - connection = (Connection) Base.getConnection(); - // 查询重复name + // 获取数据库连接 + connection = Base.getConnection(); + + // 查询分类名是否已经存在(排除当前分类 ID) sql = "select count(*) as count from book_sort where name=? and id != ?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, id); resultSet = pstmt.executeQuery(); - if(resultSet.next()) { - // 有重复 - if(resultSet.getInt("count") > 0) { - msg = "分类名不能重复"; - }else { - // 进行插入 + + // 判断分类名是否重复 + if (resultSet.next()) { + if (resultSet.getInt("count") > 0) { + msg = "分类名不能重复"; // 分类名已经存在 + } else { + // 更新分类名称和描述 sql = "update book_sort set name=?, description=? where id=?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, description); pstmt.setString(3, id); result = pstmt.executeUpdate(); - if(result == 1) { + + // 根据更新结果返回消息 + if (result == 1) { code = 0; - msg = "修改成功"; - }else { - msg = "修改失败"; + msg = "修改成功"; // 修改成功 + } else { + msg = "修改失败"; // 修改失败 } } } - + } catch (ClassNotFoundException e) { - msg = "classnotfound"; + msg = "classnotfound"; // 类未找到错误 } catch (SQLException e) { - msg = "SQL错误"; + msg = "SQL 错误"; // SQL 错误 } finally { + // 关闭数据库资源 try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { - msg = "关闭失败"; + msg = "关闭失败"; // 资源关闭失败 } } - + + // 输出 JSON 格式的响应结果 PrintWriter out = resp.getWriter(); - out.print(Util.jsonResponse(code, msg, null)); - + out.print(Util.jsonResponse(code, msg, null)); // 返回 JSON 响应 } } diff --git a/src/servlet/admin/SortList.java b/src/servlet/admin/SortList.java index f731e3e..418df56 100644 --- a/src/servlet/admin/SortList.java +++ b/src/servlet/admin/SortList.java @@ -18,57 +18,90 @@ import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; +/** + * Servlet 用于查询并返回所有书籍分类的列表。 + * + * 该 Servlet 从数据库中查询所有书籍分类信息,并将其以 JSON 格式返回。 + */ @WebServlet("/admin/sortList") public class SortList extends HttpServlet { + + /** + * 处理 GET 请求,查询所有书籍分类信息并返回 JSON 格式的响应 + * + * 该方法从数据库查询所有书籍分类,并将其封装为 JSON 格式的响应返回给客户端。 + * 如果查询成功,返回分类数据;如果查询失败或没有数据,返回相应的错误消息。 + * + * @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"); - // 接受参数 - // 准备参数 + // 准备数据库操作相关变量 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; - JSONObject jsonObject = new JSONObject(); - JSONArray jsonArray = new JSONArray(); - // 返回参数 - int code = 1; - String msg = "error"; - int count = 0; + JSONObject jsonObject = new JSONObject(); // 单个分类信息的 JSON 对象 + JSONArray jsonArray = new JSONArray(); // 分类列表的 JSON 数组 + + // 初始化返回状态和消息 + int code = 1; // 默认为失败 + String msg = "error"; // 默认为错误消息 + String sql = ""; - PrintWriter out = resp.getWriter(); - // 开始查询 + PrintWriter out = resp.getWriter(); // 获取响应输出流 + + // 开始查询数据库 try { + // 获取数据库连接 connection = Base.getConnection(); + + // 执行 SQL 查询,查询所有书籍分类信息 sql = "select * from book_sort"; pstmt = connection.prepareStatement(sql); resultSet = pstmt.executeQuery(); + + // 遍历查询结果并将其添加到 JSON 数组中 while (resultSet.next()) { + // 获取每个分类的字段值并将其添加到 JSON 对象中 jsonObject.put("id", resultSet.getString("id")); jsonObject.put("name", resultSet.getString("name")); jsonObject.put("description", resultSet.getString("description")); + + // 将 JSON 对象添加到 JSON 数组中 jsonArray.add(jsonObject.toString()); } + + // 根据查询结果返回相应的消息 if (!jsonArray.isEmpty()) { - code = 0; + code = 0; // 表示成功 msg = "查询成功"; } else { - msg = "数据为空"; + msg = "数据为空"; // 如果没有数据,返回提示信息 } + } catch (ClassNotFoundException e) { + // 捕获 ClassNotFoundException 错误 msg = "没找到"; e.printStackTrace(); } catch (SQLException e) { - msg = "sql错误"; + // 捕获 SQL 错误 + msg = "sql 错误"; } finally { + // 关闭数据库资源 try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } - out.print(Util.jsonResponse(code, msg, jsonArray.toString())); + // 输出 JSON 格式的响应结果 + out.print(Util.jsonResponse(code, msg, jsonArray.toString())); } - } diff --git a/src/servlet/admin/UpdatePassword.java b/src/servlet/admin/UpdatePassword.java index ebd205c..d3855a6 100644 --- a/src/servlet/admin/UpdatePassword.java +++ b/src/servlet/admin/UpdatePassword.java @@ -18,81 +18,109 @@ import javabean.Base; import javabean.Util; import net.sf.json.JSONObject; - +/** + * Servlet 用于管理员密码修改操作。 + * + * 该 Servlet 处理管理员请求修改密码的操作,要求提供原密码、新密码以及确认密码。 + * 在成功验证原密码后,更新密码并返回相应的操作结果。 + */ @WebServlet("/admin/updatePassword") public class UpdatePassword extends HttpServlet { + + /** + * 处理 POST 请求,执行密码更新操作 + * + * 该方法接受原密码、新密码和确认密码,验证原密码是否正确,若正确则更新密码。 + * + * @param req 请求对象 + * @param resp 响应对象 + * @throws ServletException 如果发生 Servlet 错误 + * @throws IOException 如果发生 I/O 错误 + */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置响应类型为 JSON resp.setContentType("application/json; charset=utf8"); - // 接受数据 + + // 接收请求参数 String oldPassword = req.getParameter("oldPassword"); String newPassword = req.getParameter("newPassword"); String conPassword = req.getParameter("conPassword"); + + // 获取当前登录用户的用户名 HttpSession session = req.getSession(); String username = (String) session.getAttribute("admin"); - - - // 准备资源 + + // 准备数据库操作的变量 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; String sql = null; int count = 0; - // 返回数据 - int code = 1; - String msg = "error"; + + // 准备返回数据 + int code = 1; // 默认为失败 + String msg = "error"; // 默认为错误信息 JSONObject json = new JSONObject(); PrintWriter out = resp.getWriter(); - - // 可靠性 - if(conPassword.equals(newPassword)) { - // 查询 + + // 检查新密码和确认密码是否一致 + if (conPassword.equals(newPassword)) { + // 查询数据库验证原密码 try { connection = Base.getConnection(); - // 验证账号密码 + // 验证原密码是否正确 sql = "select count(*) as count from admin where username=? and password=?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, username); - pstmt.setString(2, Util.passMd5(oldPassword)); + pstmt.setString(2, Util.passMd5(oldPassword)); // 对原密码进行 MD5 加密 resultSet = pstmt.executeQuery(); - while(resultSet.next()) { + + // 获取查询结果 + while (resultSet.next()) { count = resultSet.getInt("count"); } - // 修改密码 - // 密码正确 - if(count >= 1) { + + // 如果原密码正确,进行密码更新 + if (count >= 1) { + // 更新密码 sql = "update admin set password=? where username=?"; pstmt = connection.prepareStatement(sql); - pstmt.setString(1, Util.passMd5(newPassword)); + pstmt.setString(1, Util.passMd5(newPassword)); // 对新密码进行 MD5 加密 pstmt.setString(2, username); result = pstmt.executeUpdate(); - if(result == 1) { - code = 0; + + // 判断密码是否更新成功 + if (result == 1) { + code = 0; // 成功 msg = "修改成功"; - }else { + } else { msg = "修改失败"; } - - }else { - msg = "密码错误"; + + } else { + msg = "密码错误"; // 原密码不正确 } + } catch (ClassNotFoundException e) { - msg = "class notfound"; + msg = "class notfound"; // 类未找到错误 } catch (SQLException e) { - msg = "sql错误"; + msg = "sql错误"; // SQL 错误 } finally { + // 关闭数据库资源 try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { - msg = "关闭失败"; + msg = "关闭失败"; // 关闭数据库资源失败 } } - - }else { - msg = "两次密码不一致"; + + } else { + msg = "两次密码不一致"; // 新密码和确认密码不一致 } + + // 输出 JSON 格式的响应结果 out.print(Util.jsonResponse(code, msg, null)); } - } From c41c268df3bc0ae343557459fc1722a71304dbb3 Mon Sep 17 00:00:00 2001 From: Wangder03 Date: Fri, 13 Dec 2024 20:22:10 +0800 Subject: [PATCH 4/5] wangd --- WebContent/admin/bookborrowlist.jsp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WebContent/admin/bookborrowlist.jsp b/WebContent/admin/bookborrowlist.jsp index 36a979e..b04ad29 100644 --- a/WebContent/admin/bookborrowlist.jsp +++ b/WebContent/admin/bookborrowlist.jsp @@ -4,7 +4,7 @@ <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ page language="java" contentType="text/html; charset=UTF-8" - pageEncoding="UTF-8"%>//1//1 + pageEncoding="UTF-8"%> From de77c2f94c7084be518630298a5e0cbca0990977 Mon Sep 17 00:00:00 2001 From: zw-j2003 <2845872007@qq.com> Date: Sat, 14 Dec 2024 00:26:22 +0800 Subject: [PATCH 5/5] sovled problem --- .idea/.gitignore | 8 ++++++++ .idea/vcs.xml | 4 ++++ 2 files changed, 12 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..d843f34 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file