113 KiB
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<String, Object> hashMap = new HashMap<String, Object>();
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<String, Object> 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<String, String>(); // 传输数据过滤 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<String, String> 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<String, String> 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"%>
<html lang="en"> <head> </head> <% 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 ); %>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") : "" %> |
<% 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"> 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();
%>
<!-- 搜索框 -->
<script type="text/html" id="search">
<div class="demoTable">
条件搜索:
<div class="layui-inline">
<select id="condition" name="condition" lay-verify="required">
<option value=""></option>
<option value="id">ID</option>
<option value="name">书名</option>
<option value="author">作者</option>
<option value="library_id">图书馆</option>
<option value="position">位置</option>
<option value="status">状态</option>
<option value="description">描述</option>
</select>
</div>
<div class="layui-inline">
<input class="layui-input" name="conditionValue" id="conditionValue" autocomplete="off" placeholder="请输入搜索内容">
</div>
<button class="layui-btn" data-type="reload" lay-event="search">搜索</button>
<button type="button" class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon">添加书籍</i></button>
</div>
</script>
<!-- 表单 -->
<table id="demo" lay-filter="form">
</table>
<!-- 行操作 -->
<script type="text/html" id="operateBar">
<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="bookBorrowList">查看借阅</a>
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
// 图书馆json
var libraryJson = <%=libraryJson %>
// 图书分类json
var bookSortJson = <%=bookSortJson %>
</script>
<!-- 状态模板 -->
<script type="text/html" id="statusTpl">
{{# if(d.status == 1){ }}
<span style="color:#5FB878;">可借</span>
{{# } else { }}
借出
{{# } }}
</script>
<script>
layui.use(['table', 'jquery'],function(){
$ = layui.jquery;
var table = layui.table;
var tableIns = table.render({
elem: '#demo'
,height: 600
,url: './bookList'
,title: '数据表单'
,toolbar: '#search'
,page: true
,cols: [[
{type: 'numbers', width:50, fixed:'left'}
,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
,{field: 'name', title: '书名', width:170, sort: true}
,{field: 'author', title: '作者', width: 140, sort: true}
,{field: 'library_id', title: '图书馆', width:80, edit: true //,templet: '#libraryTemp'}
,templet: function(d){
return libraryJson[d.library_id];
}}
,{field: 'sort_id', title: '分类', width: 80, sort: true, edit:true
,templet: function(d){
return bookSortJson[d.sort_id];
}}
,{field: 'position', title: '位置', width: 110, sort: true}
,{field: 'status', title: '状态', width: 60, templet:'#statusTpl'}
,{field: 'description', title: '描述', width: 340}
,{fixed: 'right', title:'操作', width: 200, align:'center', toolbar: '#operateBar'} //这里的toolbar值是模板元素的选择器
]]
});
// 直接编辑
table.on('edit(form)', function(obj){ //注:edit是固定事件名,test是table原始容器的属性 lay-filter="对应的值"
console.log(obj.value); //得到修改后的值
console.log(obj.field); //当前编辑的字段名
console.log(obj.data); //所在行的所有相关数据
});
// 监听侧边工具条 编辑,删除
table.on('tool(form)', function(obj){ //注:tool 是工具条事件名,test 是 table 原始容器的属性 lay-filter="对应的值"
var data = obj.data; //获得当前行数据
var layEvent = obj.event; //获得 lay-event 对应的值(也可以是表头的 event 参数对应的值)
var tr = obj.tr; //获得当前行 tr 的 DOM 对象(如果有的话)
var id = data.id;
var name = data.name;
if(layEvent === 'del'){ //删除
layer.confirm('真的删除行么', function(index){
layer.close(index);
//向服务端发送删除指令
$.ajax({
url: './bookDel',
method: 'get',
dataType: 'JSON',
data: "id=" +id,
success: function(data){
if(data.code == 0){
layer.msg(data.msg);
$('.layui-laypage-btn').click();
}else{
layer.msg(data.msg);
}
}
})
});
} else if(layEvent === 'edit'){ //编辑
// 页面编辑
layer.open({
type: 2,
title: "更改信息",
area: ['800px', '600px'],
maxmin: true, //开启最大化最小化按钮
shadeClose: true,
content: "bookedit.jsp?id="+ id,
end: function(){
$(".layui-laypage-btn").click();
}
});
}else if(layEvent === 'bookBorrowList') { //查看该书籍借阅历史
layer.open({
title: '书籍借阅历史',
type: 2,
area: ['800px', '600px'],
maxmin: true,
shadeClose: true,
content: "bookborrowlist.jsp?id=" +id +"&name="+name
})
}
});
// 顶部工具栏事件
table.on('toolbar(form)', function(obj){
var checkStatus = table.checkStatus(obj.config.id);
var data = obj.data;
switch(obj.event){
// 条件查找
case 'search':
var conditionValue = $('#conditionValue');
var condition = $('#condition');
//这里以搜索为例
tableIns.reload({
where: { //设定异步数据接口的额外参数,任意设
"condition": condition.val(),
"conditionValue": conditionValue.val()
}
,page: {
curr: 1 //重新从第 1 页开始
}
});
break;
// 添加书籍
case 'add':
var addBookLayer = layer.open({
type: 2,
title: "添加书籍",
area: ['800px', '600px'],
maxmin: true, //开启最大化最小化按钮
shadeClose: true,
content: "bookadd.jsp",
end: function () {
console.log("finish add");
$(".layui-laypage-btn").click();
}
});
layer.full(addBookLayer);
break;
};
});
});
</script>
</html>
//list1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html lang="en">
<head>
</head>
</table>
<!-- 头部工具栏 -->
<script type="text/html" id="headBar">
条件搜索:
<div class="layui-inline">
<select id="condition" name="condition" lay-verify="required">
<option value=""></option>
<option value="other">超期未还</option>
<option value="id">ID</option>
<option value="card_id">借阅证号</option>
<option value="book_id">书籍ID</option>
<option value="borrow_date">借阅日期</option>
<option value="end_date">限制日期</option>
<option value="return_date">返还日期</option>
<option value="illegal">违章信息</option>
<option value="manager_id">处理人</option>
</select>
</div>
<div class="layui-inline">
<input class="layui-input" id="conditionValue" name="conditionValue" id="demoReload" autocomplete="off">
</div>
<button class="layui-btn" name="condition" data-type="reload" lay-event="search">搜索</button>
</script>
<script>
layui.use(['table','jquery'], function(){
$ = layui.jquery;
var table = layui.table;
// 进行渲染
var tableIns = table.render({
elem: '#history'
,url:'./borrowList'
,toolbar: '#headBar'
,cols: [[
{field:'id', width:80, title: 'ID', sort: true}
,{field:'card_id', width:180, title: '借阅证号'}
,{field:'book_id', width:100, title: '书籍ID', sort: true}
,{field:'borrow_date', width:180, title: '借阅时间'}
,{field:'end_date', title: '限定时间', width: 180}
,{field:'return_date', width:180, title: '归还时间', sort: true}
,{field:'illegal', minWidth:280, title: '违章信息', sort: true}
,{field:'manager_id', width:80, title: '处理人'}
]]
,page: true
});
// 头部工具栏事件
table.on('toolbar(formFilter)', function(obj){
var checkStatus = table.checkStatus(obj.config.id);
switch(obj.event){
// 条件查找图书证
case 'search':
var conditionValue = $('#conditionValue');
var condition = $('#condition');
// 进行搜索,重新渲染
tableIns.reload({
where: { //设定异步数据接口的额外参数,任意设
"condition": condition.val(),
"conditionValue": conditionValue.val()
}
,page: {
curr: 1 //重新从第 1 页开始
}
});
break;
};
});
});
</script>
</html>
//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"%>
<html>
<head>
</head>
<%
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();
%>
%>
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"):"" %> |
// 获取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();
%>
<% Base.closeResource(connection, pstmt, ruleSet); %>
</html> //list <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html lang="en"> <head> </head><!-- 表格侧边栏的操作 -->
<script type="text/html" id="operateBar">
<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="cardBorrow">查看借阅</a>
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 状态模板 -->
<script type="text/html" id="statusTpl">
{{# if(d.status == 0){ }}
<span style="color:red">挂失<span>
{{# } else { }}
可用
{{# } }}
</script>
<script>
layui.use(['table','jquery'], function(){
$ = layui.jquery;
var table = layui.table;
// 进行渲染
var tableIns = table.render({
elem: '#cardTable'
,url:'./cardList'
,toolbar: '#headBar'
,height: 600
,cols: [[
{field:'id', width:180, title: 'ID', sort: true}
,{field:'reader', width:180, title: '用户名', sort: true}
,{field:'rule_id', width:180, title: '借阅规则', sort: true}
,{field:'status', width:180, title: '状态', templet: '#statusTpl'}
,{fixed: 'right', title:'操作', toolbar: '#operateBar', align: 'center', width:250}
]]
,page: true
});
// 头部工具栏事件
table.on('toolbar(formFilter)', function(obj){
var checkStatus = table.checkStatus(obj.config.id);
switch(obj.event){
// 条件查找图书证
case 'search':
var conditionValue = $('#conditionValue');
var condition = $('#condition');
// 进行搜索,重新渲染
tableIns.reload({
where: { //设定异步数据接口的额外参数,任意设
"condition": condition.val(),
"conditionValue": conditionValue.val()
}
,page: {
curr: 1 //重新从第 1 页开始
}
});
break;
// 添加借书证
case 'add':
var addCardLayer = layer.open({
type: 2,
title: '添加借书证',
area: ['800px', '500px'],
maxmin: true,
shadeClose: true,
content: 'cardadd.jsp',
end: function(){
$('.layui-laypage-btn').click();
}
});
//layer.full(addCardLayer);
};
});
// 侧边工具栏事件
table.on(('tool(formFilter)'), function(obj){
var data = obj.data;
var layEvent = obj.event;
var tr = obj.tr;
var id = data.id;
switch(obj.event){
case 'edit':
layer.open({
type: 2,
title: '更改信息',
area: ['800px', '600px'],
maxmin: true,
shadeClose: true,
content: 'cardedit.jsp?id=' +id,
end: function(){
$(".layui-laypage-btn").click();
}
})
break;
case 'del':
layer.confirm('确认删除么?<br><span style="color:red;">这将删除该借阅证的所有记录</span>',function(index){
layer.close(index);
$.ajax({
url: './cardDel',
type: 'get',
data: 'id=' +id,
dataType: 'json',
timeout: 3000,
success: function(data){
if(data.code == 0){
console.log(data);
layer.msg(data.msg,{
icon: 6,
time: 1500
})
// 还是本页数据
$(".layui-laypage-btn").click();
}else{
layer.open({
title: '失败',
content: data.msg
})
}
},
error: function(){
layer.msg("连接超时");
}
})
})
break;
case 'cardBorrow':
layer.open({
type: 2,
title: '借阅历史',
area: ['800px', '600px'],
maxmin: true,
shadeClose: true,
content: 'cardborrow.jsp?id=' +id,
end: function(){
//$(".layui-laypage-btn").click();
}
})
}
})
});
</script>
</html>
//index
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html lang="en">
<head>
</head>
</html>
//librarydata
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
</head>
</head>
</head>
%>
lay-verify="required" required autocomplete="off" placeholder="请输入姓名" class="layui-input layui-hide"><!-- 表格侧边栏 -->
<script type="text/html" id="operateBar">
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
layui.use(['table','jquery'], function(){
$ = layui.jquery;
var table = layui.table;
// 进行渲染
var tableIns = table.render({
elem: '#managerTable'
,url:'./managerList'
,toolbar: '#headBar'
,height: 600
,cols: [[
{field:'id', width:80, title: 'ID', sort: true}
,{field:'account', width:80, title: '账号', sort: true}
,{field:'name', width:80, title: '姓名'}
,{field:'email', title: '邮箱', minWidth: 150}
,{fixed: 'right', title:'操作', toolbar: '#operateBar', align: 'center', width:150}
]]
});
// 头部工具栏事件
table.on('toolbar(formFilter)', function(obj){
var checkStatus = table.checkStatus(obj.config.id);
switch(obj.event){
// 添加管理员
case 'add':
var addCardLayer = layer.open({
type: 2,
title: '添加管理员',
area: ['800px', '500px'],
maxmin: true,
shadeClose: true,
content: 'manageradd.jsp',
});
//layer.full(addCardLayer);
};
});
// 侧边工具栏事件
table.on(('tool(formFilter)'), function(obj){
var data = obj.data;
var layEvent = obj.event;
var id = data.id;
var tr = obj.tr;
switch(obj.event){
case 'edit':
layer.open({
type: 2,
title: '更改信息',
area: ['800px', '600px'],
maxmin: true,
shadeClose: true,
content: 'manageredit.jsp?id=' +id,
})
break;
case 'del':
layer.confirm('确定要删除么?',function(){
layer.msg("ok");
$.ajax({
url: './managerDel',
data: 'id=' +id,
type: 'get',
dataType: 'json',
timeout: 3000,
success: function(data){
layer.msg("???");
if(data.code == 0){
layer.msg(data.msg,{
icon: 6,
anim: 5,
time: 500
});
setTimeout(function(){
parent.location.reload();
},500);
}else{
layer.msg(data.code);
}
},
error: function(){
layer.msg("连接超时");
}
})
})
}
})
});
</script>
</html>