pull/7/head
HeJiaYan 5 months ago
parent 335493cd8f
commit 86bb2c736a

@ -3,81 +3,84 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <html>
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title></title> <title></title>
<!-- Bootstrap --> <!-- 引入Bootstrap CSS -->
<link href="../css/bootstrap.min.css" rel="stylesheet"> <link href="../css/bootstrap.min.css" rel="stylesheet">
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery所以必须放在前边) --> <!-- 引入jQueryBootstrap的JavaScript插件依赖于jQuery -->
<script src="../js/jquery.min.js"></script> <script src="../js/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script> <!-- 引入Bootstrap JavaScript -->
<style> <script src="../js/bootstrap.min.js"></script>
.showCarousel .carousel-inner > .item > img {
<style>
/* 轮播图图片样式 */
.showCarousel .carousel-inner > .item > img {
display: block; display: block;
width:100%; width:100%;
height:620px; height:620px;
} }
.showCarousel{ /* 轮播图顶部间距 */
.showCarousel{
margin-top:2%; margin-top:2%;
} }
font{ /* 字体样式 */
font{
font-size:40px; font-size:40px;
color:black; color:black;
font-family:YouYuan; font-family:YouYuan;
font-weight:900; font-weight:900;
} }
.carousel-caption{ /* 轮播图文字描述底部间距 */
.carousel-caption{
margin-bottom:10%; margin-bottom:10%;
} }
</style> </style>
</head> </head>
<body> <body>
<% <%
if(session.getAttribute("reader")!=null && session.getAttribute("reader_first")!=null &&session.getAttribute("reader_first").equals("1")){ // 检查会话属性 'reader' 和 'reader_first' 是否不为空且 'reader_first' 等于 "1"
if(session.getAttribute("reader") != null && session.getAttribute("reader_first") != null && session.getAttribute("reader_first").equals("1")){
// 将 'reader_first' 设置为 "2",表示用户已经访问过
session.setAttribute("reader_first", "2"); session.setAttribute("reader_first", "2");
//session.setAttribute("reader", session.getAttribute("reader")); // 可选地,可以在会话中保留 reader 属性
// session.setAttribute("reader", session.getAttribute("reader"));
%>
<script>window.parent.location.href = "./04readerFrame.jsp";</script>
<%
} }
%> %>
<!-- 轮播组件 -->
<div id="carousel-example-generic" class="carousel slide showCarousel" data-ride="carousel" data-interval="2000" style="width:96%;margin-left:2%;"> <div id="carousel-example-generic" class="carousel slide showCarousel" data-ride="carousel" data-interval="2000" style="width:96%;margin-left:2%;">
<!-- Indicators --> <!-- 指示器 -->
<ol class="carousel-indicators"> <ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li> <li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li> <li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol> </ol>
<!-- Wrapper for slides --> <!-- 轮播内容 -->
<div class="carousel-inner" role="listbox"> <div class="carousel-inner" role="listbox">
<div class="item active"> <div class="item active">
<img src="../public/image/1.jpg" alt="..."> <img src="path/to/image1.jpg" alt="..."> <!-- 在这里添加你的图片路径 -->
<div class="carousel-caption"> <div class="carousel-caption">
<font style="color:white;">唯有知识永不负你!</font> <font style="color:white;">唯有知识永不负你!</font>
</div> </div>
</div> </div>
<div class="item"> <div class="item">
<img src="../public/image/2.jpg" alt="..."> <img src="path/to/image2.jpg" alt="..."> <!-- 在这里添加你的图片路径 -->
<div class="carousel-caption"> <div class="carousel-caption">
<font style="color:white;">学海无涯,书韵悠扬</font> <font style="color:white;">学海无涯,书韵悠扬</font>
</div> </div>
</div> </div>
<div class="item"> <div class="item">
<img src="../public/image/3.jpg" alt="..."> <img src="path/to/image3.jpg" alt="..."> <!-- 在这里添加你的图片路径 -->
<div class="carousel-caption"> <div class="carousel-caption">
<font style="color:white;">图一份神闲气静,书几笔悦目赏心</font> <font style="color:white;">图一份神闲气静,书几笔悦目赏心</font>
</div> </div>
</div> </div>
</div> </div>
<!-- Controls --> <!-- 控制按钮 -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev"> <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span> <span class="sr-only">Previous</span>

