You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
library_manage_system/src/servlet/reader/Borrow.java

118 lines
5.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package servlet.reader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javabean.Base;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet implementation class Borrow
*/
@WebServlet("/reader/borrow")
public class Borrow extends HttpServlet {
@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 = ""; // 初始化查询条件字符串
if (page == null) {
page = "1"; // 默认第一页
}
if (limit == null) {
limit = "10"; // 默认每页显示10条记录
}
// 准备数据库连接和查询对象
Connection connection = null;
PreparedStatement pstmt = null;
PreparedStatement countPstmt = null;
ResultSet resultSet = null;
ResultSet countSet = null;
String sql = ""; // SQL查询语句
String countSql = ""; // 统计总数SQL查询语句
// 返回结果的参数
int code = 1; // 状态码1表示失败0表示成功
String msg = "无数据"; // 返回消息
int count = 0; // 总记录数
HttpSession session = req.getSession(); // 获取用户会话
JSONObject jsonData = new JSONObject(); // JSON对象存储单条记录数据
JSONArray jsonArray = new JSONArray(); // JSON数组存储多条记录数据
JSONObject jsonResult = new JSONObject(); // JSON对象存储最终返回结果
// 执行查询操作
try {
connection = (Connection) Base.getConnection(); // 获取数据库连接
sql = "select * from borrow_books where card_id = " + session.getAttribute("reader"); // 构建基本查询语句
if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) {
where = " and " + condition + " like '%" + conditionValue + "%' "; // 添加查询条件
sql += where; // 拼接完整查询语句
}
sql += " limit ?,?"; // 添加分页限制
System.out.println("???" + sql); // 打印SQL查询语句调试用
pstmt = connection.prepareStatement(sql); // 预编译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")); // 获取并存储记录ID
jsonData.put("card_id", resultSet.getString("card_id")); // 获取并存储读者ID
jsonData.put("book_id", resultSet.getString("book_id")); // 获取并存储书籍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); // 将记录添加到JSON数组中
}
countSql = "select count(*) as count from borrow_books where card_id = " + req.getSession().getAttribute("reader"); // 构建统计总数SQL查询语句
countSql += where; // 拼接查询条件
countPstmt = connection.prepareStatement(countSql); // 预编译统计总数SQL查询语句
countSet = countPstmt.executeQuery(); // 执行查询,获取结果集
if (countSet.next()) {
count = countSet.getInt("count"); // 获取总记录数
}
if (!jsonArray.isEmpty()) { // 如果JSON数组不为空表示有数据
code = 0; // 设置状态码为0表示成功
msg = "查询成功"; // 设置返回消息为“查询成功”
}
} catch (ClassNotFoundException e) { // 捕获类未找到异常
msg = "class没找到"; // 设置返回消息为“class没找到”
} catch (SQLException e) { // 捕获SQL异常
msg = "sql错误"; // 设置返回消息为“sql错误”
} finally { // 最终块,用于关闭资源
try {
Base.closeResource(null, pstmt, resultSet); // 关闭查询语句和结果集
Base.closeResource(connection, countPstmt, countSet); // 关闭数据库连接和统计结果集
} catch (SQLException e) { // 捕获SQL异常
msg = "关闭资源失败"; // 设置返回消息为“关闭资源失败”
}
}
// 将结果封装成JSON对象并输出到响应中
jsonResult.put("code", code); // 添加状态码到JSON对象中
jsonResult.put("count", count); // 添加总记录数到JSON对象中
jsonResult.put("msg", msg); // 添加返回消息到JSON对象中
jsonResult.put("data", jsonArray.toArray()); // 添加数据数组到JSON对象中
PrintWriter out = resp.getWriter(); // 获取响应输出流
out.print(jsonResult.toString()); // 输出JSON结果到响应中
}
}