基于javaweb+mysql的jsp+servlet图书图书馆管理系统(java+jsp+layui+bootstrap+servlet+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的JSP+Servlet图书图书馆管理系统(java+jsp+layui+bootstrap+servlet+mysql)
项目介绍
使用jsp+servlet、layui、mysql完成的图书馆系统,包含用户图书借阅、图书管理员、系统管理员界面,功能齐全。
开发工具为eclipse/IDEA,环境java8,tomcat9.0+,mysql为5.7(mysql8也行)
项目详细介绍
本图书管理系统总体上分为前台页面显示和后台管理。
前台页面(即本书图书管理系统的首页)实现了公告的显示,图书查询,留言建议三大主要功能,有读者规则查看功能,师生们可以看到图书管理人员发布的最新公告信息,并可以查询自己感兴趣的图书,查看留言提议,用户登录后还有个人资料修改、个人借阅信息查询、个人违章信息查询等功能,也可以给学校的图书管理人员留言提议。
后台的页面则集成了图书管理中所需的功能,分成图书管理人员和系统管理人员,平时管理人员的工作都是在后台中完成的。前台是为了师生显示的。相对应的后台是针对学校图书管理人员,后台的页面都加密,如果不正常登录是进入不了后台管理页面的,后台图书管理人员功能包括:借阅图书、归还图书、借书记录显示与查找、还书记录显示与查找、公告增删改查;后台系统管理员功能包括:对书籍的增删改查、对书籍分类的增删改、对借阅证的增删改查、对借阅信息的显示与查找、对借阅规则的增删改、对图书管理员的增删改、对近期借阅书籍数量的折线图显示,以及对这些表格数据进行Excel表格的输出
共包含三个大模块:用户、图书管理员、系统管理员
一、用户模块
二、图书管理员模块
三、系统管理员模块
sql表
共有十张数据库表,书籍表、借书记录表、留言表、系统管理员表、借阅证表、图书管理管理员表书籍分类表、图书馆表、借阅规则表、公告表 - 书籍表 - 编号、姓名、作者、存放的图书馆、分类编号、存放位置、是否借出、书籍描述 - 借书记录表 - 编号、借阅证编号、书籍编号、借书日期、限制日期、归还日期、违规描述、处理人编号 - 留言表 - 编号、借阅证编号、留言内容、留言日期 - 系统管理员表 - 账号、密码 - 借阅证表 - 编号、密码、借阅者姓名、规则编号、状态(丢失、可用) - 图书管理员表 - 编号、姓名、账号、密码、邮箱 - 书籍分类表 - 编号、分类名、分类描述 - 图书馆表 - 编号、图书馆名、描述 - 规则表 - 编号、限制借阅数量、限制借阅天数、图书超期每天费用 - 公告表 - 编号、标题、公告内容、发布日期
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA; 3.tomcat环境:Tomcat 9.x版本,注:tomcat9.0以下会有问题; 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.是否Maven项目: 否;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目 6.数据库:MySql 5.7版本,(mysql8也行)
技术栈
使用说明
resultSet = pstmt.executeQuery(); while (resultSet.next()) { String library = resultSet.getString("library_id"); String sql1 = "select * from library where ID =" + library; PreparedStatement pstmt1 = connection.prepareStatement(sql1); ResultSet rs1 = pstmt1.executeQuery(); String lib = ""; while (rs1.next()) { lib = rs1.getString("name"); } String sortid = resultSet.getString("sort_id"); String sql2 = "select * from book_sort where ID =" + sortid; PreparedStatement pstmt2 = connection.prepareStatement(sql2); ResultSet rs2 = pstmt2.executeQuery(); String sort = ""; while (rs2.next()) { sort = rs2.getString("name"); } jsonData.put("id", resultSet.getString("id")); jsonData.put("name", resultSet.getString("name")); jsonData.put("author", resultSet.getString("author")); jsonData.put("library_id", lib); jsonData.put("sort_id", sort); jsonData.put("position", resultSet.getString("position")); jsonData.put("status", resultSet.getString("status")); jsonData.put("description", resultSet.getString("description")); jsonArray.add(jsonData); } countSql = "select count(*) as count from books "; countSql += where; countPstmt = connection.prepareStatement(countSql); countSet = countPstmt.executeQuery(); if (countSet.next()) { count = countSet.getInt("count"); } if (!jsonArray.isEmpty()) { code = 0; msg = "查询成功"; } } catch (ClassNotFoundException e) { msg = "class没找到"; } catch (SQLException e) { msg = "sql错误";
pstmt1 = connection1.prepareStatement(findIdSql); pstmt1.setString(1, password); pstmt1.setString(2, reader); pstmt1.setString(3, rule_id); pstmt1.setString(4, status); dataSet = pstmt1.executeQuery(); if(dataSet.next()) { jsonData.put("id", dataSet.getString("id")); } } catch (ClassNotFoundException e) { msg = "发生异常"; } catch (SQLException e) { msg = "sql错误"; System.out.println("sql失败"); } 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 { code = "1"; msg = "执行失败"; } } json.put("code", code); json.put("msg", msg); json.put("data", jsonData.toString()); PrintWriter out = resp.getWriter(); out.print(json.toString()); } } package servlet.admin;
* Servlet implementation class ReturnTable */ @WebServlet("/manager1/returnTable") public class ReturnTable extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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 = ""; // 无限制条件 if (page == null) { page = "1"; } if (limit == null) { limit = "10"; } // 准备查询 Connection connection = null; PreparedStatement pstmt = null; PreparedStatement countPstmt = null; ResultSet resultSet = null; ResultSet countSet = null; String sql = ""; String countSql = ""; // 准备返回参数 int code = 1; String msg = "无数据"; int count = 0; JSONObject jsonData = new JSONObject(); JSONArray jsonArray = new JSONArray(); JSONObject jsonResult = new JSONObject(); // 进行查询 try { connection = (Connection) Base.getConnection(); sql = "select * from borrow_books where manager_id is null"; if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) {
@WebServlet("/admin/ruleAdd") public class RuleAdd extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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 num = 0; try { MaplibraryMap = Common.getLibraryMap(); for(String key : libraryMap.keySet()) { if(req.getParameter("borrow_library[" +key +"]") != null) { if(num == 0) { borrow_library += key; num++; }else { borrow_library += "、"+key; } } }
String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式 Pattern p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); Matcher m_script = p_script.matcher(htmlStr); htmlStr=m_script.replaceAll(""); // 过滤script标签 Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); Matcher m_style=p_style.matcher(htmlStr); htmlStr=m_style.replaceAll(""); // 过滤style标签 Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); Matcher m_html=p_html.matcher(htmlStr); htmlStr=m_html.replaceAll(""); // 过滤html标签 return htmlStr.trim(); // 返回文本字符串 } /** * 一般使用ServletRequest对象获取表单提交的数据, * (主要通过 getParameter() 和 getParameterValues() * 方法获取),再此创建内部类Request,重写getParameter() * 和 getParameterValues(),并在重写的两个方法中实现过滤 */ class Request extends HttpServletRequestWrapper{// HttpServletRequest //Wrapper是servletRequest的实现类 public Request(HttpServletRequest request) { super(request); } @Override public String getParameter(String name) { // 返回过滤后的参数值 return filter(super.getRequest().getParameter(name)); } @Override public String[] getParameterValues(String name) { // 获取所有参数值 String[] values = super.getRequest().getParameterValues(name); // 通过循环对所有参数进行进行过滤 for(int i=0;ivalues[i] = filter(values[i]); } return values; } }
countSql += where; countPstmt = connection.prepareStatement(countSql); countSet = countPstmt.executeQuery(); if (countSet.next()) { count = countSet.getInt("count"); } if (!jsonArray.isEmpty()) { 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 = "关闭资源失败"; } } // 返回数据 jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); jsonResult.put("data", jsonArray.toArray()); PrintWriter out = resp.getWriter(); out.print(jsonResult.toString()); } } package servlet.manager;
String position = req.getParameter("position"); String status = req.getParameter("status"); String description = req.getParameter("description"); System.out.println(description+"-------------"); JSONObject json = new JSONObject(); //if(id == null || id.equals("")) Connection connection = null; PreparedStatement pstmt = null; //ResultSet resultSet = null; int result = 0; 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(); pstmt = connection.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, author); pstmt.setString(3, library_id); pstmt.setString(4, sort_id); pstmt.setString(5, position); pstmt.setString(6, status); pstmt.setString(7, description); pstmt.setString(8, id); result = pstmt.executeUpdate(); } catch (SQLException e) { } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { Base.closeResource(connection, pstmt, null); } catch (SQLException e) { e.printStackTrace(); } } if(result==1) { json.put("code", "0"); json.put("msg", "success"); }else { json.put("code", "1"); json.put("msg", "error"); } out.write(json.toString()); //System.out.println(postData); //JSONObject json = JSONObject.fromObject(); }
count = countSet.getInt("count"); } if (!jsonArray.isEmpty()) { 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 = "关闭资源失败"; } } // 返回数据 jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); jsonResult.put("data", jsonArray.toArray()); PrintWriter out = resp.getWriter(); out.print(jsonResult.toString()); } } package servlet.manager;
String conditionValue = (String) req.getParameter("conditionValue"); String where = null; // 无限制条件 if (page == null) { page = "1"; } if (limit == null) { limit = "10"; } Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int code = 1; String msg = "error"; int count = 0; String sql = ""; // String countSql = "" 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 if (condition != null && conditionValue != null && !condition.isEmpty() && !conditionValue.isEmpty()) { where = " where " + condition + " like '%" + conditionValue + "%'"; sql = sql + where; } // 分页 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()) { jsonObject.put("id", resultSet.getString("id")); jsonObject.put("password", resultSet.getString("password")); jsonObject.put("reader", resultSet.getString("reader")); jsonObject.put("rule_id", resultSet.getString("rule_id")); jsonObject.put("status", resultSet.getString("status")); jsonArray.add(jsonObject); } // 获取总数 sql = "select count(*) as count from borrow_card ";
} package servlet.admin; @WebServlet("/admin/cardDel") public class CardDel extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/json; charset=utf8"); // 接收数据 String id = req.getParameter("id"); // 处理数据 Connection connection = null; PreparedStatement delCardPstmt = null; PreparedStatement delHistoryPstmt = null; String delCardSql = null; String delHistorySql = null; int delCardResult = 0; int delHistoryResult = 0; // 返回数据 String code = "1"; String msg = "error"; JSONObject jsonObject = new JSONObject(); JSONObject jsonData = new JSONObject(); // 开始处理 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);
// 获取所有参数值 String[] values = super.getRequest().getParameterValues(name); // 通过循环对所有参数进行进行过滤 for(int i=0;ivalues[i] = filter(values[i]); } return values; } } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { /*if(encoding != null){ request.setCharacterEncoding(encoding); //将request替换为重写后的request request = new Request((HttpServletRequest) request); response.setContentType("text/html; charset = "+encoding); }*/ request = new Request((HttpServletRequest) request); chain.doFilter(request, response); } /** * @see Filter#destroy() */ public void destroy() { } } package servlet.admin;
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } package servlet.admin; @WebServlet("/admin/cardEdit") public class CardEdit extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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"); // 准备资源 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; String sql = null;
String msg = "error"; JSONObject jsonObject = new JSONObject(); JSONObject jsonData = new JSONObject(); // 开始处理 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); } catch (SQLException e) { msg = "关闭失败"; } } } PrintWriter out = resp.getWriter(); if(delCardResult == 1) { code = "0"; msg = "删除借阅证成功"; } jsonObject.put("code", code); jsonObject.put("msg", msg); jsonObject.put("data", jsonData); out.print(jsonObject.toString()); }
limit = "10"; } // 准备查询 Connection connection = null; PreparedStatement pstmt = null; PreparedStatement countPstmt = null; ResultSet resultSet = null; ResultSet countSet = null; String sql = ""; String countSql = ""; // 准备返回参数 int code = 1; String msg = "无数据"; int count = 0; JSONObject jsonData = new JSONObject(); JSONArray jsonArray = new JSONArray(); JSONObject jsonResult = new JSONObject(); // 进行查询 try { connection = (Connection) Base.getConnection(); sql = "select * from borrow_books where manager_id is not null"; if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) { where = " and " + condition + " like '%" + conditionValue + "%' "; sql += where; } sql += " limit ?,?";// 1 10 (1-1)*10 System.out.println("???" + sql); pstmt = connection.prepareStatement(sql); pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit)); pstmt.setInt(2, Integer.parseInt(limit)); resultSet = pstmt.executeQuery(); while (resultSet.next()) { jsonData.put("id", resultSet.getString("id")); jsonData.put("card_id", resultSet.getString("card_id")); jsonData.put("book_id", resultSet.getString("book_id")); jsonData.put("borrow_date", resultSet.getString("borrow_date")); jsonData.put("end_date", resultSet.getString("end_date")); jsonData.put("return_date", resultSet.getString("return_date")); jsonData.put("illegal", resultSet.getString("illegal")); jsonData.put("manager_id", resultSet.getString("manager_id")); jsonArray.add(jsonData); } countSql = "select count(*) as count from borrow_books where manager_id is not null";
jsonData.put("book_id", resultSet.getString("book_id")); jsonData.put("borrow_date", resultSet.getString("borrow_date")); jsonData.put("end_date", resultSet.getString("end_date")); jsonArray.add(jsonData); } countSql = "select count(*) as count from borrow_books where manager_id is null"; countSql += where; countPstmt = connection.prepareStatement(countSql); countSet = countPstmt.executeQuery(); if (countSet.next()) { count = countSet.getInt("count"); } if (!jsonArray.isEmpty()) { 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 = "关闭资源失败"; } } // 返回数据 jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); jsonResult.put("data", jsonArray.toArray()); PrintWriter out = resp.getWriter(); out.print(jsonResult.toString()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response)
msg = "class没找到"; } catch (SQLException e) { msg = "sql错误"; } finally { try { Base.closeResource(null, pstmt, resultSet); Base.closeResource(connection, countPstmt, countSet); } catch (SQLException e) { msg = "关闭资源失败"; } } // 返回数据 jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); jsonResult.put("data", jsonArray.toArray()); PrintWriter out = resp.getWriter(); out.print(jsonResult.toString()); } } package servlet.reader; /** * Servlet implementation class Illegal */ @WebServlet("/reader/illegal") public class Illegal extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/json; charset=utf8");
result = pstmt.executeUpdate(); if(result == 1) { code = 0; msg = "success"; } } catch (ClassNotFoundException e) { msg = "classnotfound"; } catch (SQLException e) { msg = "SQL错误"; } finally { try { Base.closeResource(connection, pstmt, null); } catch (SQLException e) { msg = "关闭失败"; } } PrintWriter out = resp.getWriter(); out.print(Util.jsonResponse(code, msg, null)); } } package servlet.admin; @WebServlet("/admin/cardDel") public class CardDel extends HttpServlet {
} 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 { try { Base.closeResource(connection, pstmt, null); } catch (SQLException e) { msg = "关闭失败"; } } PrintWriter out = resp.getWriter(); out.print(Util.jsonResponse(code, msg, null)); } } package servlet.admin;
JSONObject json = new JSONObject(); PrintWriter out = resp.getWriter(); // 判断数据 if(id == null || password == null || reader == null || reader == null || status == null || id.equals("") || password.equals("") || reader.equals("") || rule_id.equals("") || status.equals("")) { code = "1"; msg = "参数不能为空"; }else { sql = "update borrow_card set password=?, reader=?, rule_id=?, status=? where id=?"; try { connection = (Connection) Base.getConnection(); 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 = "错误"; } catch (SQLException e) { msg = "sql错误"; } finally { try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } if(result == 1) { code = "0"; msg = "修改成功"; } json.put("code", code); json.put("msg", msg); out.print(json.toString()); } } } package servlet.admin;
} } // 返回数据 jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); jsonResult.put("data", jsonArray.toArray()); PrintWriter out = resp.getWriter(); out.print(jsonResult.toString()); } } package servlet.reader; /** * Servlet implementation class Illegal */ @WebServlet("/reader/illegal") public class Illegal extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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 = ""; // 无限制条件 if (page == null) { page = "1"; }
// 准备返回参数 int code = 1; String msg = "无数据"; int count = 0; HttpSession session = req.getSession(); JSONObject jsonData = new JSONObject(); JSONArray jsonArray = new JSONArray(); JSONObject jsonResult = new JSONObject(); // 进行查询 try { connection = (Connection) Base.getConnection(); sql = "select * from borrow_books where card_id = " + session.getAttribute("reader"); if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) { where = " and " + condition + " like '%" + conditionValue + "%' "; sql += where; } sql += " limit ?,?";// 1 10 (1-1)*10 System.out.println("???" + sql); pstmt = connection.prepareStatement(sql); pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit)); pstmt.setInt(2, Integer.parseInt(limit)); resultSet = pstmt.executeQuery(); while (resultSet.next()) { jsonData.put("id", resultSet.getString("id")); jsonData.put("card_id", resultSet.getString("card_id")); jsonData.put("book_id", resultSet.getString("book_id")); jsonData.put("borrow_date", resultSet.getString("borrow_date")); jsonData.put("end_date", resultSet.getString("end_date")); jsonData.put("return_date", resultSet.getString("return_date")); jsonArray.add(jsonData); } countSql = "select count(*) as count from borrow_books where card_id = " + req.getSession().getAttribute("reader"); countSql += where; countPstmt = connection.prepareStatement(countSql); countSet = countPstmt.executeQuery(); if (countSet.next()) { count = countSet.getInt("count");
} catch (SQLException e) { msg = "sql错误"; } finally { try { Base.closeResource(null, pstmt, resultSet); Base.closeResource(connection, countPstmt, countSet); } catch (SQLException e) { msg = "关闭资源失败"; } } // 返回数据 jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); jsonResult.put("data", jsonArray.toArray()); PrintWriter out = resp.getWriter(); out.print(jsonResult.toString()); } } package servlet.manager; @WebServlet("/manager1/borrowTable")
@WebServlet("/admin/borrowList") public class BorrowList extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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 = ""; // 无限制条件 if(page == null) { page = "1"; } if(limit == null) { limit = "10"; } // 准备查询 Connection connection = null; PreparedStatement pstmt = null; PreparedStatement countPstmt = null; ResultSet resultSet = null; ResultSet countSet = null; String sql = ""; String countSql = ""; // 准备返回参数 int code = 1; String msg = "error"; int count = 0;
jsonObject.put("id", resultSet.getString("id")); jsonObject.put("password", resultSet.getString("password")); jsonObject.put("reader", resultSet.getString("reader")); jsonObject.put("rule_id", resultSet.getString("rule_id")); jsonObject.put("status", resultSet.getString("status")); jsonArray.add(jsonObject); } // 获取总数 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 = "成功"; } } catch (ClassNotFoundException e) { msg = "没找到"; e.printStackTrace(); } catch (SQLException e) { msg = "sql错误"; } finally { try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); jsonResult.put("data", jsonArray.toString()); PrintWriter out = resp.getWriter();
@WebServlet("/admin/cardAdd") public class CardAdd extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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 code = "1"; String msg = "error"; String data = ""; JSONObject json = new JSONObject(); JSONObject jsonData = new JSONObject(); Connection connection = null; Connection connection1 = null; PreparedStatement pstmt = null; PreparedStatement pstmt1 = null; String sql = null; int result = 0; ResultSet dataSet = null; // 参数不能为空 if(reader == null || password == null || rule_id == null || rule_id == null || status == null) { code = "1"; msg = "值不能为空"; }else { try { connection = (Connection) Base.getConnection(); 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();
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 = "关闭资源失败"; } } // 返回数据 jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); jsonResult.put("data", jsonArray.toArray()); PrintWriter out = resp.getWriter(); out.print(jsonResult.toString()); } } package servlet.admin;
} } 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 = "关闭资源失败"; } } // 返回数据 jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); jsonResult.put("data", jsonArray.toArray()); PrintWriter out = resp.getWriter(); out.print(jsonResult.toString()); } } package servlet.admin; @WebServlet("/admin/borrowList") public class BorrowList extends HttpServlet { @Override