@ -3,59 +3,62 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <html>
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title></title> <title></title>
<!-- Bootstrap --> <!-- 引入Bootstrap的CSS文件 -->
<link href="../css/bootstrap.min.css" rel="stylesheet"> <link href="../css/bootstrap.min.css" rel="stylesheet">
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery所以必须放在前边) --> <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery所以必须放在前边) -->
<script src="../js/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<style>
</style> <style>
</style>
</head> </head>
<body> <body>
<nav class="navbar navbar-inverse" role="navigation"> <!-- 导航栏 -->
<nav class="navbar navbar-inverse" role="navigation">
<div class="container-fluid"> <div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display --> <!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header"> <div class="navbar-header">
<!-- 折叠按钮,用于在小屏幕设备上显示菜单 -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span> <span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
</button> </button>
<!-- 网站标题 -->
<a class="navbar-brand" href="#">欢迎登陆图书管理系统</a> <a class="navbar-brand" href="#">欢迎登陆图书管理系统</a>
</div> </div>
<!-- Collect the nav links, forms, and other content for toggling --> <!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<!-- 左侧导航链接 -->
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li><p style="margin-top:25%;color:grey;">借阅者页面<span class="sr-only">(current)</span></p></li> <li><p style="margin-top:25%;color:grey;">借阅者页面<span class="sr-only">(current)</span></p></li>
</ul> </ul>
<!-- 右侧导航链接 -->
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
<li><a href="javascript:;"> <li><a href=";">
<!-- 如果session中没有reader属性则不显示任何内容否则显示reader的值 -->
<%if(session.getAttribute("reader")==null){ <%if(session.getAttribute("reader")==null){
// 空操作
} else{%> } else{%>
<%=session.getAttribute("reader") %> <%=session.getAttribute("reader") %>
<%} %> <%} %>
</a></li> </a></li>
<!-- 如果session中有reader属性则显示退出链接否则显示登录链接 -->
<%if(session.getAttribute("reader") !=null) {%> <%if(session.getAttribute("reader") !=null) {%>
<li><a href="./exit" target="_parent">退出</a></li> <li><a href="./exit" target="_parent">退出</a></li>
<%}else{%> <%}else{%>
<li><a href="04readerFrame.jsp" target="_parent" onClick="">登录</a></li> <li><a href="04readerFrame.jsp" target="_parent" onClick="">登录</a></li>
<%} <%}
%> %>
</ul> </ul>
</div> </div>
</div> </div>
</nav> </nav>
</body> </body>
</html> </html>

@ -8,24 +8,26 @@ import java.sql.SQLException;
public class Reader { public class Reader {
@SuppressWarnings("null") @SuppressWarnings("null")
public String login(String user, String psw) throws ClassNotFoundException, SQLException { public String login(String user, String psw) throws ClassNotFoundException, SQLException {
//检查用户名是否为空
if (user == null || user.trim().equals("")) { if (user == null || user.trim().equals("")) {
return "账号不能为空"; return "账号不能为空";
} else if (psw == null || psw.trim().equals("")) { } else if (psw == null || psw.trim().equals("")) {//检查密码是否为空
return "密码不能为空"; return "密码不能为空";
} }
Connection connection = null; Connection connection = null; // 数据库连接对象
PreparedStatement pstmt = null; PreparedStatement pstmt = null; // 预编译的SQL语句对象
ResultSet resultSet = null; ResultSet resultSet = null; // 查询结果集对象
String sql = "select * from borrow_card where ID=? and PASSWORD=?"; String sql = "select * from borrow_card where ID=? and PASSWORD=?"; // SQL查询语句
connection = Base.getConnection();
pstmt = (PreparedStatement) connection.prepareStatement(sql); connection = Base.getConnection(); // 获取数据库连接
pstmt.setString(1, user); pstmt = (PreparedStatement) connection.prepareStatement(sql); // 创建PreparedStatement对象
pstmt.setString(2, psw); pstmt.setString(1, user); // 设置第一个参数(用户名)
resultSet = pstmt.executeQuery(); pstmt.setString(2, psw); // 设置第二个参数(密码)
if (resultSet.next()) { resultSet = pstmt.executeQuery(); // 执行查询
if (resultSet.next()) { // 如果查询有结果,说明账号密码正确
return "1"; return "1";
} }
return "账号或密码错误"; return "账号或密码错误"; // 如果没有查询到结果,返回错误信息
} }
} }

