package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javabean.Admin; import net.sf.json.JSONObject; /** * 管理员登录 * * @author Mingyue * */ @WebServlet("/adminLogin") public class AdminLogin extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置头文件 response.setContentType("application/json; charset=utf8"); PrintWriter out = response.getWriter(); // 获取账号密码 String username = request.getParameter("username"); String password = request.getParameter("password"); // 设置响应map HashMap hashMap = new HashMap(); Admin admin = new Admin(); String result = null; try { result = admin.login(username, password); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } if (result != null && result.equals("1")) { HttpSession session = request.getSession(); session.setAttribute("admin", username); hashMap.put("code", 0); hashMap.put("msg", "登录成功"); hashMap.put("url", request.getContextPath() +"/admin/index.jsp"); }else { hashMap.put("code", 1); hashMap.put("msg", result); } JSONObject json = JSONObject.fromObject(hashMap); out.write(json.toString()); } } //bookadd package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import net.sf.json.JSONObject; /** * Servlet implementation class BookAdd */ @WebServlet("/admin/bookAdd") public class BookAdd extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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 JSONObject json = new JSONObject(); Connection connection = null; PreparedStatement pstmt = null; //ResultSet resultSet = null; int result = 0; String sql = "insert into books(name, author, library_id, sort_id, position, status, description) values(?,?,?,?,?,?,?)"; System.out.println(sql); 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); result = pstmt.executeUpdate(); } catch (SQLException e) { } catch (ClassNotFoundException e) { 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()); } } //bookdel package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javabean.JDBCBean; import net.sf.json.JSONObject; @WebServlet("/admin/bookDel") public class BookDel extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/json; charset=utf8"); String id = req.getParameter("id"); JSONObject json = new JSONObject(); JDBCBean db = new JDBCBean(); String sql = "delete from books where id = " +id; int result = 0; int code = 1; String msg = ""; if( id != null && !id.equals("") ) { result = db.executeUpdate(sql); } if( result == 1 ) { code = 0; msg = "删除成功"; }else { code = 1; msg = "删除失败"; } json.put("code", code); json.put("msg", msg); db.close(); PrintWriter out = resp.getWriter(); out.print( json.toString() ); } } //bookedit package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import net.sf.json.JSONObject; @WebServlet("/admin/bookEdit") public class BookEdit 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 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+"-------------"); 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(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } } //booklist package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javabean.Admin; import javabean.Common; import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * Servlet implementation class BookList */ @WebServlet("/admin/bookList") public class BookList extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/json; charset=utf8"); JSONObject json = new JSONObject(); String result = null; Map map = null; int code = 1; String msg = ""; String data = ""; 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(); // 传输数据过滤 if(page == null) { page = "1"; } if(limit == null) { limit = "10"; } if(condition == null || conditionValue == null || condition.isEmpty() || conditionValue.isEmpty()) { condition = null; conditionValue = null; }else { where.put("condition", condition); where.put("conditionValue", conditionValue); } Admin admin = new Admin(); try { map = admin.getBookList(page, limit, where); result = (String) map.get("data"); } catch (ClassNotFoundException | SQLException e) { 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); } PrintWriter out = resp.getWriter(); out.print(json.toString()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } //borrowlist package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @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 jsonData = new JSONObject(); JSONArray jsonArray = new JSONArray(); JSONObject jsonResult = new JSONObject(); // 进行查询 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)); 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 "; countSql +=where; countPstmt = connection.prepareStatement(countSql); countSet = countPstmt.executeQuery(); if(countSet.next()) { count = countSet.getInt("count"); } if(!jsonArray.isEmpty()) { code = 0; msg = "查询成功"; }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 = "关闭资源失败"; } } // 返回数据 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()); } } //cardadd package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import javabean.JDBCBean; import net.sf.json.JSONObject; /** * Servlet implementation class CardAdd */ @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(); 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); 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()); } } //carddel package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @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); 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()); } } //cardedit package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import net.sf.json.JSONObject; @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 code = "1"; String msg = "error"; 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()); } } } //cardlist package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javabean.Base; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * Servlet implementation class CardList */ @WebServlet("/admin/cardList") public class CardList 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 = 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 "; // 有限制 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(); out.print(jsonResult.toString()); // out.print("{\"code\":0,\"msg\":\"\",\"count\":\"234\",\"data\":[{\"id\":\"1\",\"password\":\"23442\",\"reader\":\"minm\",\"rule_id\":\"1\",\"status\":\"2\"}]}"); } } //librarydata package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javabean.Base; import javabean.DateTime; import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @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 { resp.setContentType("application/json; charset:utf8"); // 准备参数 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; String sql = ""; 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(); // 开始查询 try { 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); pstmt = connection.prepareStatement(sql); pstmt.setString(1,date); resultSet = pstmt.executeQuery(); while(resultSet.next()) { jsonData.add(resultSet.getString("count")); jsonDays.add(md); } } jsonObject.put("data", jsonData); jsonObject.put("days", jsonDays); if(!jsonObject.isEmpty()) { code = 0; msg = "查询成功"; }else { msg = "数据为空"; } } catch (ClassNotFoundException e) { msg = "没找到"; e.printStackTrace(); } catch (SQLException e) { msg = "sql错误"; }finally { try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } out.print( Util.jsonResponse(code, msg, jsonObject.toString()) ); } } //loginout package servlet.admin; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class LoginOut */ @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"); } } //manageradd package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @WebServlet("/admin/managerAdd") public class ManagerAdd extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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"); // 准备参数 String sql = ""; 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("")) { msg = "参数不能为空"; out.print(Util.jsonResponse(code, msg, null)); }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) { sql = "insert into manager(name, account, password, email) values(?,?,?,?)"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, account); pstmt.setString(3, password); pstmt.setString(4, email); result = pstmt.executeUpdate(); } // 返回数据 if(result == 1 && count == 0) { code = 0; msg = "添加成功"; }else if(count > 0){ msg = "账号重复"; }else { msg = "添加失败"; } } catch (ClassNotFoundException e) { msg = "class not found"; } catch (SQLException e) { msg = "sql错误"; } finally { try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { e.printStackTrace(); } } out.print(Util.jsonResponse(code, msg, null)); } } } //managerdel package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import javabean.Util; @WebServlet("/admin/managerDel") public class ManagerDel extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/json; charset=utf8"); String id = req.getParameter("id"); // 准备参数 String sql = ""; Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; // 返回参数 int code = 1; String msg = ""; PrintWriter out = resp.getWriter(); try { connection = (Connection) Base.getConnection(); sql = "delete from manager where id=?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, id); result = pstmt.executeUpdate(); if(result == 1) { code = 0; msg = "删除成功"; }else { msg = "删除失败"; } } catch (ClassNotFoundException e) { msg = "class not found"; } catch (SQLException e) { msg = "sql错误"; } out.print(Util.jsonResponse(code, msg, null)); } } //manageredit package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @WebServlet("/admin/managerEdit") public class ManagerEdit 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 name = req.getParameter("name"); String password = req.getParameter("password"); String email = req.getParameter("email"); // 准备参数 String sql = ""; 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("")) { msg = "参数不能为空"; out.print(Util.jsonResponse(code, msg, null)); }else { try { connection = (Connection) Base.getConnection(); // 添加管理员 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); result = pstmt.executeUpdate(); // 返回数据 if(result == 1 ){ code = 0; msg = "修改成功"; }else { msg = "修改失败"; } } catch (ClassNotFoundException e) { msg = "class not found"; } catch (SQLException e) { msg = "sql错误"; } finally { try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { e.printStackTrace(); } } out.print(Util.jsonResponse(code, msg, null)); } } } //managerlist package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @WebServlet("/admin/managerList") public class ManagerList extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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(); PrintWriter out = resp.getWriter(); try { connection = (Connection) Base.getConnection(); sql = "select * from manager"; pstmt = connection.prepareStatement(sql); resultSet = pstmt.executeQuery(); 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); } if (!jsonArray.isEmpty()) { code = 0; msg = "查询成功"; } else { msg = "数据为空"; } } catch (ClassNotFoundException e) { msg = "class找不到"; } catch (SQLException e) { msg = "sql错误"; } out.print(Util.jsonResponse(code, msg, jsonArray.toString())); } } //ruleadd package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import javabean.Common; import javabean.Util; @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 { Map libraryMap = 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; } } } 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 { try { Base.closeResource(connection, pstmt, null); } catch (SQLException e) { msg = "关闭失败"; } } PrintWriter out = resp.getWriter(); out.print(Util.jsonResponse(code, msg, null)); } } //ruledel package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import javabean.Util; @WebServlet("/admin/ruleDel") public class RuleDel 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 pstmt = null; ResultSet resultSet = null; int result = 0; String sql = ""; // 返回数据 int code = 1; String msg = "error"; PrintWriter out = resp.getWriter(); // 进行查询 try { connection = (Connection) Base.getConnection(); sql = "delete from rules where id = ?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, id); result = pstmt.executeUpdate(); if(result == 1) { code = 0; msg = "删除成功"; }else { msg = "删除失败"; } } catch (ClassNotFoundException e) { msg = "class没找到"; } catch (SQLException e) { msg = "sql错误"; } out.print(Util.jsonResponse(code, msg, null)); } } //ruleedit package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import javabean.Common; import javabean.Util; /** * Servlet implementation class RuleEdit */ @WebServlet("/admin/ruleEdit") public class RuleEdit 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 { Map libraryMap = 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; } } } 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=?"; 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) { 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)); } } //rulelist package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import javabean.Base; import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @WebServlet("/admin/ruleList") public class RuleList extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/json; charset=utf8"); // 准备查询 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; String sql = ""; // 准备返回参数 int code = 1; String msg = "error"; JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); try { connection = (Connection) Base.getConnection(); sql = "select * from rules"; pstmt = connection.prepareStatement(sql); resultSet = pstmt.executeQuery(); 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")); jsonArray.add(jsonObject); } code = 0; if(!jsonArray.isEmpty()) { msg = "查询成功"; }else { msg = "没有数据"; } } catch (SQLException e) { msg = "sql错误"; } catch (ClassNotFoundException e) { msg = "class没找到"; } finally { try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } PrintWriter out = resp.getWriter(); out.print( Util.jsonResponse(code, msg, jsonArray.toString()) ); } } //sortadd package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javabean.Base; import javabean.Common; import javabean.Util; @WebServlet("/admin/sortAdd") public class SortAdd extends HttpServlet{ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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 = ""; try { connection = (Connection) Base.getConnection(); // 查询重复name 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) { msg = "分类名不能重复"; }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) { code = 0; msg = "添加成功"; }else { msg = "添加失败"; } } } } catch (ClassNotFoundException e) { msg = "classnotfound"; } catch (SQLException e) { msg = "SQL错误"; } finally { try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } PrintWriter out = resp.getWriter(); out.print(Util.jsonResponse(code, msg, null)); } } //sortdel package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javabean.Base; import javabean.Util; @WebServlet("/admin/sortDel") public class SortDel extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // !!!!! 默认1为未分类 String defaultId = "1"; resp.setContentType("application/json; charset=utf8"); // 接受数据 String id = req.getParameter("id"); // 准备数据 Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; int result = 0; String sql = ""; // 准备返回数据 int code = 1; String msg = ""; try { // 不能删除未分类 if(defaultId.equals(id)) { msg = "不能删除未分类"; }else { connection = (Connection) Base.getConnection(); // 分类下的文章修改 sql = "update books set sort_id=? where sort_id=?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, defaultId); pstmt.setString(2, 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) { code = 0; msg = "删除成功"; }else { msg = "删除失败"; } } } catch (ClassNotFoundException e) { msg = "classnotfound"; } catch (SQLException e) { msg = "SQL错误"; } finally { try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } PrintWriter out = resp.getWriter(); out.print(Util.jsonResponse(code, msg, null)); } } //sortedit package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javabean.Base; import javabean.Util; @WebServlet("/admin/sortEdit") public class SortEdit 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 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 = ""; try { connection = (Connection) Base.getConnection(); // 查询重复name 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 { // 进行插入 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) { code = 0; msg = "修改成功"; }else { msg = "修改失败"; } } } } catch (ClassNotFoundException e) { msg = "classnotfound"; } catch (SQLException e) { msg = "SQL错误"; } finally { try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } PrintWriter out = resp.getWriter(); out.print(Util.jsonResponse(code, msg, null)); } } //sortlist package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javabean.Base; import javabean.Util; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @WebServlet("/admin/sortList") public class SortList extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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; String sql = ""; PrintWriter out = resp.getWriter(); // 开始查询 try { connection = Base.getConnection(); sql = "select * from book_sort"; pstmt = connection.prepareStatement(sql); resultSet = pstmt.executeQuery(); while (resultSet.next()) { jsonObject.put("id", resultSet.getString("id")); jsonObject.put("name", resultSet.getString("name")); jsonObject.put("description", resultSet.getString("description")); jsonArray.add(jsonObject.toString()); } if (!jsonArray.isEmpty()) { code = 0; msg = "查询成功"; } else { msg = "数据为空"; } } catch (ClassNotFoundException e) { msg = "没找到"; e.printStackTrace(); } catch (SQLException e) { msg = "sql错误"; } finally { try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } out.print(Util.jsonResponse(code, msg, jsonArray.toString())); } } //updatepassword package servlet.admin; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javabean.Base; import javabean.Util; import net.sf.json.JSONObject; @WebServlet("/admin/updatePassword") public class UpdatePassword extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 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"; JSONObject json = new JSONObject(); PrintWriter out = resp.getWriter(); // 可靠性 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)); resultSet = pstmt.executeQuery(); while(resultSet.next()) { count = resultSet.getInt("count"); } // 修改密码 // 密码正确 if(count >= 1) { sql = "update admin set password=? where username=?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, Util.passMd5(newPassword)); pstmt.setString(2, username); result = pstmt.executeUpdate(); if(result == 1) { code = 0; msg = "修改成功"; }else { msg = "修改失败"; } }else { msg = "密码错误"; } } catch (ClassNotFoundException e) { msg = "class notfound"; } catch (SQLException e) { msg = "sql错误"; } finally { try { Base.closeResource(connection, pstmt, resultSet); } catch (SQLException e) { msg = "关闭失败"; } } }else { msg = "两次密码不一致"; } out.print(Util.jsonResponse(code, msg, null)); } } //JS部分 //bookadd <%@page import="java.sql.ResultSet"%> <%@page import="javabean.JDBCBean"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> bookadd <% ResultSet librarySet = null; ResultSet bookSortSet = null; // 获取图书馆列表 JDBCBean db2 = new JDBCBean(); String librarySql = "select * from library"; librarySet = db2.executeQuery( librarySql ); // 获取书籍分类 JDBCBean db3 = new JDBCBean(); String bookSortSql = "select * from book_sort"; bookSortSet = db3.executeQuery( bookSortSql ); %>
可借
不可借
//borrowlist <%@page import="javabean.Util"%> <%@page import="javabean.Base"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 书籍借阅历史 <% String id = request.getParameter("id"); String name = request.getParameter("name"); Connection connection = null; PreparedStatement pstmt = null; ResultSet resultSet = null; String sql = "select * from borrow_books,borrow_card where book_id=? and card_id=borrow_card.id order by borrow_books.id desc"; connection = (Connection)Base.getConnection(); pstmt = connection.prepareStatement(sql); pstmt.setString(1,id); resultSet = pstmt.executeQuery(); /* while(resultSet.next()){ out.print(resultSet.getString("id")); }*/ %>
<%while(resultSet.next()){ %> <%} %>
ID 书籍ID 书籍名 借阅证 借阅者 借阅时间 归还时间 违规信息 处理人
<%=resultSet.getString("id") %> <%=id %> <%=name %> <%=resultSet.getString("borrow_card.id") %> <%=resultSet.getString("reader") %> <%=Util.getFormatDateTime(resultSet.getString("borrow_date")) %> <%=resultSet.getString("return_date") != null? Util.getFormatDateTime(resultSet.getString("return_date")) : "" %> <%=resultSet.getString("illegal") != null? resultSet.getString("illegal") : "" %> <%=resultSet.getString("manager_id") != null? resultSet.getString("manager_id") : "" %>
//edit <%@page import="javabean.JDBCBean"%> <%@page import="java.sql.ResultSet"%> <%@page import="javabean.Admin"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> bookedit <% JDBCBean db = new JDBCBean(); int id = Integer.parseInt(request.getParameter("id")); ResultSet resultSet = null; ResultSet librarySet = null; ResultSet bookSortSet = null; String sql = "select * from books where id=" +id ; resultSet = db.executeQuery(sql); resultSet.next(); String name = resultSet.getString("name"); String author = resultSet.getString("author"); int library_id = resultSet.getInt("library_id"); int sort_id = resultSet.getInt("sort_id"); String position = resultSet.getString("position"); int status = resultSet.getInt("status"); String description = resultSet.getString("description"); db.close(); // 获取图书馆列表 JDBCBean db2 = new JDBCBean(); String librarySql = "select * from library"; librarySet = db2.executeQuery( librarySql ); // 获取书籍分类 JDBCBean db3 = new JDBCBean(); String bookSortSql = "select * from book_sort"; bookSortSet = db3.executeQuery( bookSortSql ); %>
<%-- 隐藏id --%> class="layui-hide">
required lay-verify="required" placeholder="请输入书名" autocomplete="off" class="layui-input">
required lay-verify="required" placeholder="请输入作者" autocomplete="off" class="layui-input">
required lay-verify="required" placeholder="请输入位置编号" autocomplete="off" class="layui-input">
> >
<% // 关闭资源 bookSortSet.close(); librarySet.close(); resultSet.close(); db.close(); %> //list <%@page import="net.sf.json.JSONObject"%> <%@page import="java.sql.ResultSet"%> <%@page import="javabean.JDBCBean"%> <%@page import="java.util.Map"%> <%@page import="java.util.HashMap"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> test <% JDBCBean libraryDb = new JDBCBean(); JDBCBean bookSortDb = new JDBCBean(); ResultSet librarySet = null; ResultSet bookSortSet = null; // 准备sql String librarySql = "select * from library"; String bookSortSql = "select * from book_sort"; // 进行查询 librarySet = libraryDb.executeQuery( librarySql ); bookSortSet = bookSortDb.executeQuery( bookSortSql ); // 准备json JSONObject libraryJson = new JSONObject(); JSONObject bookSortJson = new JSONObject(); // 遍历set // 获取图书馆json while( librarySet.next() ){ libraryJson.put(librarySet.getString("id") , librarySet.getString("name")); } // 获取分类json while( bookSortSet.next() ){ bookSortJson.put(bookSortSet.getString("id") , bookSortSet.getString("name")); } librarySet.close(); libraryDb.close(); %>
//list1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> test
//borrowlist <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 借阅记录
//cardadd <%@page import="javabean.Base"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@page import="net.sf.json.JSONObject"%> <%@page import="net.sf.json.JSONArray"%> <%@page import="javabean.Admin"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 图书证修改 <% Connection connection = null; PreparedStatement pstmt = null; ResultSet ruleSet = null; String sql = "select * from rules"; String result = ""; connection = (Connection)Base.getConnection(); pstmt = connection.prepareStatement(sql); ruleSet = pstmt.executeQuery(); %>
//borrow <%@page import="javabean.Util"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@page import="javabean.Base"%> <%@page import="java.sql.ResultSet"%> <%@page import="javabean.JDBCBean"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 借阅证借阅记录 <% String id = request.getParameter("id"); Connection connection = (Connection)Base.getConnection(); String sql = "select * from borrow_books,books where card_id=? and borrow_books.book_id = books.id"; PreparedStatement pstmt = connection.prepareStatement(sql); pstmt.setString(1, id); ResultSet resultSet = null; resultSet = pstmt.executeQuery(); %>
<% System.out.println(Util.getCurrentTimeString()); %> <% while(resultSet.next()){ %> <%-- 图书超期 --%> <%if(Util.getFormatDateTime(resultSet.getString("end_date")).compareTo(Util.getCurrentTimeString()) < 0 && resultSet.getString("return_date") == null){ %> <%} else{ %> <%} %> <%} %>
ID 书籍ID 书籍名 借阅时间 截止时间 归还时间 违规信息 处理人
<%=resultSet.getString("card_id") %> <%=resultSet.getString("book_id") %> <%=resultSet.getString("books.name") %> <%=Util.getFormatDateTime(resultSet.getString("borrow_date")) %> <%=Util.getFormatDateTime(resultSet.getString("end_date"))%> <%=resultSet.getString("return_date")!=null?Util.getFormatDateTime(resultSet.getString("return_date")) : "未归还" %> <%=resultSet.getString("illegal")!=null?resultSet.getString("illegal"):""%> <%=resultSet.getString("manager_id")!=null?resultSet.getString("manager_id"):"" %>
//edit <%@page import="javabean.Base"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@page import="net.sf.json.JSONObject"%> <%@page import="net.sf.json.JSONArray"%> <%@page import="javabean.Admin"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 图书证修改 <% // 获取借阅证信息 String id = request.getParameter("id"); // 获取rule Connection connection = null; PreparedStatement pstmt = null; PreparedStatement infoPstmt = null; ResultSet ruleSet = null; ResultSet infoSet = null; String sql = "select * from rules"; String infoSql = "select * from borrow_card where id=?"; String result = ""; // 公用连接 connection = (Connection)Base.getConnection(); pstmt = connection.prepareStatement(sql); infoPstmt = connection.prepareStatement(infoSql); infoPstmt.setString(1,id); infoSet = infoPstmt.executeQuery(); infoSet.next(); ruleSet = pstmt.executeQuery(); %>
class="layui-input" disabled>
lay-verify="required" autocomplete="off" placeholder="请输入姓名" class="layui-input">
placeholder="请输入密码" autocomplete="off" class="layui-input" lay-verify="required">
> >
<% Base.closeResource(connection, pstmt, ruleSet); %> //list <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 借阅卡
//index <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 系统管理员 //librarydata <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> ECharts
//manageradd <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 管理员添加
//edit <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@page import="javabean.Base"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 管理员修改 <% String id = request.getParameter("id"); Connection connection = (Connection)Base.getConnection(); String sql = "select * from manager where id=?"; PreparedStatement pstmt = connection.prepareCall(sql); pstmt.setString(1,id); ResultSet resultSet = pstmt.executeQuery(); resultSet.next(); %>
lay-verify="required" required autocomplete="off" placeholder="请输入姓名" class="layui-input layui-hide">
lay-verify="required" required autocomplete="off" placeholder="请输入姓名" class="layui-input">
disabled lay-verify="required" placeholder="请输入账号" autocomplete="off" class="layui-input">
lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
//list <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 借阅卡