Merge remote-tracking branch 'origin/branch_wang' into branch_wang

# Conflicts:
#	src/servlet/admin/AdminLogin.java
pull/11/head
zw-j2003 5 months ago
commit e9bf3d828c

@ -14,25 +14,67 @@ import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author Mingyue
*
*
*
*
* 访
*
* @author Mingyue
*/
public class AdminFilter implements Filter {
/**
*
*
*
*
*
* @param filterConfig
* @throws ServletException
*/
public void init(FilterConfig filterConfig) throws ServletException {
// 初始化操作(当前没有)
}
}
/**
*
*
* Servlet
* JavaScript
*
* @param request Servlet
* @param response Servlet
* @param chain Servlet
* @throws IOException
* @throws ServletException Servlet
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 将ServletRequest转换为HttpServletRequest以便处理HTTP请求
HttpServletRequest req = (HttpServletRequest) request;
// 获取会话对象
HttpSession session = req.getSession();
// 检查会话中是否存在名为"admin"的属性,若不存在则跳转到登录页面
if(session == null || session.getAttribute("admin") == null) {
// 将ServletResponse转换为HttpServletResponse以便处理HTTP响应
HttpServletResponse rep = (HttpServletResponse)response;
PrintWriter out =response.getWriter();
// iframe父页面直接跳转到登录界面
// 获取输出流
PrintWriter out = rep.getWriter();
// 使用JavaScript进行重定向跳转到管理员登录页面
out.print("<script>window.parent.location.href='"+ req.getContextPath() +"/adminLogin.html'</script>");
} else {
// 如果管理员已登录继续执行过滤器链中的下一个过滤器或目标Servlet
chain.doFilter(request, response);
}
chain.doFilter(request, response);
}
/**
*
*
*
*/
public void destroy() {
// 销毁操作(当前没有)
}
}