@ -24,51 +24,70 @@ import net.sf.json.JSONObject;
public class Book extends HttpServlet { public class Book extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为JSON字符编码为UTF-8
resp.setContentType("application/json; charset=utf8"); resp.setContentType("application/json; charset=utf8");
// 接收参数
String limit = req.getParameter("limit"); // 接收请求参数
String page = req.getParameter("page"); String limit = req.getParameter("limit"); // 每页显示的记录数
String condition = (String) req.getParameter("condition"); String page = req.getParameter("page"); // 当前页码
String conditionValue = (String) req.getParameter("conditionValue"); String condition = (String) req.getParameter("condition"); // 查询条件字段
String where = ""; // 无限制条件 String conditionValue = (String) req.getParameter("conditionValue"); // 查询条件值
String where = ""; // SQL查询条件字符串
// 默认分页参数
if (page == null) { if (page == null) {
page = "1"; page = "1";
} }
if (limit == null) { if (limit == null) {
limit = "10"; limit = "10";
} }
// 准备查询
// 数据库连接和SQL语句准备
Connection connection = null; Connection connection = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
PreparedStatement countPstmt = null;//HE PreparedStatement countPstmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
ResultSet countSet = null; ResultSet countSet = null;
String sql = ""; String sql = "";
String countSql = ""; String countSql = "";
// 准备返回参数
int code = 1;
String msg = "无数据";
int count = 0;
JSONObject jsonData = new JSONObject(); // 返回数据结构初始化
JSONArray jsonArray = new JSONArray(); int code = 1; // 状态码1表示失败0表示成功
JSONObject jsonResult = new JSONObject(); String msg = "无数据"; // 消息提示
// 进行查询 int count = 0; // 总记录数
JSONObject jsonData = new JSONObject(); // 单条记录的JSON对象
JSONArray jsonArray = new JSONArray(); // 所有记录的JSON数组
JSONObject jsonResult = new JSONObject(); // 最终返回的JSON对象
try { try {
// 获取数据库连接
connection = (Connection) Base.getConnection(); connection = (Connection) Base.getConnection();
sql = "select * from books "; sql = "select * from books ";
// 根据条件拼接SQL查询语句
if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) { if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) {
where = " where " + condition + " like '%" + conditionValue + "%' "; where = " where " + condition + " like '%" + conditionValue + "%' ";
sql += where; sql += where;
} }
sql += " limit ?,?";// 1 10 (1-1)*10 // 分页查询
sql += " limit ?,?"; // 计算偏移量和限制数量
pstmt = connection.prepareStatement(sql); pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit)); pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit));
pstmt.setInt(2, Integer.parseInt(limit)); pstmt.setInt(2, Integer.parseInt(limit));
resultSet = pstmt.executeQuery(); resultSet = pstmt.executeQuery();
while (resultSet.next()) {
// 处理查询结果集
while (resultSet.next()) {
// 获取图书信息
String library = resultSet.getString("library_id"); String library = resultSet.getString("library_id");
String sortid = resultSet.getString("sort_id");
jsonData.put("id", resultSet.getString("id"));
jsonData.put("name", resultSet.getString("name"));
jsonData.put("author", resultSet.getString("author"));
jsonData.put("position", resultSet.getString("position"));
jsonData.put("status", resultSet.getString("status"));
jsonData.put("description", resultSet.getString("description"));
// 获取图书馆名称
String sql1 = "select * from library where ID =" + library; String sql1 = "select * from library where ID =" + library;
PreparedStatement pstmt1 = connection.prepareStatement(sql1); PreparedStatement pstmt1 = connection.prepareStatement(sql1);
ResultSet rs1 = pstmt1.executeQuery(); ResultSet rs1 = pstmt1.executeQuery();
@ -76,8 +95,9 @@ public class Book extends HttpServlet {
while (rs1.next()) { while (rs1.next()) {
lib = rs1.getString("name"); lib = rs1.getString("name");
} }
jsonData.put("library_id", lib);
String sortid = resultSet.getString("sort_id"); // 获取分类名称
String sql2 = "select * from book_sort where ID =" + sortid; String sql2 = "select * from book_sort where ID =" + sortid;
PreparedStatement pstmt2 = connection.prepareStatement(sql2); PreparedStatement pstmt2 = connection.prepareStatement(sql2);
ResultSet rs2 = pstmt2.executeQuery(); ResultSet rs2 = pstmt2.executeQuery();
@ -85,17 +105,12 @@ public class Book extends HttpServlet {
while (rs2.next()) { while (rs2.next()) {
sort = rs2.getString("name"); sort = rs2.getString("name");
} }
jsonData.put("id", resultSet.getString("id"));
jsonData.put("name", resultSet.getString("name"));
jsonData.put("author", resultSet.getString("author"));
jsonData.put("library_id", lib);
jsonData.put("sort_id", sort); jsonData.put("sort_id", sort);
jsonData.put("position", resultSet.getString("position"));
jsonData.put("status", resultSet.getString("status")); // 将单条记录添加到JSON数组中
jsonData.put("description", resultSet.getString("description"));
jsonArray.add(jsonData); jsonArray.add(jsonData);
} }
// 统计总记录数
countSql = "select count(*) as count from books "; countSql = "select count(*) as count from books ";
countSql += where; countSql += where;
countPstmt = connection.prepareStatement(countSql); countPstmt = connection.prepareStatement(countSql);
@ -104,28 +119,28 @@ public class Book extends HttpServlet {
count = countSet.getInt("count"); count = countSet.getInt("count");
} }
if (!jsonArray.isEmpty()) { if (!jsonArray.isEmpty()) {
code = 0; code = 0; // 查询成功
msg = "查询成功"; msg = "查询成功";
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
msg = "class没找到"; msg = "class没找到"; // 类未找到异常处理
} catch (SQLException e) { } catch (SQLException e) {
msg = "sql错误"; msg = "sql错误"; // SQL异常处理
} finally { } finally {
// 关闭资源
try { try {
Base.closeResource(null, pstmt, resultSet); Base.closeResource(null, pstmt, resultSet);
Base.closeResource(connection, countPstmt, countSet); Base.closeResource(connection, countPstmt, countSet);
} catch (SQLException e) { } catch (SQLException e) {
msg = "关闭资源失败"; msg = "关闭资源失败"; // 关闭资源异常处理
} }
} }
// 返回数据 // 构建返回的JSON对象
jsonResult.put("code", code); jsonResult.put("code", code);
jsonResult.put("count", count); jsonResult.put("count", count);
jsonResult.put("msg", msg); jsonResult.put("msg", msg);
jsonResult.put("data", jsonArray.toArray()); jsonResult.put("data", jsonArray.toArray());
// 输出JSON响应
PrintWriter out = resp.getWriter(); PrintWriter out = resp.getWriter();
out.print(jsonResult.toString()); out.print(jsonResult.toString());
} }

