基于javaweb+mysql的jsp+servlet图书图书馆管理系统(java+jsp+layui+bootstrap+servlet+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的JSP+Servlet图书图书馆管理系统(java+jsp+layui+bootstrap+servlet+mysql)
项目介绍
使用jsp+servlet、layui、mysql完成的图书馆系统,包含用户图书借阅、图书管理员、系统管理员界面,功能齐全。
开发工具为eclipse/IDEA,环境java8,tomcat9.0+,mysql为5.7(mysql8也行)
项目详细介绍
本图书管理系统总体上分为前台页面显示和后台管理。
前台页面(即本书图书管理系统的首页)实现了公告的显示,图书查询,留言建议三大主要功能,有读者规则查看功能,师生们可以看到图书管理人员发布的最新公告信息,并可以查询自己感兴趣的图书,查看留言提议,用户登录后还有个人资料修改、个人借阅信息查询、个人违章信息查询等功能,也可以给学校的图书管理人员留言提议。
后台的页面则集成了图书管理中所需的功能,分成图书管理人员和系统管理人员,平时管理人员的工作都是在后台中完成的。前台是为了师生显示的。相对应的后台是针对学校图书管理人员,后台的页面都加密,如果不正常登录是进入不了后台管理页面的,后台图书管理人员功能包括:借阅图书、归还图书、借书记录显示与查找、还书记录显示与查找、公告增删改查;后台系统管理员功能包括:对书籍的增删改查、对书籍分类的增删改、对借阅证的增删改查、对借阅信息的显示与查找、对借阅规则的增删改、对图书管理员的增删改、对近期借阅书籍数量的折线图显示,以及对这些表格数据进行Excel表格的输出
共包含三个大模块:用户、图书管理员、系统管理员
一、用户模块
- 查看公告:能看到图书管理员发布的公告信息。 2. 图书查看:分页显示,能看到图书总览,能通过图书编号、图书名称、作者、存放位置、图书描述进行模糊查询 3. 个人违章信息(登录后):个人历史违章信息与查询 4. 图书馆读者留言(登录后):对图书馆的建议,或对书籍的评价 5. 查看借阅规则:不同的规则有不同的借阅限定数量、限定时间、超期费用 6. 个人信息(登录后):对个人信息的查看与登录密码的修改 7. 个人借阅信息(登录后):对个人借阅记录的分页查看与查询
二、图书管理员模块
- 借还图书:帮助用户借阅图书,并判断符不符合借阅规则、返还图书时判断有没有超期,超期则输出超期费用再归还 2. 借阅报表:分页显示所有借阅记录、能进行查询和输出结果到excel公告管理:能添加编辑和删除公告 3. 还书报表:分页显示所有未还的书籍,能通过借阅证号、图书编号、借阅日期、截止日期等条件进行模糊查询 4. 个人信息修改:修改个人信息、密码等
三、系统管理员模块
- 书籍管理:分页,新添图书,设置图书各种信息,编辑删除,通过编号、书名、作者、描述等进行搜索,输出结果到excel,并且能查看该书籍被谁借阅过。 2. 分类管理:分页显示,增删改书籍分类,书籍分类方便查阅整理 3. 借阅证管理:分页显示所有借阅证,增删改查用户借阅证,查看该借阅证所有的借阅记录 4. 借阅规则管理:能显示、编辑、删除所有借阅规则,借阅规则将决定该图书证能借阅什么图书馆的图书,能借多少本,能借多久以及超期每天的费用。 5. 借阅信息查询:分页显示,能条件查询超期没还的,根据编号、借阅证号、书籍编号、借阅日期等进行模糊查询,输出结果到excel等,系统管理员只能查询,不能增删改借阅信息,这个操作由图书管理员操作。 6. 图书管理员管理:对图书管理员进行增删改操作,但是不能修改原来的账号 7. 图书借阅统计:通过折线图将图书馆近30天的每天借阅书籍数量直观的显示出来。
sql表
共有十张数据库表,书籍表、借书记录表、留言表、系统管理员表、借阅证表、图书管理管理员表书籍分类表、图书馆表、借阅规则表、公告表 - 书籍表 - 编号、姓名、作者、存放的图书馆、分类编号、存放位置、是否借出、书籍描述 - 借书记录表 - 编号、借阅证编号、书籍编号、借书日期、限制日期、归还日期、违规描述、处理人编号 - 留言表 - 编号、借阅证编号、留言内容、留言日期 - 系统管理员表 - 账号、密码 - 借阅证表 - 编号、密码、借阅者姓名、规则编号、状态(丢失、可用) - 图书管理员表 - 编号、姓名、账号、密码、邮箱 - 书籍分类表 - 编号、分类名、分类描述 - 图书馆表 - 编号、图书馆名、描述 - 规则表 - 编号、限制借阅数量、限制借阅天数、图书超期每天费用 - 公告表 - 编号、标题、公告内容、发布日期
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA; 3.tomcat环境:Tomcat 9.x版本,注:tomcat9.0以下会有问题; 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.是否Maven项目: 否;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目 6.数据库:MySql 5.7版本,(mysql8也行)
技术栈
- 后端:JSP+Servlet 2. 前端:jsp+layui+bootstrap+jQuery
使用说明
- 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件; 2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,配置tomcat,然后运行; 3. 将项目中src/javabean/Base.java和JDBCBean.java配置文件中的数据库配置改为自己的配置; 4. 运行项目,输入localhost:8080/xxx 登录 5. 用户账号密码:1805010219 1234 图书管理员账号密码:root 1234 系统管理员账号密码:admin admin
String countSql = "";
// 准备返回参数
int code = 1;
String msg = "无数据";
int count = 0;
JSONObject jsonData = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject jsonResult = new JSONObject();
// 进行查询
try {
connection = (Connection) Base.getConnection();
sql = "select * from borrow_books where manager_id is not null";
if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) {
where = " and " + condition + " like '%" + conditionValue + "%' ";
sql += where;
}
sql += " limit ?,?";// 1 10 (1-1)*10
System.out.println("???" + sql);
pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit));
pstmt.setInt(2, Integer.parseInt(limit));
resultSet = pstmt.executeQuery();
while (resultSet.next()) {
jsonData.put("id", resultSet.getString("id"));
jsonData.put("card_id", resultSet.getString("card_id"));
jsonData.put("book_id", resultSet.getString("book_id"));
jsonData.put("borrow_date", resultSet.getString("borrow_date"));
jsonData.put("end_date", resultSet.getString("end_date"));
jsonData.put("return_date", resultSet.getString("return_date"));
jsonData.put("illegal", resultSet.getString("illegal"));
jsonData.put("manager_id", resultSet.getString("manager_id"));
jsonArray.add(jsonData);
}
countSql = "select count(*) as count from borrow_books where manager_id is not null";
countSql += where;
countPstmt = connection.prepareStatement(countSql);
countSet = countPstmt.executeQuery();
if (countSet.next()) {
count = countSet.getInt("count");
}
if (!jsonArray.isEmpty()) {
code = 0;
msg = "查询成功";
}
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());
}
}
@WebServlet("/manager1/borrowTable")
public class BorrowTable 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");
}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);
}
}
msg = "关闭失败";
}
}
PrintWriter out = resp.getWriter();
out.print(Util.jsonResponse(code, msg, null));
}
}
@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;
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"));
jsonArray.add(jsonData);
}
countSql = "select count(*) as count from borrow_books where manager_id is null";
countSql += where;
countPstmt = connection.prepareStatement(countSql);
countSet = countPstmt.executeQuery();
if (countSet.next()) {
count = countSet.getInt("count");
}
if (!jsonArray.isEmpty()) {
code = 0;
msg = "查询成功";
}
} catch (ClassNotFoundException e) {
msg = "class没找到";
} catch (SQLException e) {
msg = "sql错误";
} finally {
try {
Base.closeResource(null, pstmt, resultSet);
Base.closeResource(connection, countPstmt, countSet);
} catch (SQLException e) {
msg = "关闭资源失败";
}
}
// 返回数据
jsonResult.put("code", code);
jsonResult.put("count", count);
jsonResult.put("msg", msg);
jsonResult.put("data", jsonArray.toArray());
PrintWriter out = resp.getWriter();
ResultSet countSet = null;
String sql = "";
String countSql = "";
// 准备返回参数
int code = 1;
String msg = "无数据";
int count = 0;
HttpSession session = req.getSession();
JSONObject jsonData = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject jsonResult = new JSONObject();
// 进行查询
try {
connection = (Connection) Base.getConnection();
sql = "select * from borrow_books where ILLEGAL is not null and length(trim(illegal))>0 AND CARD_ID = "
+ session.getAttribute("reader");
if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) {
where = " and " + condition + " like '%" + conditionValue + "%' ";
sql += where;
}
sql += " limit ?,?";// 1 10 (1-1)*10
System.out.println("???" + sql);
pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit));
pstmt.setInt(2, Integer.parseInt(limit));
resultSet = pstmt.executeQuery();
while (resultSet.next()) {
jsonData.put("id", resultSet.getString("id"));
jsonData.put("card_id", resultSet.getString("card_id"));
jsonData.put("book_id", resultSet.getString("book_id"));
jsonData.put("borrow_date", resultSet.getString("borrow_date"));
jsonData.put("end_date", resultSet.getString("end_date"));
jsonData.put("return_date", resultSet.getString("return_date"));
jsonData.put("illegal", resultSet.getString("illegal"));
jsonData.put("manager_id", resultSet.getString("manager_id"));
jsonArray.add(jsonData);
}
countSql = "select count(*) as count from borrow_books where ILLEGAL is not null and length(trim(illegal))>0 AND CARD_ID = "
+ session.getAttribute("reader");
countSql += where;
countPstmt = connection.prepareStatement(countSql);
countSet = countPstmt.executeQuery();
if (countSet.next()) {
count = countSet.getInt("count");
}
if (!jsonArray.isEmpty()) {
code = 0;
msg = "查询成功";
}
} catch (ClassNotFoundException e) {
// 删除借书记录
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());
}
}
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));
@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();
jsonData.put("card_id", resultSet.getString("card_id"));
jsonData.put("book_id", resultSet.getString("book_id"));
jsonData.put("borrow_date", resultSet.getString("borrow_date"));
jsonData.put("end_date", resultSet.getString("end_date"));
jsonData.put("return_date", resultSet.getString("return_date"));
jsonArray.add(jsonData);
}
countSql = "select count(*) as count from borrow_books where card_id = "
+ req.getSession().getAttribute("reader");
countSql += where;
countPstmt = connection.prepareStatement(countSql);
countSet = countPstmt.executeQuery();
if (countSet.next()) {
count = countSet.getInt("count");
}
if (!jsonArray.isEmpty()) {
code = 0;
msg = "查询成功";
}
} catch (ClassNotFoundException e) {
msg = "class没找到";
} catch (SQLException e) {
msg = "sql错误";
} finally {
try {
Base.closeResource(null, pstmt, resultSet);
Base.closeResource(connection, countPstmt, countSet);
} catch (SQLException e) {
msg = "关闭资源失败";
}
}
// 返回数据
jsonResult.put("code", code);
jsonResult.put("count", count);
jsonResult.put("msg", msg);
jsonResult.put("data", jsonArray.toArray());
PrintWriter out = resp.getWriter();
out.print(jsonResult.toString());
}
}
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 = "修改失败";
}
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);
}
}
@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");
}
}
/**
* Servlet implementation class ReturnTable
*/
@WebServlet("/manager1/returnTable")
public class ReturnTable extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json; charset=utf8");
// 接收参数
String limit = req.getParameter("limit");
String page = req.getParameter("page");
String condition = (String) req.getParameter("condition");
String conditionValue = (String) req.getParameter("conditionValue");
String where = ""; // 无限制条件
if (page == null) {
page = "1";
}
if (limit == null) {
limit = "10";
}
// 准备查询
Connection connection = null;
PreparedStatement pstmt = null;
PreparedStatement countPstmt = null;
ResultSet resultSet = null;
ResultSet countSet = null;
String sql = "";
String countSql = "";
// 准备返回参数
/**
* 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(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);
}
}
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);
@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();
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));
}
}
}