@ -11,52 +11,62 @@ import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Admin {
/**
*
*
* @param username
* @param password
* @return
*
*
* @param username
* @param password
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
@SuppressWarnings("null")
public String login(String username, String password) throws ClassNotFoundException, SQLException {
// 参数校验
if (username == null || username.trim().equals("")) {
return "账号不能为空";
} else if (password == null || password.trim().equals("")) {
return "密码不能为空";
}
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
// SQL查询语句验证用户和密码
String sql = "select * from admin where username=? and password=? limit 1";
connection = Base.getConnection();
pstmt = (PreparedStatement) connection.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, Util.passMd5(password));
pstmt.setString(2, Util.passMd5(password)); // 假设Util.passMd5()是对密码的加密方法
resultSet = pstmt.executeQuery();
try{
if (resultSet.next()) {
try {
if (resultSet.next()) { // 如果有记录,说明登录成功
return "1";
}
}catch(Exception e) {
} catch (Exception e) {
return "发生未知错误";
}finally {
if(Base.closeResource(connection, pstmt, resultSet) == false) {
} finally {
// 关闭数据库连接资源
if (Base.closeResource(connection, pstmt, resultSet) == false) {
return "关闭失败";
}
}
// 如果没有记录,账号或密码错误
return "账号或密码错误";
}
/**
*
* @param page
* @param limit
* @return String json
*
*
* @param page
* @param limit
* @param where
* @return Map
* @throws ClassNotFoundException
* @throws SQLException
*/
@ -66,27 +76,30 @@ public class Admin {
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
connection = Base.getConnection();
int number = Integer.valueOf(page);
int size = Integer.valueOf(limit);
int number = Integer.valueOf(page); // 页码
int size = Integer.valueOf(limit); // 每页条数
// 构建查询语句
String sql = "select * from books ";
if(where!=null && !where.isEmpty()) {
whereString += " where "+where.get("condition") +" like '%" +where.get("conditionValue") +"%' ";
sql += whereString;
if (where != null && !where.isEmpty()) {
whereString += " where " + where.get("condition") + " like '%" + where.get("conditionValue") + "%' ";
sql += whereString; // 根据条件拼接查询语句
}
sql += "order by id desc limit ?,? ";
sql += "order by id desc limit ?,? "; // 分页查询
pstmt = (PreparedStatement) connection.prepareStatement(sql);
pstmt.setInt(1, (number-1) * size );
pstmt.setInt(2, size);
pstmt.setInt(1, (number - 1) * size); // 设置分页参数
pstmt.setInt(2, size); // 每页显示的记录数
resultSet = pstmt.executeQuery();
JSONObject json = new JSONObject();
String result = "";
int i = 1;
// 获取行数据
while( resultSet.next() ) {
//System.out.println("????-------" +resultSet.getInt("count"));
// 遍历查询结果并构造JSON格式的返回数据
while (resultSet.next()) {
json.put("id", resultSet.getInt("id"));
json.put("name", resultSet.getString("name"));
json.put("author", resultSet.getString("author"));
@ -95,65 +108,74 @@ public class Admin {
json.put("position", resultSet.getString("position"));
json.put("status", resultSet.getInt("status"));
json.put("description", resultSet.getString("description"));
if(i==1) {
// 拼接多个JSON对象
if (i == 1) {
result = json.toString();
}else {
result += "," +json.toString();
} else {
result += "," + json.toString();
}
i++;
}
map.put("data", result);
// 获取总数count重写sql
// 获取总数
int count = 0;
sql = "select count(*) as count from books ";
if(where!=null && !where.isEmpty()) {
sql += whereString;
sql = "select count(*) as count from books ";
if (where != null && !where.isEmpty()) {
sql += whereString; // 根据条件计算总数
}
pstmt = connection.prepareStatement(sql);
resultSet = pstmt.executeQuery();
if(resultSet.next()) {
if (resultSet.next()) {
count = resultSet.getInt("count");
}
map.put("count", count);
Base.closeResource(connection, pstmt, resultSet);
return map;
}
/**
* id(String)
* @param id
* @return
* ID
*
* @param id ID
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public static String getSortName(String id) throws ClassNotFoundException, SQLException {
if(id==null || id.equals(""))
if (id == null || id.equals("")) {
return "参数错误";
}
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
String sql = null;
String result = null;
connection = Base.getConnection();
connection = Base.getConnection();
sql = "select name from book_sort where id=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, id);
resultSet = pstmt.executeQuery();
if(resultSet.next()) {
if (resultSet.next()) {
result = resultSet.getString("name");
}else {
} else {
result = "查询失败";
}
Base.closeResource(connection, pstmt, null);
return result;
}
/**
* id
* @param id
* @return
* ID
*
* @param id ID
* @return ResultSet
* @throws ClassNotFoundException
* @throws SQLException
*/
@ -161,51 +183,59 @@ public class Admin {
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
String sql = "select * from books where id=? ";
String sql = "select * from books where id=?";
connection = Base.getConnection();
pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, id);
resultSet = pstmt.executeQuery();
//Base.closeResource(null, null, null);
connection.close();
if(resultSet.next()) {
return resultSet;
if (resultSet.next()) {
return resultSet; // 返回查询到的结果
}
return null;
return null; // 没有找到对应的书籍
}
/* 没用 */
/**
* 使
*
* @return JSONArray
* @throws ClassNotFoundException
* @throws SQLException
*/
public static JSONArray getRules() throws ClassNotFoundException, SQLException {
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
String sql = "select * from rules";
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
String result = "";
connection = Base.getConnection();
pstmt = connection.prepareStatement(sql);
resultSet = pstmt.executeQuery();
while(resultSet.next()) {
jsonObject.put("id", resultSet.getString("id"));
jsonObject.put("borrow_num", resultSet.getString("borrow_num"));
jsonObject.put("borrow_library", resultSet.getString("borrow_library"));
jsonObject.put("overtime_fee", resultSet.getString("overtime_fee"));
jsonArray.add(jsonObject);
System.out.println(jsonArray.toString());
}
Base.closeResource(connection, pstmt, resultSet);
PreparedStatement pstmt = null;
ResultSet resultSet = null;
String sql = "select * from rules";
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
connection = Base.getConnection();
pstmt = connection.prepareStatement(sql);
resultSet = pstmt.executeQuery();
// 遍历规则表构造JSON对象
while (resultSet.next()) {
jsonObject.put("id", resultSet.getString("id"));
jsonObject.put("borrow_num", resultSet.getString("borrow_num"));
jsonObject.put("borrow_library", resultSet.getString("borrow_library"));
jsonObject.put("overtime_fee", resultSet.getString("overtime_fee"));
jsonArray.add(jsonObject);
System.out.println(jsonArray.toString());
}
Base.closeResource(connection, pstmt, resultSet);
return jsonArray;
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//Common common = new Common();
//System.out.println(common.getCount("books"));
Admin admin = new Admin();
System.out.println(admin.getSortName("2"));
//Map map = admin.getBookList("1", "100");
//System.out.println( map.get("count"));
System.out.println(admin.getSortName("2")); // 示例,获取分类名称
// Map map = admin.getBookList("1", "100");
// System.out.println(map.get("count")); // 示例,获取书籍数量
}
}

@ -7,6 +7,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
public class Base {
// 数据库连接信息
private static String driver = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/library?&useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
private static String username = "root";
@ -14,15 +15,17 @@ public class Base {
/**
*
*
* @return
* @throws ClassNotFoundException
*
* @return
* @throws ClassNotFoundException JDBC
*/
public static Connection getConnection() throws ClassNotFoundException {
Connection connection = null;
try {
// 加载数据库驱动
Class.forName(driver);
connection = (Connection) DriverManager.getConnection(url, username, password);
// 获取数据库连接
connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
@ -30,66 +33,70 @@ public class Base {
}
/**
*
*
* @param connection
*
* @param preparedStatement
* @param resultSet
*
* @param sql
* @param params
* @return
* @throws SQLException
* SELECT
*
* @param connection
* @param preparedStatement PreparedStatement
* @param resultSet
* @param sql SQL
* @param params SQLnull
* @return
* @throws SQLException SQL
*/
public static ResultSet executequery(Connection connection, PreparedStatement preparedStatement,
ResultSet resultSet, String sql, Object[] params) throws SQLException {
ResultSet resultSet, String sql, Object[] params) throws SQLException {
if (preparedStatement == null) {
preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
// 如果PreparedStatement为null则创建新的PreparedStatement对象
preparedStatement = connection.prepareStatement(sql);
}
// 设置SQL语句中的参数
for (int i = 0; params != null && i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
// 执行查询并返回结果集
resultSet = preparedStatement.executeQuery();
return resultSet;
}
/**
*
*
* @param connection
* @param preparedStatement
* @param sql
* @param params
* @return
* @throws SQLException
* INSERTUPDATEDELETE
*
* @param connection
* @param preparedStatement PreparedStatement
* @param sql SQL
* @param params SQLnull
* @return
* @throws SQLException SQL
*/
public static int executeUpdate(Connection connection, PreparedStatement preparedStatement, String sql,
Object[] params) throws SQLException {
Object[] params) throws SQLException {
if (preparedStatement == null) {
preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
// 如果PreparedStatement为null则创建新的PreparedStatement对象
preparedStatement = connection.prepareStatement(sql);
}
// 设置SQL语句中的参数
for (int i = 0; params != null && i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
// 执行更新语句,返回更新的行数
int updateRows = preparedStatement.executeUpdate();
return updateRows;
}
/**
*
*
* @param connection
* @param preparedStatement
* @param resultSet
* @return
* @throws SQLException
*
*
* @param connection
* @param preparedStatement PreparedStatement
* @param resultSet
* @return truefalse
* @throws SQLException
*/
public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet)
throws SQLException {
boolean flag = true;
// 关闭ResultSet资源
if (resultSet != null) {
try {
resultSet.close();
@ -99,6 +106,8 @@ public class Base {
flag = false;
}
}
// 关闭PreparedStatement资源
if (preparedStatement != null) {
try {
preparedStatement.close();
@ -108,6 +117,8 @@ public class Base {
flag = false;
}
}
// 关闭数据库连接资源
if (connection != null) {
try {
connection.close();
@ -117,7 +128,8 @@ public class Base {
flag = false;
}
}
// 返回资源关闭的状态
return flag;
}
}

@ -6,51 +6,88 @@ import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCBean {
private static String driver = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/library?&useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
private static String username = "root";
private static String password = "root";
// 数据库连接信息
private static String driver = "com.mysql.cj.jdbc.Driver"; // MySQL数据库驱动
private static String url = "jdbc:mysql://localhost:3306/library?&useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"; // 数据库URL
private static String username = "root"; // 数据库用户名
private static String password = "root"; // 数据库密码
// 数据库连接对象
private Connection conn = null;
// 用于执行SQL查询和更新的Statement对象
private Statement stmt = null;
/**
*
*
* `JDBCBean`
*
*/
public JDBCBean() {
try {
// 加载MySQL数据库驱动
Class.forName(driver);
// 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// 创建Statement对象用于执行SQL语句
stmt = conn.createStatement();
System.out.println("同数据库建立连接!");
} catch (Exception ex) {
// 捕获并输出数据库连接错误
System.out.println("无法同数据库建立连接!");
}
}
/**
* INSERTUPDATEDELETE
*
* @param s SQLINSERTUPDATEDELETE
* @return 0
*/
public int executeUpdate(String s) {
int result = 0;
try {
// 执行SQL更新操作
System.out.println(s + "------" + stmt + "-----");
result = stmt.executeUpdate(s);
} catch (Exception e) {
// 捕获并输出执行更新错误
System.out.println("执行更新错误!");
}
return result;
}
/**
* SELECT
*
* @param s SQLSELECT
* @return ResultSetnull
*/
public ResultSet executeQuery(String s) {
ResultSet rs = null;
try {
// 执行SQL查询操作
rs = stmt.executeQuery(s);
} catch (Exception e) {
// 捕获并输出查询执行错误
System.out.println("执行查询错误! " + e.getMessage());
}
return rs;
}
/**
* Statement
*
* Statement
*/
public void close() {
try {
// 关闭Statement对象
stmt.close();
// 关闭数据库连接
conn.close();
} catch (Exception e) {
// 捕获并忽略关闭资源时的异常
}
}
}

@ -16,51 +16,86 @@ import javabean.Admin;
import net.sf.json.JSONObject;
/**
*
*
*zhang
* Servlet
*
* Servlet
*
*
*/
@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());
}
/**
* GET
*
* GET
*
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws ServletException
* @throws IOException
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 简单响应,输出请求的上下文路径
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* POST
*
* POST
*
*
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws ServletException
* @throws IOException
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置响应的内容类型为JSON字符集为utf8
response.setContentType("application/json; charset=utf8");
PrintWriter out = response.getWriter();
// 获取客户端传递的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
// 创建一个HashMap来存储返回的响应数据
HashMap<String, Object> hashMap = new HashMap<String, Object>();
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 admin = new Admin();
String result = null;
try {
// 调用Admin类中的login方法验证用户名和密码
result = admin.login(username, password);
} catch (ClassNotFoundException | SQLException e) {
// 捕获并打印异常
e.printStackTrace();
}
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);
}
// 如果登录成功返回成功的JSON响应
if (result != null && result.equals("1")) {
// 创建会话并设置管理员的用户名
HttpSession session = request.getSession();
session.setAttribute("admin", username);
JSONObject json = JSONObject.fromObject(hashMap);
out.write(json.toString());
// 设置响应内容为成功
hashMap.put("code", 0); // 0表示成功
hashMap.put("msg", "登录成功");
// 返回登录成功后的跳转页面(管理员首页)
hashMap.put("url", request.getContextPath() + "/admin/index.jsp");
} else {
// 如果登录失败,返回失败信息
hashMap.put("code", 1); // 1表示失败
hashMap.put("msg", result); // 登录失败的提示信息
}
}
// 将HashMap转化为JSON对象并返回给客户端
JSONObject json = JSONObject.fromObject(hashMap);
out.write(json.toString());
}
}

@ -17,32 +17,63 @@ import javabean.Base;
import net.sf.json.JSONObject;
/**
* Servlet implementation class BookAdd
* Servlet
*
* Servlet IDID
* JSON
*/
@WebServlet("/admin/bookAdd")
public class BookAdd extends HttpServlet {
/**
* POST
*
* HTTP
* JDBC JSON
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON字符编码为 UTF-8
resp.setContentType("application/json; charset=utf8");
String name = req.getParameter("name");
String author = req.getParameter("author");
String library_id = req.getParameter("library_id");
String sort_id = req.getParameter("sort_id");
String position = req.getParameter("position");
String status = req.getParameter("status");
String description = req.getParameter("description");
System.out.println(description+"-------------"); //debug
// 获取请求参数(书籍信息)
String name = req.getParameter("name"); // 书名
String author = req.getParameter("author"); // 作者
String library_id = req.getParameter("library_id"); // 所属馆ID
String sort_id = req.getParameter("sort_id"); // 书籍类别ID
String position = req.getParameter("position"); // 书籍位置
String status = req.getParameter("status"); // 书籍状态
String description = req.getParameter("description"); // 书籍描述
// 用于调试输出描述字段的内容
System.out.println(description + "-------------"); // debug
// 创建 JSON 对象用于存储响应数据
JSONObject json = new JSONObject();
Connection connection = null;
PreparedStatement pstmt = null;
//ResultSet resultSet = null;
int result = 0;
Connection connection = null; // 数据库连接
PreparedStatement pstmt = null; // SQL 执行对象
int result = 0; // 记录 SQL 执行结果(影响的行数)
// SQL 插入语句
String sql = "insert into books(name, author, library_id, sort_id, position, status, description) values(?,?,?,?,?,?,?)";
System.out.println(sql);
System.out.println(sql); // debug输出 SQL 语句
// 获取响应输出流
PrintWriter out = resp.getWriter();
try {
// 获取数据库连接
connection = (Connection) Base.getConnection();
// 创建 PreparedStatement 对象
pstmt = connection.prepareStatement(sql);
// 设置 SQL 语句中的参数
pstmt.setString(1, name);
pstmt.setString(2, author);
pstmt.setString(3, library_id);
@ -50,26 +81,36 @@ public class BookAdd extends HttpServlet {
pstmt.setString(5, position);
pstmt.setString(6, status);
pstmt.setString(7, description);
// 执行 SQL 插入操作,返回影响的行数
result = pstmt.executeUpdate();
} catch (SQLException e) {
// 捕获 SQL 异常(可以在这里做日志记录或处理)
e.printStackTrace();
} catch (ClassNotFoundException e) {
// 捕获 ClassNotFoundException 异常(数据库驱动类未找到)
e.printStackTrace();
}finally {
} finally {
// 关闭数据库资源
try {
Base.closeResource(connection, pstmt, null);
} catch (SQLException e) {
e.printStackTrace();
e.printStackTrace(); // 关闭资源时发生异常
}
}
if(result==1) {
json.put("code", "0");
json.put("msg", "success");
}else {
json.put("code", "1");
json.put("msg", "error");
// 判断插入操作是否成功,返回相应的 JSON 数据
if (result == 1) {
// 插入成功,返回成功的 JSON 响应
json.put("code", "0"); // code = 0 表示成功
json.put("msg", "success"); // 返回成功消息
} else {
// 插入失败,返回错误的 JSON 响应
json.put("code", "1"); // code = 1 表示失败
json.put("msg", "error"); // 返回失败消息
}
// 将 JSON 响应写入输出流
out.write(json.toString());
}
}

@ -13,34 +13,73 @@ import javabean.JDBCBean;
import net.sf.json.JSONObject;
/**
* Servlet
*
* Servlet ID
* JSON
*/
@WebServlet("/admin/bookDel")
public class BookDel extends HttpServlet {
/**
* GET
*
* ID
* JDBC JSON
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json; charset=utf8");
// 设置响应内容类型为 JSON字符编码为 UTF-8
resp.setContentType("application/json; charset=utf8");
// 获取请求参数中的书籍 ID
String id = req.getParameter("id");
// 创建 JSON 对象用于响应数据
JSONObject json = new JSONObject();
// 创建 JDBCBean 对象用于执行数据库操作
JDBCBean db = new JDBCBean();
String sql = "delete from books where id = " +id;
// 构建 SQL 删除语句,删除指定 ID 的书籍
String sql = "delete from books where id = " + id;
// 定义删除操作的结果变量
int result = 0;
// 默认状态为失败
int code = 1;
String msg = "";
if( id != null && !id.equals("") ) {
// 如果 ID 不为空且不为 "空" 字符串,则执行删除操作
if (id != null && !id.equals("")) {
// 执行 SQL 删除语句,返回影响的行数
result = db.executeUpdate(sql);
}
if( result == 1 ) {
code = 0;
msg = "删除成功";
}else {
code = 1;
msg = "删除失败";
// 判断删除操作是否成功1 表示成功0 表示失败
if (result == 1) {
code = 0; // 删除成功
msg = "删除成功"; // 返回成功消息
} else {
code = 1; // 删除失败
msg = "删除失败"; // 返回失败消息
}
// 将操作结果封装为 JSON 对象
json.put("code", code);
json.put("msg", msg);
// 关闭数据库连接
db.close();
// 获取响应输出流并将 JSON 数据写入响应
PrintWriter out = resp.getWriter();
out.print( json.toString() );
out.print(json.toString());
}
}

@ -3,7 +3,6 @@ package servlet.admin;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
@ -17,12 +16,32 @@ import java.sql.Connection;
import javabean.Base;
import net.sf.json.JSONObject;
/**
* Servlet
*
* Servlet
* JSON
*/
@WebServlet("/admin/bookEdit")
public class BookEdit extends HttpServlet {
/**
* POST
*
* ID
* JSON
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON字符编码为 UTF-8
resp.setContentType("application/json; charset=utf8");
// 获取请求中的参数:书籍 ID、书名、作者、馆藏 ID、分类 ID、位置、状态和描述
String id = req.getParameter("id");
String name = req.getParameter("name");
String author = req.getParameter("author");
@ -31,18 +50,34 @@ public class BookEdit extends HttpServlet {
String position = req.getParameter("position");
String status = req.getParameter("status");
String description = req.getParameter("description");
System.out.println(description+"-------------");
// 打印描述信息进行调试
System.out.println(description + "-------------");
// 创建 JSON 对象用于响应数据
JSONObject json = new JSONObject();
//if(id == null || id.equals(""))
// 声明数据库连接和 PreparedStatement 对象
Connection connection = null;
PreparedStatement pstmt = null;
//ResultSet resultSet = null;
// 更新操作的执行结果
int result = 0;
// SQL 更新语句,更新指定 ID 的书籍信息
String sql = "update books set name=? ,author=? ,library_id=? ,sort_id=? ,position=? ,status=?, description=? where id=?";
// 获取响应输出流
PrintWriter out = resp.getWriter();
try {
// 获取数据库连接
connection = (Connection) Base.getConnection();
// 创建 PreparedStatement 对象,设置 SQL 语句
pstmt = connection.prepareStatement(sql);
// 设置 SQL 语句中的参数
pstmt.setString(1, name);
pstmt.setString(2, author);
pstmt.setString(3, library_id);
@ -51,38 +86,48 @@ public class BookEdit extends HttpServlet {
pstmt.setString(6, status);
pstmt.setString(7, description);
pstmt.setString(8, id);
// 执行更新操作
result = pstmt.executeUpdate();
} catch (SQLException e) {
// 捕获 SQL 异常(目前没有处理,打印错误信息)
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
// 捕获类未找到异常
e.printStackTrace();
}finally {
} finally {
// 确保数据库资源被释放
try {
Base.closeResource(connection, pstmt, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
if(result==1) {
// 判断更新操作是否成功
if (result == 1) {
// 更新成功,返回成功状态
json.put("code", "0");
json.put("msg", "success");
}else {
} else {
// 更新失败,返回失败状态
json.put("code", "1");
json.put("msg", "error");
}
// 将 JSON 响应数据写入输出流
out.write(json.toString());
//System.out.println(postData);
//JSONObject json = JSONObject.fromObject();
}
/**
* GET POST
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}

@ -13,70 +13,111 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javabean.Admin;
import javabean.Common;
import javabean.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet implementation class BookList
* Servlet
*
* Servlet
* JSON
*/
@WebServlet("/admin/bookList")
public class BookList extends HttpServlet {
/**
* GET
*
*
* JSON
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON字符编码为 UTF-8
resp.setContentType("application/json; charset=utf8");
// 初始化响应的 JSON 对象
JSONObject json = new JSONObject();
// 定义返回的数据和状态码
String result = null;
Map<String, Object> map = null;
int code = 1;
String msg = "";
String data = "";
// 获取分页参数,默认值为 page=1 和 limit=10
String page = (String) req.getParameter("page");
String limit = (String) req.getParameter("limit");
// 获取查询条件参数(如果有)
String condition = (String) req.getParameter("condition");
String conditionValue = (String) req.getParameter("conditionValue");
Map where = new HashMap<String, String>();
// 传输数据过滤
// 创建条件过滤的 Map 对象
Map<String, String> where = new HashMap<String, String>();
// 如果没有传递分页参数则默认设置为第一页和每页10条记录
if(page == null) {
page = "1";
}
if(limit == null) {
limit = "10";
}
// 如果传递了查询条件参数,则将其加入查询条件 Map 中
if(condition == null || conditionValue == null || condition.isEmpty() || conditionValue.isEmpty()) {
condition = null;
conditionValue = null;
}else {
} else {
where.put("condition", condition);
where.put("conditionValue", conditionValue);
}
// 实例化 Admin 对象来调用获取书籍列表的方法
Admin admin = new Admin();
try {
// 调用 getBookList 方法来获取书籍列表数据
map = admin.getBookList(page, limit, where);
result = (String) map.get("data");
result = (String) map.get("data"); // 获取查询结果
} catch (ClassNotFoundException | SQLException e) {
msg = "数据库获取信息失败";
// 捕获数据库操作异常,设置错误消息
msg = "数据库获取信息失败";
}
// 如果没有获取到数据或数据为空,返回错误消息
if(result == null || result.isEmpty() || result.equals("1")) {
json.put("code", 1);
json.put("msg", "数据为空");
} else {
// 获取到数据,返回成功状态,并包含查询结果
json.put("code", 0);
json.put("msg", "success");
json.put("count", map.get("count"));
result = "[" +result +"]";
json.put("data", result);
json.put("count", map.get("count")); // 返回总记录数
result = "[" + result + "]"; // 格式化数据为 JSON 数组
json.put("data", result); // 返回数据
}
// 将 JSON 响应数据写入输出流
PrintWriter out = resp.getWriter();
out.print(json.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
/**
* POST doGet
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp); // 对于 POST 请求直接调用 GET 请求处理方法
}
}

@ -18,61 +18,92 @@ import javabean.Base;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet
*
* Servlet
* JSON
*/
@WebServlet("/admin/borrowList")
public class BorrowList extends HttpServlet {
/**
* GET
*
*
* JSON
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON字符编码为 UTF-8
resp.setContentType("application/json; charset=utf8");
// 接收参数
String limit = req.getParameter("limit");
String page = req.getParameter("page");
String condition = (String) req.getParameter("condition");
String conditionValue = (String) req.getParameter("conditionValue");
String where = ""; // 无限制条件
// 获取分页、查询条件相关的参数
String limit = req.getParameter("limit"); // 每页显示的记录数
String page = req.getParameter("page"); // 当前页码
String condition = (String) req.getParameter("condition"); // 查询条件(字段)
String conditionValue = (String) req.getParameter("conditionValue"); // 查询条件值
// 默认值处理:如果未传递分页参数,默认为第一页,默认每页 10 条记录
if(page == null) {
page = "1";
}
if(limit == null) {
limit = "10";
}
// 准备查询
page = "1";
}
if(limit == null) {
limit = "10";
}
// 准备 SQL 查询
Connection connection = null;
PreparedStatement pstmt = null;
PreparedStatement countPstmt = null;
ResultSet resultSet = null;
ResultSet countSet = null;
String sql = "";
String countSql = "";
String sql = "select id, card_id, book_id, "
+ "DATE_FORMAT(borrow_date, '%Y-%m-%d %k:%i:%s') as borrow_date, "
+ "DATE_FORMAT(return_date, '%Y-%m-%d %k:%i:%s') as return_date, "
+ "DATE_FORMAT(end_date, '%Y-%m-%d %k:%i:%s') as end_date, "
+ "illegal, manager_id from borrow_books"; // 查询借阅记录的 SQL 语句
String where = ""; // 记录条件过滤部分
if(condition != null && conditionValue != null && !condition.isEmpty() && !conditionValue.isEmpty()) {
// 如果传递了条件,构造 WHERE 语句进行过滤
where = " where " + condition + " like '%" + conditionValue + "%' ";
sql += where;
} else if(condition != null && condition.equals("other")) {
// 处理特殊的查询条件,例如逾期未还的记录
where = " where return_date is null and curtime() > end_date ";
sql += where;
}
// 添加分页查询的限制
sql += " limit ?,?";
// 准备返回参数
int code = 1;
String msg = "error";
int count = 0;
JSONObject jsonData = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject jsonResult = new JSONObject();
// 进行查询
int code = 1; // 默认失败的状态码
String msg = "error"; // 默认错误信息
int count = 0; // 总记录数
JSONObject jsonData = new JSONObject(); // 存储单条借阅记录的 JSON 对象
JSONArray jsonArray = new JSONArray(); // 存储所有借阅记录的 JSON 数组
JSONObject jsonResult = new JSONObject(); // 最终返回的 JSON 对象
try {
// 获取数据库连接
connection = (Connection) Base.getConnection();
sql = "select id, card_id, book_id, "
+ "DATE_FORMAT(borrow_date, '%Y-%m-%d %k:%i:%s') as borrow_date, "
+ "DATE_FORMAT(return_date, '%Y-%m-%d %k:%i:%s') as return_date, "
+ "DATE_FORMAT(end_date, '%Y-%m-%d %k:%i:%s') as end_date,"
+ "illegal, manager_id "
+ "from borrow_books";
if(condition!=null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) {
where = " where "+ condition +" like '%" +conditionValue +"%' ";
sql += where;
}else if(condition!=null && condition.equals("other")) {
where = " where return_date is null and curtime()>end_date ";
sql +=where;
}
sql += " limit ?,?";
// 执行查询操作,获取借阅记录数据
pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, (Integer.parseInt(page)-1) * Integer.parseInt(limit));
pstmt.setInt(2, Integer.parseInt(limit));
pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit)); // 设置分页的起始位置
pstmt.setInt(2, Integer.parseInt(limit)); // 设置分页的限制条数
resultSet = pstmt.executeQuery();
// 遍历查询结果,将每条借阅记录放入 JSON 数组中
while(resultSet.next()) {
jsonData.put("id", resultSet.getString("id"));
jsonData.put("card_id", resultSet.getString("card_id"));
@ -84,43 +115,46 @@ public class BorrowList extends HttpServlet {
jsonData.put("manager_id", resultSet.getString("manager_id"));
jsonArray.add(jsonData);
}
countSql = "select count(*) as count from borrow_books ";
countSql +=where;
// 查询符合条件的总记录数
String countSql = "select count(*) as count from borrow_books " + where;
countPstmt = connection.prepareStatement(countSql);
countSet = countPstmt.executeQuery();
if(countSet.next()) {
count = countSet.getInt("count");
}
// 根据查询结果设置响应状态
if(!jsonArray.isEmpty()) {
code = 0;
msg = "查询成功";
}else {
} else {
code = 0;
msg = "没有数据";
}
} catch (ClassNotFoundException e) {
msg = "class没找到";
} catch (SQLException e) {
msg = "sql错误";
} finally {
try {
// 关闭数据库连接和相关资源
Base.closeResource(null, pstmt, resultSet);
Base.closeResource(connection, countPstmt, countSet);
} catch (SQLException e) {
msg = "关闭资源失败";
}
}
// 返回数据
// 构造返回的 JSON 数据
jsonResult.put("code", code);
jsonResult.put("count", count);
jsonResult.put("msg", msg);
jsonResult.put("data", jsonArray.toArray());
jsonResult.put("data", jsonArray.toArray()); // 返回的借阅记录数据
// 输出最终的 JSON 响应
PrintWriter out = resp.getWriter();
out.print(jsonResult.toString());
}
}

@ -19,24 +19,44 @@ import javabean.JDBCBean;
import net.sf.json.JSONObject;
/**
* Servlet implementation class CardAdd
* Servlet
*
* Servlet
*
*/
@WebServlet("/admin/cardAdd")
public class CardAdd extends HttpServlet {
/**
* POST
*
*
*
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON字符编码为 UTF-8
resp.setContentType("application/json; charset=utf8");
// 获取参数
String reader = req.getParameter("reader");
String password = req.getParameter("password");
String rule_id = req.getParameter("rule_id");
String status = req.getParameter("status");
// 准备资源
// 获取传递的参数
String reader = req.getParameter("reader"); // 读者
String password = req.getParameter("password"); // 密码
String rule_id = req.getParameter("rule_id"); // 借阅规则 ID
String status = req.getParameter("status"); // 借阅卡状态
// 初始化返回信息
String code = "1";
String msg = "error";
String data = "";
JSONObject json = new JSONObject();
JSONObject jsonData = new JSONObject();
// 数据库连接对象
Connection connection = null;
Connection connection1 = null;
PreparedStatement pstmt = null;
@ -44,23 +64,29 @@ public class CardAdd extends HttpServlet {
String sql = null;
int result = 0;
ResultSet dataSet = null;
// 参数不能为空
if(reader == null || password == null || rule_id == null || rule_id == null || status == null) {
// 参数不能为空校验
if(reader == null || password == null || rule_id == null || status == null) {
code = "1";
msg = "值不能为空";
}else {
} else {
try {
// 获取数据库连接
connection = (Connection) Base.getConnection();
// 插入借阅卡的 SQL 语句
sql = "insert into borrow_card(password, reader, rule_id, status) values(?,?,?,?)";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, password);
pstmt.setString(2, reader);
pstmt.setString(3, rule_id);
pstmt.setString(4, status);
// 执行插入操作
result = pstmt.executeUpdate();
//获取id
connection1= (Connection) Base.getConnection();
// 获取插入记录的 ID
connection1 = (Connection) Base.getConnection();
String findIdSql = "select id from borrow_card where password=? and reader=? and rule_id=? and status=? limit 1";
pstmt1 = connection1.prepareStatement(findIdSql);
pstmt1.setString(1, password);
@ -68,6 +94,8 @@ public class CardAdd extends HttpServlet {
pstmt1.setString(3, rule_id);
pstmt1.setString(4, status);
dataSet = pstmt1.executeQuery();
// 获取插入记录的 ID
if(dataSet.next()) {
jsonData.put("id", dataSet.getString("id"));
}
@ -76,27 +104,33 @@ public class CardAdd extends HttpServlet {
} catch (SQLException e) {
msg = "sql错误";
System.out.println("sql失败");
}
try {
Base.closeResource(connection, pstmt, null);
Base.closeResource(connection1, pstmt1, dataSet);
} catch (SQLException e) {
msg = "关闭资源失败";
} finally {
// 关闭数据库资源
try {
Base.closeResource(connection, pstmt, null);
Base.closeResource(connection1, pstmt1, dataSet);
} catch (SQLException e) {
msg = "关闭资源失败";
}
}
// 根据插入结果更新响应消息
if(result == 1 && !jsonData.isNullObject() && !jsonData.isEmpty()) {
System.out.println(jsonData.toString()); //debug
code = "0";
msg = "添加成功";
}else {
} else {
code = "1";
msg = "执行失败";
}
}
// 构造 JSON 响应
json.put("code", code);
json.put("msg", msg);
json.put("data", jsonData.toString());
// 输出 JSON 响应到客户端
PrintWriter out = resp.getWriter();
out.print(json.toString());
}
}

@ -17,14 +17,34 @@ import javabean.Base;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet
*
* Servlet ID
*
*/
@WebServlet("/admin/cardDel")
public class CardDel extends HttpServlet {
/**
* GET
*
* ID
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON字符编码为 UTF-8
resp.setContentType("application/json; charset=utf8");
// 接收数据
// 获取传入的借阅卡 ID 参数
String id = req.getParameter("id");
// 处理数据
// 初始化数据库连接和 SQL 语句
Connection connection = null;
PreparedStatement delCardPstmt = null;
PreparedStatement delHistoryPstmt = null;
@ -32,34 +52,39 @@ public class CardDel extends HttpServlet {
String delHistorySql = null;
int delCardResult = 0;
int delHistoryResult = 0;
// 返回数据
// 初始化返回信息
String code = "1";
String msg = "error";
JSONObject jsonObject = new JSONObject();
JSONObject jsonData = new JSONObject();
// 开始处理
// 如果传入的 ID 不为空,开始处理删除操作
if(id != null && !id.equals("")) {
try {
// 公共连接
// 获取数据库连接
connection = (Connection) Base.getConnection();
// 删除借书记录
delHistorySql = "delete from borrow_books where card_id=?";
delHistoryPstmt = connection.prepareStatement(delHistorySql);
delHistoryPstmt.setString(1, id);
delHistoryResult = delHistoryPstmt.executeUpdate();
// 返回删除记录
// 返回删除的借书记录数
jsonData.put("num", delHistoryResult);
// 删除阅读证
// 删除借阅卡
delCardSql = "delete from borrow_card where id=? limit 1";
delCardPstmt = connection.prepareStatement(delCardSql);
delCardPstmt.setString(1, id);
delCardResult = delCardPstmt.executeUpdate();
} catch (ClassNotFoundException e) {
msg = "连接失败";
} catch (SQLException e) {
msg = "sql错误";
} finally {
// 关闭数据库资源
try {
delCardPstmt.close();
Base.closeResource(connection, delCardPstmt, null);
@ -68,15 +93,20 @@ public class CardDel extends HttpServlet {
}
}
}
// 获取响应输出流
PrintWriter out = resp.getWriter();
// 判断删除操作是否成功
if(delCardResult == 1) {
code = "0";
msg = "删除借阅证成功";
}
// 返回 JSON 格式的响应
jsonObject.put("code", code);
jsonObject.put("msg", msg);
jsonObject.put("data", jsonData);
out.print(jsonObject.toString());
}
}

@ -17,64 +17,97 @@ import java.sql.Connection;
import javabean.Base;
import net.sf.json.JSONObject;
/**
* Servlet
*
* Servlet ID
*/
@WebServlet("/admin/cardEdit")
public class CardEdit extends HttpServlet {
/**
* POST
*
* ID ID
*
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON字符编码为 UTF-8
resp.setContentType("application/json; charset=utf8");
// 接受数据
// 获取传入的借阅卡参数
String id = req.getParameter("id");
String password = req.getParameter("password");
String reader = req.getParameter("reader");
String rule_id = req.getParameter("rule_id");
String status = req.getParameter("status");
// 准备资源
// 初始化数据库连接和 SQL 语句
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
int result = 0;
String sql = null;
// 返回数据
// 初始化返回信息
String code = "1";
String msg = "error";
JSONObject json = new JSONObject();
PrintWriter out = resp.getWriter();
// 判断数据
if(id == null || password == null || reader == null || reader == null || status == null ||
// 判断接收到的参数是否为空
if(id == null || password == null || reader == null || rule_id == null || status == null ||
id.equals("") || password.equals("") || reader.equals("") || rule_id.equals("") || status.equals("")) {
code = "1";
msg = "参数不能为空";
}else {
} else {
// 构建更新 SQL 语句
sql = "update borrow_card set password=?, reader=?, rule_id=?, status=? where id=?";
try {
// 获取数据库连接
connection = (Connection) Base.getConnection();
// 预编译 SQL 语句并设置参数
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, password);
pstmt.setString(2, reader);
pstmt.setString(3, rule_id);
pstmt.setString(4, status);
pstmt.setString(5, id);
// 执行更新操作
result = pstmt.executeUpdate();
} catch (ClassNotFoundException e1) {
msg = "错误";
msg = "数据库连接错误";
} catch (SQLException e) {
msg = "sql错误";
msg = "SQL 错误";
} finally {
try {
// 关闭数据库连接和资源
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
msg = "关闭失败";
msg = "资源关闭失败";
}
}
// 判断更新操作是否成功
if(result == 1) {
code = "0";
msg = "修改成功";
}
json.put("code", code);
json.put("msg", msg);
out.print(json.toString());
}
}
// 将返回信息封装成 JSON 格式
json.put("code", code);
json.put("msg", msg);
// 输出响应数据
out.print(json.toString());
}
}

@ -18,25 +18,46 @@ import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet implementation class CardList
* Servlet
*
* Servlet
*/
@WebServlet("/admin/cardList")
public class CardList extends HttpServlet {
/**
* GET
*
*
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON字符编码为 UTF-8
resp.setContentType("application/json; charset=utf8");
// 接收参数
// 接收查询参数limit, page, condition 和 conditionValue
String limit = req.getParameter("limit");
String page = req.getParameter("page");
String condition = (String) req.getParameter("condition");
String conditionValue = (String) req.getParameter("conditionValue");
String condition = req.getParameter("condition");
String conditionValue = req.getParameter("conditionValue");
// 初始化 SQL 查询条件
String where = null; // 无限制条件
// 设置默认值
if (page == null) {
page = "1";
}
if (limit == null) {
limit = "10";
}
// 初始化数据库连接和 SQL 语句
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
@ -44,29 +65,41 @@ public class CardList extends HttpServlet {
String msg = "error";
int count = 0;
String sql = "";
// String countSql = ""
// 准备返回数据的 JSON 对象
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject jsonResult = new JSONObject();
try {
// 获取数据
// 获取数据库连接
connection = (Connection) Base.getConnection();
sql = "select id,password,reader,rule_id,status from borrow_card";
// where
// 初始 SQL 查询语句
sql = "select id, password, reader, rule_id, status from borrow_card";
// 如果有查询条件,构造 where 子句
if (condition != null && conditionValue != null && !condition.isEmpty() && !conditionValue.isEmpty()) {
where = " where " + condition + " like '%" + conditionValue + "%'";
sql = sql + where;
}
// 分页
sql += " order by id desc limit ?,?";
// 构造分页查询
sql += " order by id desc limit ?,?";
pstmt = connection.prepareStatement(sql);
try {
// 设置分页参数
pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit));
pstmt.setInt(2, Integer.parseInt(limit));
} catch (NumberFormatException | SQLException e1) {
// 异常捕获,避免参数错误影响执行
}
// 执行查询
resultSet = pstmt.executeQuery();
while (resultSet.next()) {
// 构造每一条借阅卡记录的 JSON 数据
jsonObject.put("id", resultSet.getString("id"));
jsonObject.put("password", resultSet.getString("password"));
jsonObject.put("reader", resultSet.getString("reader"));
@ -74,41 +107,46 @@ public class CardList extends HttpServlet {
jsonObject.put("status", resultSet.getString("status"));
jsonArray.add(jsonObject);
}
// 获取总数
sql = "select count(*) as count from borrow_card ";
// 有限制
// 获取借阅卡总数,用于分页
sql = "select count(*) as count from borrow_card";
if (where != null) {
sql = sql + where;
}
pstmt = connection.prepareStatement(sql);
resultSet = pstmt.executeQuery();
if (resultSet.next()) {
count = resultSet.getInt("count");
}
// 判断查询结果是否为空
if (!jsonArray.isEmpty()) {
code = 0;
msg = "成功";
msg = "查询成功";
}
} catch (ClassNotFoundException e) {
msg = "没找到";
msg = "数据库连接失败";
e.printStackTrace();
} catch (SQLException e) {
msg = "sql错误";
msg = "SQL 错误";
} finally {
// 关闭数据库连接和资源
try {
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
msg = "关闭失败";
msg = "资源关闭失败";
}
}
// 构造返回的 JSON 数据
jsonResult.put("code", code);
jsonResult.put("count", count);
jsonResult.put("msg", msg);
jsonResult.put("data", jsonArray.toString());
// 输出响应
PrintWriter out = resp.getWriter();
out.print(jsonResult.toString());
// out.print("{\"code\":0,\"msg\":\"\",\"count\":\"234\",\"data\":[{\"id\":\"1\",\"password\":\"23442\",\"reader\":\"minm\",\"rule_id\":\"1\",\"status\":\"2\"}]}");
}
}

@ -19,14 +19,31 @@ import javabean.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet
*
* Servlet 30
*/
@WebServlet("/admin/libraryData")
public class LibraryData extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
* GET
*
* 30
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON字符编码为 UTF-8
resp.setContentType("application/json; charset:utf8");
// 准备参数
// 初始化数据库连接、SQL语句、JSON 数据
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
@ -34,51 +51,61 @@ public class LibraryData extends HttpServlet {
JSONObject jsonObject = new JSONObject();
JSONArray jsonData = new JSONArray();
JSONArray jsonDays = new JSONArray();
// 返回参数
// 返回参数:状态码、消息、数据
int code = 1;
String msg = "error";
int count = 0;
// 获取响应输出流
PrintWriter out = resp.getWriter();
// 开始查询
// 开始查询获取过去30天的借阅数据
try {
connection = Base.getConnection();
int i = 30;
// 获取30天
while(i!=0) {
connection = Base.getConnection(); // 获取数据库连接
int i = 30; // 查询过去30天的数据
while (i != 0) {
i--;
sql = "select count(*) as count from borrow_books where date_format(borrow_date,'%Y-%m-%d')=? order by id desc";
String date = DateTime.showDate(-i); // 设置日期
String md = DateTime.showMD(-i);
String date = DateTime.showDate(-i); // 获取当前日期的前i天
String md = DateTime.showMD(-i); // 获取当前日期的前i天的月日格式MM-dd
pstmt = connection.prepareStatement(sql);
pstmt.setString(1,date);
pstmt.setString(1, date); // 设置日期参数
resultSet = pstmt.executeQuery();
while(resultSet.next()) {
while (resultSet.next()) {
// 将借阅数量和日期分别添加到 JSON 数组中
jsonData.add(resultSet.getString("count"));
jsonDays.add(md);
}
}
}
// 将查询结果放入 JSON 对象中
jsonObject.put("data", jsonData);
jsonObject.put("days", jsonDays);
if(!jsonObject.isEmpty()) {
code = 0;
msg = "查询成功";
}else {
// 判断查询结果是否为空
if (!jsonObject.isEmpty()) {
code = 0; // 状态码为 0 表示成功
msg = "查询成功";
} else {
msg = "数据为空";
}
} catch (ClassNotFoundException e) {
msg = "没找到";
msg = "数据库连接失败";
e.printStackTrace();
} catch (SQLException e) {
msg = "sql错误";
}finally {
msg = "SQL 错误";
} finally {
// 关闭数据库连接和资源
try {
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
msg = "关闭失败";
msg = "关闭数据库资源失败";
}
}
out.print( Util.jsonResponse(code, msg, jsonObject.toString()) );
}
// 返回 JSON 响应
out.print(Util.jsonResponse(code, msg, jsonObject.toString()));
}
}

@ -9,20 +9,35 @@ import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class LoginOut
* Servlet
*
* Servlet
*/
@WebServlet("/admin/logOut")
public class LoginOut extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = req.getSession();
if(session.getAttribute("admin") != null) {
session.removeAttribute("admin");
}
resp.sendRedirect(req.getContextPath() +"/adminLogin.html");
}
/**
* GET
*
* `admin`
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取当前会话对象
HttpSession session = req.getSession();
// 如果会话中存在 "admin" 属性(即管理员已登录),则移除该属性
if (session.getAttribute("admin") != null) {
session.removeAttribute("admin");
}
// 重定向到管理员登录页面
resp.sendRedirect(req.getContextPath() + "/adminLogin.html");
}
}

@ -19,46 +19,71 @@ import javabean.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet
*
* Servlet SQL
*/
@WebServlet("/admin/managerAdd")
public class ManagerAdd extends HttpServlet {
/**
* POST
*
*
* JSON
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON
resp.setContentType("application/json; charset=utf8");
// 接收参数
// 接收前端传递的参数
String name = req.getParameter("name");
String account = req.getParameter("account");
String password = req.getParameter("password");
String email = req.getParameter("email");
// 准备参数
// SQL 语句及数据库连接准备
String sql = "";
Connection connection = null;
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
int result = 0;
int count = 0;
// 返回参数
// 返回的响应数据
int code = 1;
String msg = "";
PrintWriter out = resp.getWriter();
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
// 进行查询
if(name==null || name.equals("") || account==null || account.equals("") || password==null || password.equals("") || email==null || email.equals("")) {
JSONObject jsonObject = new JSONObject();
// 进行参数校验,检查不能为空的字段
if (name == null || name.equals("") || account == null || account.equals("") ||
password == null || password.equals("") || email == null || email.equals("")) {
msg = "参数不能为空";
out.print(Util.jsonResponse(code, msg, null));
}else {
} else {
try {
// 获取数据库连接
connection = (Connection) Base.getConnection();
// 验证账号
// 验证账号是否已存在
sql = "select count(*) as count from manager where account=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, account);
resultSet = pstmt.executeQuery();
resultSet.next();
count = resultSet.getInt("count");
// 添加管理员
if(count == 0) {
// 如果账号不存在,则插入新管理员
if (count == 0) {
sql = "insert into manager(name, account, password, email) values(?,?,?,?)";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, name);
@ -67,13 +92,14 @@ public class ManagerAdd extends HttpServlet {
pstmt.setString(4, email);
result = pstmt.executeUpdate();
}
// 返回数据
if(result == 1 && count == 0) {
// 根据执行结果返回响应信息
if (result == 1 && count == 0) {
code = 0;
msg = "添加成功";
}else if(count > 0){
} else if (count > 0) {
msg = "账号重复";
}else {
} else {
msg = "添加失败";
}
} catch (ClassNotFoundException e) {
@ -82,11 +108,14 @@ public class ManagerAdd extends HttpServlet {
msg = "sql错误";
} finally {
try {
// 关闭数据库资源
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 返回响应结果
out.print(Util.jsonResponse(code, msg, null));
}
}

@ -17,42 +17,75 @@ import java.sql.Connection;
import javabean.Base;
import javabean.Util;
/**
* Servlet
*
* Servlet ID
*/
@WebServlet("/admin/managerDel")
public class ManagerDel extends HttpServlet {
/**
* GET ID
*
* ID
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON
resp.setContentType("application/json; charset=utf8");
// 获取请求中的 ID 参数
String id = req.getParameter("id");
// 准备参数
// SQL 语句及数据库连接准备
String sql = "";
Connection connection = null;
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
int result = 0;
// 返回参数
// 返回的响应数据
int code = 1;
String msg = "";
PrintWriter out = resp.getWriter();
try {
// 获取数据库连接
connection = (Connection) Base.getConnection();
// SQL 删除语句
sql = "delete from manager where id=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, id);
result = pstmt.executeUpdate();
if(result == 1) {
pstmt.setString(1, id); // 设置 ID 参数
result = pstmt.executeUpdate(); // 执行删除操作
// 根据操作结果返回响应
if (result == 1) {
code = 0;
msg = "删除成功";
}else {
} else {
msg = "删除失败";
}
} catch (ClassNotFoundException e) {
msg = "class not found";
} catch (SQLException e) {
msg = "sql错误";
} finally {
// 关闭数据库资源
try {
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 返回 JSON 格式的响应
out.print(Util.jsonResponse(code, msg, null));
}
}

@ -19,50 +19,70 @@ import javabean.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet
*
* Servlet ID
*/
@WebServlet("/admin/managerEdit")
public class ManagerEdit extends HttpServlet {
/**
* POST ID
*
* ID
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON
resp.setContentType("application/json; charset=utf8");
// 接收参数
// 获取请求中的参数
String id = req.getParameter("id");
String name = req.getParameter("name");
String password = req.getParameter("password");
String email = req.getParameter("email");
// 准备参数
// 返回的响应参数
String sql = "";
Connection connection = null;
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
int result = 0;
// 返回参数
int code = 1;
String msg = "";
PrintWriter out = resp.getWriter();
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
// 进行查询
if(name==null || name.equals("")|| password==null || password.equals("") || email==null || email.equals("")) {
// 如果参数为空,返回错误信息
if (name == null || name.equals("") || password == null || password.equals("") || email == null || email.equals("")) {
msg = "参数不能为空";
out.print(Util.jsonResponse(code, msg, null));
}else {
} else {
try {
// 获取数据库连接
connection = (Connection) Base.getConnection();
// 添加管理员
// SQL 更新语句
sql = "update manager set name=?, password=?, email=? where id=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
pstmt.setString(3, email);
pstmt.setString(4, id);
pstmt.setString(1, name); // 设置名称
pstmt.setString(2, password); // 设置密码
pstmt.setString(3, email); // 设置邮箱
pstmt.setString(4, id); // 设置管理员 ID
// 执行更新操作
result = pstmt.executeUpdate();
// 返回数据
if(result == 1 ){
// 根据更新结果返回响应
if (result == 1) {
code = 0;
msg = "修改成功";
}else {
} else {
msg = "修改失败";
}
} catch (ClassNotFoundException e) {
@ -70,15 +90,16 @@ public class ManagerEdit extends HttpServlet {
} catch (SQLException e) {
msg = "sql错误";
} finally {
// 关闭数据库资源
try {
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 返回 JSON 格式的响应
out.print(Util.jsonResponse(code, msg, null));
}
}
}

@ -19,47 +19,75 @@ import javabean.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet
*
* Servlet
*/
@WebServlet("/admin/managerList")
public class ManagerList extends HttpServlet {
/**
* GET
*
* `manager` ID
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON
resp.setContentType("application/json; charset=utf8");
// 准备数据
// 初始化数据库连接相关变量
Connection connection = null;
PreparedStatement pstmt = null;
String sql = "";
ResultSet resultSet = null;
// 返回数据
int code = 1;
String msg = "error";
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
// 返回的数据初始化
int code = 1; // 默认为失败
String msg = "error"; // 默认为错误信息
JSONObject jsonObject = new JSONObject(); // 存储单条管理员信息
JSONArray jsonArray = new JSONArray(); // 存储所有管理员信息
PrintWriter out = resp.getWriter();
try {
// 获取数据库连接
connection = (Connection) Base.getConnection();
// SQL 查询语句:查询所有管理员信息
sql = "select * from manager";
pstmt = connection.prepareStatement(sql);
resultSet = pstmt.executeQuery();
// 遍历查询结果,将每条管理员信息加入 JSON 数组
while (resultSet.next()) {
jsonObject.put("id", resultSet.getString("id"));
jsonObject.put("name", resultSet.getString("name"));
jsonObject.put("account", resultSet.getString("account"));
jsonObject.put("password", resultSet.getString("password"));
jsonObject.put("email", resultSet.getString("email"));
jsonArray.add(jsonObject);
jsonObject.put("id", resultSet.getString("id")); // 管理员ID
jsonObject.put("name", resultSet.getString("name")); // 管理员名称
jsonObject.put("account", resultSet.getString("account")); // 管理员账号
jsonObject.put("password", resultSet.getString("password")); // 管理员密码
jsonObject.put("email", resultSet.getString("email")); // 管理员邮箱
jsonArray.add(jsonObject); // 将该管理员信息加入 JSON 数组
}
// 检查是否成功查询到管理员信息
if (!jsonArray.isEmpty()) {
code = 0;
code = 0; // 查询成功
msg = "查询成功";
} else {
msg = "数据为空";
msg = "数据为空"; // 查询结果为空
}
} catch (ClassNotFoundException e) {
msg = "class找不到";
msg = "class找不到"; // 类加载失败
} catch (SQLException e) {
msg = "sql错误";
msg = "sql错误"; // SQL 执行错误
}
// 返回 JSON 响应
out.print(Util.jsonResponse(code, msg, jsonArray.toString()));
}
}

@ -19,76 +19,103 @@ import javabean.Base;
import javabean.Common;
import javabean.Util;
/**
* Servlet
*
* Servlet POST
*/
@WebServlet("/admin/ruleAdd")
public class RuleAdd extends HttpServlet {
/**
* POST
*
*
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON
resp.setContentType("application/json; charset=utf8");
// 准备数据
// 初始化数据库连接相关变量
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
int result = 0;
String sql = "";
String borrow_library = "";
// 准备返回数据
int code = 1;
String msg = "";
// 获取数据
// 获取限定图书馆1、2、3
// 返回的数据初始化
int code = 1; // 默认为失败
String msg = ""; // 默认为错误信息
// 获取数据:首先检查允许借阅的图书馆
int num = 0;
try {
// 获取图书馆的配置信息
Map<String, String> libraryMap = Common.getLibraryMap();
for(String key : libraryMap.keySet()) {
if(req.getParameter("borrow_library[" +key +"]") != null) {
if(num == 0) {
// 遍历图书馆信息,获取前端传来的借阅图书馆
for (String key : libraryMap.keySet()) {
if (req.getParameter("borrow_library[" + key + "]") != null) {
if (num == 0) {
borrow_library += key;
num++;
}else {
borrow_library += "、"+key;
} else {
borrow_library += "、" + key;
}
}
}
if(borrow_library.isEmpty()) {
// 检查是否选择了允许的图书馆
if (borrow_library.isEmpty()) {
msg = "允许图书馆不能为空";
}
} catch (SQLException e) {
msg = "获取图书馆失败";
}
String borrow_num = req.getParameter("borrow_num");
String limit_day = req.getParameter("limit_day");
String overtime_fee = req.getParameter("overtime_fee");
try {
connection = (Connection) Base.getConnection();
sql = "insert into rules(borrow_num, limit_day, borrow_library, overtime_fee) values(?,?,?,?)";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, borrow_num);
pstmt.setString(2, limit_day);
pstmt.setString(3, borrow_library);
pstmt.setString(4, overtime_fee);
result = pstmt.executeUpdate();
if(result == 1) {
code = 0;
msg = "success";
}
} catch (ClassNotFoundException e) {
msg = "classnotfound";
} catch (SQLException e) {
msg = "SQL错误";
} finally {
// 获取其他规则参数
String borrow_num = req.getParameter("borrow_num"); // 借阅数量
String limit_day = req.getParameter("limit_day"); // 限制天数
String overtime_fee = req.getParameter("overtime_fee"); // 逾期费用
// 如果借阅图书馆不为空,保存借阅规则
if (msg.isEmpty()) {
try {
Base.closeResource(connection, pstmt, null);
connection = Base.getConnection();
sql = "insert into rules(borrow_num, limit_day, borrow_library, overtime_fee) values(?,?,?,?)";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, borrow_num);
pstmt.setString(2, limit_day);
pstmt.setString(3, borrow_library);
pstmt.setString(4, overtime_fee);
result = pstmt.executeUpdate();
// 判断插入是否成功
if (result == 1) {
code = 0;
msg = "success";
}
} catch (ClassNotFoundException e) {
msg = "classnotfound";
} catch (SQLException e) {
msg = "关闭失败";
msg = "SQL错误";
} finally {
try {
// 关闭数据库资源
Base.closeResource(connection, pstmt, null);
} catch (SQLException e) {
msg = "关闭失败";
}
}
}
// 将结果返回给前端
PrintWriter out = resp.getWriter();
out.print(Util.jsonResponse(code, msg, null));
}
}

@ -17,43 +17,71 @@ import java.sql.Connection;
import javabean.Base;
import javabean.Util;
/**
* Servlet
*
* Servlet ID
*/
@WebServlet("/admin/ruleDel")
public class RuleDel extends HttpServlet {
/**
* GET
*
* ID ID
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON
resp.setContentType("application/json; charset=utf8");
// 接受数据
// 获取前端传来的规则 ID
String id = req.getParameter("id");
// 准备数据
// 数据库连接相关变量
Connection connection = null;
PreparedStatement pstmt = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
int result = 0;
String sql = "";
// 返回数据
int code = 1;
String msg = "error";
PrintWriter out = resp.getWriter();
// 进行查询
// 返回数据相关变量
int code = 1; // 默认为失败
String msg = "error"; // 默认为错误消息
// 获取 PrintWriter 用于返回响应
PrintWriter out = resp.getWriter();
try {
connection = (Connection) Base.getConnection();
// 获取数据库连接
connection = Base.getConnection();
sql = "delete from rules where id = ?";
// 使用 PreparedStatement 来执行删除操作
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, id);
result = pstmt.executeUpdate();
if(result == 1) {
result = pstmt.executeUpdate(); // 执行删除操作
// 判断删除操作是否成功
if (result == 1) {
code = 0;
msg = "删除成功";
}else {
msg = "删除失败";
msg = "删除成功"; // 删除成功
} else {
msg = "删除失败"; // 删除失败
}
} catch (ClassNotFoundException e) {
msg = "class没找到";
msg = "class没找到"; // 发生 ClassNotFoundException 异常时返回错误消息
} catch (SQLException e) {
msg = "sql错误";
msg = "sql错误"; // 发生 SQL 异常时返回错误消息
}
// 将结果作为 JSON 响应返回
out.print(Util.jsonResponse(code, msg, null));
}
}

@ -20,64 +20,90 @@ import javabean.Common;
import javabean.Util;
/**
* Servlet implementation class RuleEdit
* Servlet
*
* Servlet ID
* ID
*/
@WebServlet("/admin/ruleEdit")
public class RuleEdit extends HttpServlet {
/**
* POST
*
* ID ID
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON
resp.setContentType("application/json; charset=utf8");
// 准备数据
// 数据库操作相关变量
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
int result = 0;
String sql = "";
String borrow_library = "";
// 准备返回数据
int code = 1;
// 返回数据相关变量
int code = 1; // 默认为失败
String msg = "";
// 获取数据
// 获取限定图书馆1、2、3
// 获取前端传来的修改数据
// 获取允许借阅的图书馆信息
int num = 0;
try {
// 获取图书馆的映射关系
Map<String, String> libraryMap = Common.getLibraryMap();
for(String key : libraryMap.keySet()) {
if(req.getParameter("borrow_library[" +key +"]") != null) {
if(num == 0) {
// 处理前端传来的图书馆选择数据
for (String key : libraryMap.keySet()) {
if (req.getParameter("borrow_library[" + key + "]") != null) {
if (num == 0) {
borrow_library += key;
num++;
}else {
borrow_library += "、"+key;
} else {
borrow_library += "、" + key;
}
}
}
if(borrow_library.isEmpty()) {
// 如果没有选择图书馆,则返回错误
if (borrow_library.isEmpty()) {
msg = "允许图书馆不能为空";
}
} catch (SQLException e) {
msg = "获取图书馆失败";
}
// 获取其他修改字段
String borrow_num = req.getParameter("borrow_num");
String limit_day = req.getParameter("limit_day");
String overtime_fee = req.getParameter("overtime_fee");
String id = req.getParameter("id");
// 执行数据库更新操作
try {
connection = (Connection) Base.getConnection();
sql = "update rules set borrow_num=?, limit_day=?, borrow_library=?,overtime_fee=? where id=?";
connection = Base.getConnection();
sql = "update rules set borrow_num=?, limit_day=?, borrow_library=?, overtime_fee=? where id=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, borrow_num);
pstmt.setString(2, limit_day);
pstmt.setString(3, borrow_library);
pstmt.setString(4, overtime_fee);
pstmt.setString(5, id);
result = pstmt.executeUpdate();
if(result == 1) {
result = pstmt.executeUpdate(); // 执行更新操作
// 更新成功
if (result == 1) {
code = 0;
msg = "success";
}
} catch (ClassNotFoundException e) {
msg = "classnotfound";
} catch (SQLException e) {
@ -89,12 +115,9 @@ public class RuleEdit extends HttpServlet {
msg = "关闭失败";
}
}
// 返回响应
PrintWriter out = resp.getWriter();
out.print(Util.jsonResponse(code, msg, null));
}
}

@ -19,55 +19,83 @@ import javabean.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet JSON
*
* Servlet JSON
*/
@WebServlet("/admin/ruleList")
public class RuleList extends HttpServlet {
/**
* GET
*
* SQL JSON
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON 格式
resp.setContentType("application/json; charset=utf8");
// 准备查询
// 数据库操作相关变量
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
String sql = "";
// 准备返回参数
int code = 1;
// 返回数据相关变量
int code = 1; // 默认为失败
String msg = "error";
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
try {
connection = (Connection) Base.getConnection();
// 获取数据库连接
connection = Base.getConnection();
// 执行查询语句
sql = "select * from rules";
pstmt = connection.prepareStatement(sql);
resultSet = pstmt.executeQuery();
while(resultSet.next()) {
// 处理查询结果
while (resultSet.next()) {
jsonObject.put("id", resultSet.getString("id"));
jsonObject.put("limit_day", resultSet.getString("limit_day"));
jsonObject.put("borrow_num", resultSet.getString("borrow_num"));
jsonObject.put("borrow_library", resultSet.getString("borrow_library"));
jsonObject.put("overtime_fee", resultSet.getString("overtime_fee"));
// 将每条规则数据加入到 JSON 数组中
jsonArray.add(jsonObject);
}
code = 0;
if(!jsonArray.isEmpty()) {
// 根据查询结果设置响应代码和消息
code = 0; // 查询成功
if (!jsonArray.isEmpty()) {
msg = "查询成功";
}else {
} else {
msg = "没有数据";
}
} catch (SQLException e) {
msg = "sql错误";
} catch (ClassNotFoundException e) {
msg = "class没找到";
} finally {
try {
// 关闭数据库连接及资源
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
msg = "关闭失败";
}
}
// 将查询结果封装成 JSON 格式的响应并返回
PrintWriter out = resp.getWriter();
out.print( Util.jsonResponse(code, msg, jsonArray.toString()) );
out.print(Util.jsonResponse(code, msg, jsonArray.toString()));
}
}

@ -17,67 +17,93 @@ import javabean.Base;
import javabean.Common;
import javabean.Util;
/**
* Servlet
*
* Servlet
* JSON
*/
@WebServlet("/admin/sortAdd")
public class SortAdd extends HttpServlet{
public class SortAdd extends HttpServlet {
/**
* POST
*
*
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应类型为 JSON 格式
resp.setContentType("application/json; charset=utf8");
// 接受数据
// 获取前端传来的分类名称和描述
String name = req.getParameter("name");
String description = req.getParameter("description");
// 准备数据
// 数据库操作相关变量
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
int result = 0;
String sql = "";
int count = 0 ;
// 准备返回数据
int code = 1;
String msg = "";
int count = 0;
// 返回结果相关变量
int code = 1; // 默认为失败
String msg = ""; // 默认消息
try {
connection = (Connection) Base.getConnection();
// 查询重复name
// 获取数据库连接
connection = Base.getConnection();
// 查询是否已有相同的分类名称
sql = "select count(*) as count from book_sort where name=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, name);
resultSet = pstmt.executeQuery();
if(resultSet.next()) {
// 有重复
if(resultSet.getInt("count") > 0) {
if (resultSet.next()) {
// 如果分类名称已存在
if (resultSet.getInt("count") > 0) {
msg = "分类名不能重复";
}else {
// 进行插入
} else {
// 分类名称不存在,执行插入操作
sql = "insert into book_sort(name, description) values(?,?)";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, description);
result = pstmt.executeUpdate();
if(result == 1) {
if (result == 1) {
// 插入成功
code = 0;
msg = "添加成功";
}else {
} else {
// 插入失败
msg = "添加失败";
}
}
}
} catch (ClassNotFoundException e) {
msg = "classnotfound";
msg = "class not found";
} catch (SQLException e) {
msg = "SQL错误";
msg = "SQL 错误";
} finally {
try {
// 关闭数据库资源
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
msg = "关闭失败";
}
}
// 输出返回结果
PrintWriter out = resp.getWriter();
out.print(Util.jsonResponse(code, msg, null));
}
}

@ -16,67 +16,91 @@ import javax.servlet.http.HttpServletResponse;
import javabean.Base;
import javabean.Util;
/**
* Servlet
*
* Servlet IDID 1
* ID
*/
@WebServlet("/admin/sortDel")
public class SortDel extends HttpServlet {
/**
* GET
*
* ID 1
* ID ID
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 默认1为未分类
// 默认分类 ID 为 1不允许删除
String defaultId = "1";
resp.setContentType("application/json; charset=utf8");
// 接受数据
// 获取请求参数(分类 ID
String id = req.getParameter("id");
// 准备数据
// 数据库操作相关变量
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
int result = 0;
String sql = "";
// 准备返回数据
int code = 1;
String msg = "";
// 返回结果相关变量
int code = 1; // 默认为失败
String msg = ""; // 默认为错误消息
try {
// 不能删除未分类
if(defaultId.equals(id)) {
// 判断是否为默认分类,若是则不允许删除
if (defaultId.equals(id)) {
msg = "不能删除未分类";
}else {
connection = (Connection) Base.getConnection();
// 分类下的文章修改
} else {
// 获取数据库连接
connection = Base.getConnection();
// 更新该分类下的书籍,将它们的分类 ID 修改为默认分类
sql = "update books set sort_id=? where sort_id=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, defaultId);
pstmt.setString(2, id);
result = pstmt.executeUpdate();
// 进行删除
pstmt.setString(1, defaultId); // 设置新的分类 ID默认分类
pstmt.setString(2, id); // 设置要删除的分类 ID
result = pstmt.executeUpdate(); // 执行更新操作
// 删除分类记录
sql = "delete from book_sort where id=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, id);
result = pstmt.executeUpdate();
if(result == 1) {
pstmt.setString(1, id); // 设置要删除的分类 ID
result = pstmt.executeUpdate(); // 执行删除操作
// 根据删除结果设置返回消息
if (result == 1) {
code = 0;
msg = "删除成功";
}else {
} else {
msg = "删除失败";
}
}
} catch (ClassNotFoundException e) {
msg = "classnotfound";
msg = "class not found"; // 类未找到错误
} catch (SQLException e) {
msg = "SQL错误";
msg = "SQL 错误"; // SQL 错误
} finally {
// 关闭数据库资源
try {
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
msg = "关闭失败";
msg = "关闭失败"; // 资源关闭失败
}
}
// 输出 JSON 格式的响应结果
PrintWriter out = resp.getWriter();
out.print(Util.jsonResponse(code, msg, null));
}
}

@ -16,71 +16,96 @@ import javax.servlet.http.HttpServletResponse;
import javabean.Base;
import javabean.Util;
/**
* Servlet
*
* Servlet
*
*/
@WebServlet("/admin/sortEdit")
public class SortEdit extends HttpServlet {
/**
* POST
*
*
*
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应类型为 JSON
resp.setContentType("application/json; charset=utf8");
// 接受数据
// 获取请求中的数据(分类 ID分类名和描述
String id = req.getParameter("id");
String name = req.getParameter("name");
String description = req.getParameter("description");
// 准备数据
// 数据库操作相关变量
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
int result = 0;
String sql = "";
int count = 0 ;
// 准备返回数据
int code = 1;
String msg = "";
// 返回结果相关变量
int code = 1; // 默认为失败
String msg = ""; // 默认为错误消息
try {
connection = (Connection) Base.getConnection();
// 查询重复name
// 获取数据库连接
connection = Base.getConnection();
// 查询分类名是否已经存在(排除当前分类 ID
sql = "select count(*) as count from book_sort where name=? and id != ?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, id);
resultSet = pstmt.executeQuery();
if(resultSet.next()) {
// 有重复
if(resultSet.getInt("count") > 0) {
msg = "分类名不能重复";
}else {
// 进行插入
// 判断分类名是否重复
if (resultSet.next()) {
if (resultSet.getInt("count") > 0) {
msg = "分类名不能重复"; // 分类名已经存在
} else {
// 更新分类名称和描述
sql = "update book_sort set name=?, description=? where id=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, description);
pstmt.setString(3, id);
result = pstmt.executeUpdate();
if(result == 1) {
// 根据更新结果返回消息
if (result == 1) {
code = 0;
msg = "修改成功";
}else {
msg = "修改失败";
msg = "修改成功"; // 修改成功
} else {
msg = "修改失败"; // 修改失败
}
}
}
} catch (ClassNotFoundException e) {
msg = "classnotfound";
msg = "classnotfound"; // 类未找到错误
} catch (SQLException e) {
msg = "SQL错误";
msg = "SQL 错误"; // SQL 错误
} finally {
// 关闭数据库资源
try {
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
msg = "关闭失败";
msg = "关闭失败"; // 资源关闭失败
}
}
// 输出 JSON 格式的响应结果
PrintWriter out = resp.getWriter();
out.print(Util.jsonResponse(code, msg, null));
out.print(Util.jsonResponse(code, msg, null)); // 返回 JSON 响应
}
}

@ -18,57 +18,90 @@ import javabean.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet
*
* Servlet JSON
*/
@WebServlet("/admin/sortList")
public class SortList extends HttpServlet {
/**
* GET JSON
*
* JSON
*
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应类型为 JSON
resp.setContentType("application/json; charset=utf8");
// 接受参数
// 准备参数
// 准备据库操作相关变量
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
// 返回参数
int code = 1;
String msg = "error";
int count = 0;
JSONObject jsonObject = new JSONObject(); // 单个分类信息的 JSON 对象
JSONArray jsonArray = new JSONArray(); // 分类列表的 JSON 数组
// 初始化返回状态和消息
int code = 1; // 默认为失败
String msg = "error"; // 默认为错误消息
String sql = "";
PrintWriter out = resp.getWriter();
// 开始查询
PrintWriter out = resp.getWriter(); // 获取响应输出流
// 开始查询数据库
try {
// 获取数据库连接
connection = Base.getConnection();
// 执行 SQL 查询,查询所有书籍分类信息
sql = "select * from book_sort";
pstmt = connection.prepareStatement(sql);
resultSet = pstmt.executeQuery();
// 遍历查询结果并将其添加到 JSON 数组中
while (resultSet.next()) {
// 获取每个分类的字段值并将其添加到 JSON 对象中
jsonObject.put("id", resultSet.getString("id"));
jsonObject.put("name", resultSet.getString("name"));
jsonObject.put("description", resultSet.getString("description"));
// 将 JSON 对象添加到 JSON 数组中
jsonArray.add(jsonObject.toString());
}
// 根据查询结果返回相应的消息
if (!jsonArray.isEmpty()) {
code = 0;
code = 0; // 表示成功
msg = "查询成功";
} else {
msg = "数据为空";
msg = "数据为空"; // 如果没有数据,返回提示信息
}
} catch (ClassNotFoundException e) {
// 捕获 ClassNotFoundException 错误
msg = "没找到";
e.printStackTrace();
} catch (SQLException e) {
msg = "sql错误";
// 捕获 SQL 错误
msg = "sql 错误";
} finally {
// 关闭数据库资源
try {
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
msg = "关闭失败";
}
}
out.print(Util.jsonResponse(code, msg, jsonArray.toString()));
// 输出 JSON 格式的响应结果
out.print(Util.jsonResponse(code, msg, jsonArray.toString()));
}
}

@ -18,81 +18,109 @@ import javabean.Base;
import javabean.Util;
import net.sf.json.JSONObject;
/**
* Servlet
*
* Servlet
*
*/
@WebServlet("/admin/updatePassword")
public class UpdatePassword extends HttpServlet {
/**
* POST
*
*
*
* @param req
* @param resp
* @throws ServletException Servlet
* @throws IOException I/O
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应类型为 JSON
resp.setContentType("application/json; charset=utf8");
// 接受数据
// 接收请求参数
String oldPassword = req.getParameter("oldPassword");
String newPassword = req.getParameter("newPassword");
String conPassword = req.getParameter("conPassword");
// 获取当前登录用户的用户名
HttpSession session = req.getSession();
String username = (String) session.getAttribute("admin");
// 准备资源
// 准备数据库操作的变量
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet resultSet = null;
int result = 0;
String sql = null;
int count = 0;
// 返回数据
int code = 1;
String msg = "error";
// 准备返回数据
int code = 1; // 默认为失败
String msg = "error"; // 默认为错误信息
JSONObject json = new JSONObject();
PrintWriter out = resp.getWriter();
// 可靠性
if(conPassword.equals(newPassword)) {
// 查询
// 检查新密码和确认密码是否一致
if (conPassword.equals(newPassword)) {
// 查询数据库验证原密码
try {
connection = Base.getConnection();
// 验证账号密码
// 验证原密码是否正确
sql = "select count(*) as count from admin where username=? and password=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, Util.passMd5(oldPassword));
pstmt.setString(2, Util.passMd5(oldPassword)); // 对原密码进行 MD5 加密
resultSet = pstmt.executeQuery();
while(resultSet.next()) {
// 获取查询结果
while (resultSet.next()) {
count = resultSet.getInt("count");
}
// 修改密码
// 密码正确
if(count >= 1) {
// 如果原密码正确,进行密码更新
if (count >= 1) {
// 更新密码
sql = "update admin set password=? where username=?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, Util.passMd5(newPassword));
pstmt.setString(1, Util.passMd5(newPassword)); // 对新密码进行 MD5 加密
pstmt.setString(2, username);
result = pstmt.executeUpdate();
if(result == 1) {
code = 0;
// 判断密码是否更新成功
if (result == 1) {
code = 0; // 成功
msg = "修改成功";
}else {
} else {
msg = "修改失败";
}
}else {
msg = "密码错误";
} else {
msg = "密码错误"; // 原密码不正确
}
} catch (ClassNotFoundException e) {
msg = "class notfound";
msg = "class notfound"; // 类未找到错误
} catch (SQLException e) {
msg = "sql错误";
msg = "sql错误"; // SQL 错误
} finally {
// 关闭数据库资源
try {
Base.closeResource(connection, pstmt, resultSet);
} catch (SQLException e) {
msg = "关闭失败";
msg = "关闭失败"; // 关闭数据库资源失败
}
}
}else {
msg = "两次密码不一致";
} else {
msg = "两次密码不一致"; // 新密码和确认密码不一致
}
// 输出 JSON 格式的响应结果
out.print(Util.jsonResponse(code, msg, null));
}
}

Loading…
Cancel
Save