@ -25,93 +25,93 @@ import net.sf.json.JSONObject;
public class Borrow extends HttpServlet { public class Borrow extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置响应内容类型为JSON编码格式为UTF-8
resp.setContentType("application/json; charset=utf8"); resp.setContentType("application/json; charset=utf8");
// 接收参数 // 接收分页参数
String limit = req.getParameter("limit"); String limit = req.getParameter("limit"); // 每页显示记录数
String page = req.getParameter("page"); String page = req.getParameter("page"); // 当前页码
String condition = (String) req.getParameter("condition"); String condition = (String) req.getParameter("condition"); // 查询条件字段名
String conditionValue = (String) req.getParameter("conditionValue"); String conditionValue = (String) req.getParameter("conditionValue"); // 查询条件值
String where = ""; // 无限制条件 String where = ""; // 初始化查询条件字符串
if (page == null) { if (page == null) {
page = "1"; page = "1"; // 默认第一页
} }
if (limit == null) { if (limit == null) {
limit = "10"; limit = "10"; // 默认每页显示10条记录
} }
// 准备查询 // 准备数据库连接和查询对象
Connection connection = null; Connection connection = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
PreparedStatement countPstmt = null; PreparedStatement countPstmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
ResultSet countSet = null; ResultSet countSet = null;
String sql = ""; String sql = ""; // SQL查询语句
String countSql = ""; String countSql = ""; // 统计总数SQL查询语句
// 准备返回参数 // 返回结果的参数
int code = 1; int code = 1; // 状态码1表示失败0表示成功
String msg = "无数据"; String msg = "无数据"; // 返回消息
int count = 0; int count = 0; // 总记录数
HttpSession session = req.getSession(); HttpSession session = req.getSession(); // 获取用户会话
JSONObject jsonData = new JSONObject(); JSONObject jsonData = new JSONObject(); // JSON对象存储单条记录数据
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray(); // JSON数组存储多条记录数据
JSONObject jsonResult = new JSONObject(); JSONObject jsonResult = new JSONObject(); // JSON对象存储最终返回结果
// 进行查询 // 执行查询操作
try { try {
connection = (Connection) Base.getConnection(); connection = (Connection) Base.getConnection(); // 获取数据库连接
sql = "select * from borrow_books where card_id = " + session.getAttribute("reader"); sql = "select * from borrow_books where card_id = " + session.getAttribute("reader"); // 构建基本查询语句
if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) { if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) {
where = " and " + condition + " like '%" + conditionValue + "%' "; where = " and " + condition + " like '%" + conditionValue + "%' "; // 添加查询条件
sql += where; sql += where; // 拼接完整查询语句
} }
sql += " limit ?,?";// 1 10 (1-1)*10 sql += " limit ?,?"; // 添加分页限制
System.out.println("???" + sql); System.out.println("???" + sql); // 打印SQL查询语句调试用
pstmt = connection.prepareStatement(sql); pstmt = connection.prepareStatement(sql); // 预编译SQL查询语句
pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit)); pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit)); // 设置第一个参数:起始位置
pstmt.setInt(2, Integer.parseInt(limit)); pstmt.setInt(2, Integer.parseInt(limit)); // 设置第二个参数:每页显示记录数
resultSet = pstmt.executeQuery(); resultSet = pstmt.executeQuery(); // 执行查询,获取结果集
while (resultSet.next()) { while (resultSet.next()) {
jsonData.put("id", resultSet.getString("id")); jsonData.put("id", resultSet.getString("id")); // 获取并存储记录ID
jsonData.put("card_id", resultSet.getString("card_id")); jsonData.put("card_id", resultSet.getString("card_id")); // 获取并存储读者ID
jsonData.put("book_id", resultSet.getString("book_id")); jsonData.put("book_id", resultSet.getString("book_id")); // 获取并存储书籍ID
jsonData.put("borrow_date", resultSet.getString("borrow_date")); jsonData.put("borrow_date", resultSet.getString("borrow_date")); // 获取并存储借阅日期
jsonData.put("end_date", resultSet.getString("end_date")); jsonData.put("end_date", resultSet.getString("end_date")); // 获取并存储到期日期
jsonData.put("return_date", resultSet.getString("return_date")); jsonData.put("return_date", resultSet.getString("return_date")); // 获取并存储归还日期
jsonArray.add(jsonData); jsonArray.add(jsonData); // 将记录添加到JSON数组中
} }
countSql = "select count(*) as count from borrow_books where card_id = " countSql = "select count(*) as count from borrow_books where card_id = " + req.getSession().getAttribute("reader"); // 构建统计总数SQL查询语句
+ req.getSession().getAttribute("reader"); countSql += where; // 拼接查询条件
countSql += where; countPstmt = connection.prepareStatement(countSql); // 预编译统计总数SQL查询语句
countPstmt = connection.prepareStatement(countSql); countSet = countPstmt.executeQuery(); // 执行查询,获取结果集
countSet = countPstmt.executeQuery();
if (countSet.next()) { if (countSet.next()) {
count = countSet.getInt("count"); count = countSet.getInt("count"); // 获取总记录数
} }
if (!jsonArray.isEmpty()) { if (!jsonArray.isEmpty()) { // 如果JSON数组不为空表示有数据
code = 0; code = 0; // 设置状态码为0表示成功
msg = "查询成功"; msg = "查询成功"; // 设置返回消息为“查询成功”
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) { // 捕获类未找到异常
msg = "class没找到"; msg = "class没找到"; // 设置返回消息为“class没找到”
} catch (SQLException e) { } catch (SQLException e) { // 捕获SQL异常
msg = "sql错误"; msg = "sql错误"; // 设置返回消息为“sql错误”
} finally { } finally { // 最终块,用于关闭资源
try { try {
Base.closeResource(null, pstmt, resultSet); Base.closeResource(null, pstmt, resultSet); // 关闭查询语句和结果集
Base.closeResource(connection, countPstmt, countSet); Base.closeResource(connection, countPstmt, countSet); // 关闭数据库连接和统计结果集
} catch (SQLException e) { } catch (SQLException e) { // 捕获SQL异常
msg = "关闭资源失败"; msg = "关闭资源失败"; // 设置返回消息为“关闭资源失败”
} }
} }
// 返回数据 // 将结果封装成JSON对象并输出到响应中
jsonResult.put("code", code); jsonResult.put("code", code); // 添加状态码到JSON对象中
jsonResult.put("count", count); jsonResult.put("count", count); // 添加总记录数到JSON对象中
jsonResult.put("msg", msg); jsonResult.put("msg", msg); // 添加返回消息到JSON对象中
jsonResult.put("data", jsonArray.toArray()); jsonResult.put("data", jsonArray.toArray()); // 添加数据数组到JSON对象中
PrintWriter out = resp.getWriter(); PrintWriter out = resp.getWriter(); // 获取响应输出流
out.print(jsonResult.toString()); out.print(jsonResult.toString()); // 输出JSON结果到响应中
} }
} }

