@ -25,93 +25,93 @@ import net.sf.json.JSONObject;
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 = "" ; // 无限制条件
// 接收 分页 参数
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" ;
page = "1" ; // 默认第一页
}
if ( limit = = null ) {
limit = "10" ;
limit = "10" ; // 默认每页显示10条记录
}
// 准备 查询
// 准备 数据库连接和 查询对象
Connection connection = null ;
PreparedStatement pstmt = null ;
PreparedStatement countPstmt = null ;
ResultSet resultSet = null ;
ResultSet countSet = null ;
String sql = "" ;
String countSql = "" ;
// 准备 返回参数
int code = 1 ;
String msg = "无数据" ;
int count = 0 ;
String sql = "" ; // SQL查询语句
String countSql = "" ; // 统计总数SQL查询语句
// 返回结果的 参数
int code = 1 ; // 状态码, 1表示失败, 0表示成功
String msg = "无数据" ; // 返回消息
int count = 0 ; // 总记录数
HttpSession session = req . getSession ( ) ;
HttpSession session = req . getSession ( ) ; // 获取用户会话
JSONObject jsonData = new JSONObject ( ) ;
JSONArray jsonArray = new JSONArray ( ) ;
JSONObject jsonResult = new JSONObject ( ) ;
// 进行查询
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" ) ;
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 ;
where = " and " + condition + " like '%" + conditionValue + "%' " ; // 添加查询条件
sql + = where ; // 拼接完整查询语句
}
sql + = " limit ?,?" ; // 1 10 (1-1)*10
System . out . println ( "???" + sql ) ;
pstmt = connection . prepareStatement ( sql ) ;
pstmt . setInt ( 1 , ( Integer . parseInt ( page ) - 1 ) * Integer . parseInt ( limit ) ) ;
pstmt . setInt ( 2 , Integer . parseInt ( limit ) ) ;
resultSet = pstmt . executeQuery ( ) ;
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" ) ) ;
jsonData . put ( "card_id" , resultSet . getString ( "card_id" ) ) ;
jsonData . put ( "book_id" , resultSet . getString ( "book_id" ) ) ;
jsonData . put ( "borrow_date" , resultSet . getString ( "borrow_date" ) ) ;
jsonData . put ( "end_date" , resultSet . getString ( "end_date" ) ) ;
jsonData . put ( "return_date" , resultSet . getString ( "return_date" ) ) ;
jsonArray . add ( jsonData ) ;
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" ) ;
countSql + = where ;
countPstmt = connection . prepareStatement ( countSql ) ;
countSet = countPstmt . executeQuery ( ) ;
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" ) ;
count = countSet . getInt ( "count" ) ; // 获取总记录数
}
if ( ! jsonArray . isEmpty ( ) ) {
code = 0 ;
msg = "查询成功" ;
if ( ! jsonArray . isEmpty ( ) ) { // 如果JSON数组不为空, 表示有数据
code = 0 ; // 设置状态码为0, 表示成功
msg = "查询成功" ; // 设置返回消息为“查询成功”
}
} catch ( ClassNotFoundException e ) {
msg = "class没找到" ;
} catch ( SQLException e ) {
msg = "sql错误" ;
} finally {
} 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 ) {
msg = "关闭资源失败" ;
Base . closeResource ( null , pstmt , resultSet ) ; // 关闭查询语句和结果集
Base . closeResource ( connection , countPstmt , countSet ) ; // 关闭数据库连接和统计结果集
} catch ( SQLException e ) { // 捕获SQL异常
msg = "关闭资源失败" ; // 设置返回消息为“关闭资源失败”
}
}
// 返回数据
jsonResult . put ( "code" , code ) ;
jsonResult . put ( "count" , count ) ;
jsonResult . put ( "msg" , msg ) ;
jsonResult . put ( "data" , jsonArray . toArray ( ) ) ;
PrintWriter out = resp . getWriter ( ) ;
out . print ( jsonResult . toString ( ) ) ;
// 将结果封装成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结果到响应中
}
}