@ -19,11 +19,14 @@ public class Exit extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub // TODO Auto-generated method stub
// 获取当前请求的会话对象。如果会话不存在,则创建一个新的会话。
HttpSession session = req.getSession(); HttpSession session = req.getSession();
// 检查会话中是否存在名为 "reader" 的属性。
if (session.getAttribute("reader") != null) { if (session.getAttribute("reader") != null) {
// 如果存在,则移除该属性。
session.removeAttribute("reader"); session.removeAttribute("reader");
} }
// 重定向到指定的 JSP 页面。
resp.sendRedirect(req.getContextPath() + "/reader/04readerFrame.jsp"); resp.sendRedirect(req.getContextPath() + "/reader/04readerFrame.jsp");
} }
} }

@ -25,52 +25,66 @@ import net.sf.json.JSONObject;
public class Illegal extends HttpServlet { public class Illegal extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容类型为 JSON并指定字符编码为 UTF-8
resp.setContentType("application/json; charset=utf8"); resp.setContentType("application/json; charset=utf8");
// 接收参数
String limit = req.getParameter("limit"); // 接收请求参数
String page = req.getParameter("page"); String limit = req.getParameter("limit"); // 每页显示记录数
String condition = (String) req.getParameter("condition"); String page = req.getParameter("page"); // 当前页码
String conditionValue = (String) req.getParameter("conditionValue"); String condition = (String) req.getParameter("condition"); // 查询条件字段
String where = ""; // 无限制条件 String conditionValue = (String) req.getParameter("conditionValue"); // 查询条件值
String where = ""; // 初始化查询条件字符串
// 如果未提供分页参数,则使用默认值
if (page == null) { if (page == null) {
page = "1"; page = "1"; // 默认第一页
} }
if (limit == null) { if (limit == null) {
limit = "10"; limit = "10"; // 默认每页显示10条记录
} }
// 准备查询
// 准备数据库连接和查询对象
Connection connection = null; Connection connection = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
PreparedStatement countPstmt = null; PreparedStatement countPstmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
ResultSet countSet = null; ResultSet countSet = null;
String sql = ""; String sql = ""; // SQL 查询语句
String countSql = ""; String countSql = ""; // 统计总记录数的 SQL 查询语句
// 准备返回参数
int code = 1; // 准备返回结果的参数
String msg = "无数据"; int code = 1; // 状态码1表示失败0表示成功
int count = 0; String msg = "无数据"; // 返回消息
HttpSession session = req.getSession(); int count = 0; // 总记录数
HttpSession session = req.getSession(); // 获取会话对象
// 用于存储查询结果的 JSON 对象和数组
JSONObject jsonData = new JSONObject(); JSONObject jsonData = new JSONObject();
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
JSONObject jsonResult = new JSONObject(); JSONObject jsonResult = new JSONObject();
// 进行查询
// 进行数据库查询操作
try { try {
// 获取数据库连接
connection = (Connection) Base.getConnection(); connection = (Connection) Base.getConnection();
sql = "select * from borrow_books where ILLEGAL is not null and length(trim(illegal))>0 AND CARD_ID = " // 构建 SQL 查询语句,查询借阅记录中存在违规信息的记录
+ session.getAttribute("reader"); sql = "select * from borrow_books where ILLEGAL is not null and length(trim(illegal))>0 AND CARD_ID = " + session.getAttribute("reader");
// 如果提供了查询条件,则添加到 SQL 查询语句中
if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) { if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) {
where = " and " + condition + " like '%" + conditionValue + "%' "; where = " and " + condition + " like '%" + conditionValue + "%' ";
sql += where; sql += where;
} }
sql += " limit ?,?";// 1 10 (1-1)*10 // 添加分页限制条件
System.out.println("???" + sql); sql += " limit ?,?"; // 例如limit 1,10 (1-1)*10
System.out.println("???" + sql); // 打印 SQL 查询语句(调试用)
pstmt = connection.prepareStatement(sql); pstmt = connection.prepareStatement(sql);
// 设置分页参数,计算起始位置和每页记录数
pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit)); pstmt.setInt(1, (Integer.parseInt(page) - 1) * Integer.parseInt(limit));
pstmt.setInt(2, Integer.parseInt(limit)); pstmt.setInt(2, Integer.parseInt(limit));
// 执行查询并处理结果集
resultSet = pstmt.executeQuery(); resultSet = pstmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
// 将查询结果集中的每一行数据存入 JSON 对象中,并添加到 JSON 数组中
jsonData.put("id", resultSet.getString("id")); jsonData.put("id", resultSet.getString("id"));
jsonData.put("card_id", resultSet.getString("card_id")); jsonData.put("card_id", resultSet.getString("card_id"));
jsonData.put("book_id", resultSet.getString("book_id")); jsonData.put("book_id", resultSet.getString("book_id"));
@ -81,39 +95,39 @@ public class Illegal extends HttpServlet {
jsonData.put("manager_id", resultSet.getString("manager_id")); jsonData.put("manager_id", resultSet.getString("manager_id"));
jsonArray.add(jsonData); jsonArray.add(jsonData);
} }
countSql = "select count(*) as count from borrow_books where ILLEGAL is not null and length(trim(illegal))>0 AND CARD_ID = " // 构建统计总记录数的 SQL 查询语句,并执行查询以获取总记录数
+ session.getAttribute("reader"); 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; countSql += where;
countPstmt = connection.prepareStatement(countSql); countPstmt = connection.prepareStatement(countSql);
countSet = countPstmt.executeQuery(); countSet = countPstmt.executeQuery();
if (countSet.next()) { if (countSet.next()) {
count = countSet.getInt("count"); count = countSet.getInt("count"); // 获取总记录数
} }
// 如果查询结果不为空,则更新状态码和消息
if (!jsonArray.isEmpty()) { if (!jsonArray.isEmpty()) {
code = 0; code = 0; // 查询成功
msg = "查询成功"; msg = "查询成功";
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
msg = "class没找到"; msg = "class没找到"; // 捕获类未找到异常并设置错误消息
} catch (SQLException e) { } catch (SQLException e) {
msg = "sql错误"; msg = "sql错误"; // 捕获 SQL 异常并设置错误消息
} finally { } finally {
// 关闭数据库资源
try { try {
Base.closeResource(null, pstmt, resultSet); Base.closeResource(null, pstmt, resultSet);
Base.closeResource(connection, countPstmt, countSet); Base.closeResource(connection, countPstmt, countSet);
} catch (SQLException e) { } catch (SQLException e) {
msg = "关闭资源失败"; msg = "关闭资源失败"; // 捕获关闭资源时的异常并设置错误消息
} }
} }
// 返回数据 // 将查询结果封装到 JSON 对象中并返回给客户端
jsonResult.put("code", code); jsonResult.put("code", code); // 状态码
jsonResult.put("count", count); jsonResult.put("count", count); // 总记录数
jsonResult.put("msg", msg); jsonResult.put("msg", msg); // 返回消息
jsonResult.put("data", jsonArray.toArray()); jsonResult.put("data", jsonArray.toArray()); // 查询结果数据
PrintWriter out = resp.getWriter(); PrintWriter out = resp.getWriter(); // 获取输出流对象
out.print(jsonResult.toString()); out.print(jsonResult.toString()); // 输出 JSON 字符串作为响应
} }
} }

@ -25,15 +25,18 @@ public class ReaderLogin extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
// 设置头文件 // 设置响应内容类型和字符编码
response.setContentType("application/json; charset=utf8"); response.setContentType("application/json; charset=utf8");
PrintWriter out = response.getWriter(); PrintWriter out = response.getWriter();
// 获取账号密码
// 获取请求参数中的账号和密码
String user = request.getParameter("user"); String user = request.getParameter("user");
String psw = request.getParameter("psw"); String psw = request.getParameter("psw");
// 设置响应map
// 创建一个HashMap用于存储响应数据
HashMap<String, Object> hashMap = new HashMap<String, Object>(); HashMap<String, Object> hashMap = new HashMap<String, Object>();
// 创建Reader对象并调用其login方法进行登录验证
Reader reader = new Reader(); Reader reader = new Reader();
String result = null; String result = null;
try { try {
@ -41,18 +44,23 @@ public class ReaderLogin extends HttpServlet {
} catch (ClassNotFoundException | SQLException e) { } catch (ClassNotFoundException | SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
// 根据登录结果设置响应数据
if (result != null && result.equals("1")) { if (result != null && result.equals("1")) {
// 登录成功,创建会话并设置相关属性
HttpSession session = request.getSession(); HttpSession session = request.getSession();
session.setAttribute("reader", user); session.setAttribute("reader", user);
session.setAttribute("reader_first", "1"); // 登录 session.setAttribute("reader_first", "1"); // 标记为首次登录
hashMap.put("code", 0); hashMap.put("code", 0); // 状态码0表示成功
hashMap.put("msg", "登录成功"); hashMap.put("msg", "登录成功"); // 提示信息
hashMap.put("url", request.getContextPath() + "/reader/01main.jsp"); hashMap.put("url", request.getContextPath() + "/reader/01main.jsp"); // 跳转页面
} else { } else {
hashMap.put("code", 1); // 登录失败,设置错误信息
hashMap.put("msg", result); hashMap.put("code", 1); // 状态码1表示失败
hashMap.put("msg", result); // 错误信息
} }
// response.sendRedirect(request.getContextPath() +"/test.jsp");
// 将响应数据转换为JSON格式并输出
JSONObject json = JSONObject.fromObject(hashMap); JSONObject json = JSONObject.fromObject(hashMap);
out.write(json.toString()); out.write(json.toString());

Loading…
Cancel
Save