@ -20,94 +20,94 @@ import net.sf.json.JSONObject;
/ * *
* Servlet implementation class ReturnTable
* /
@WebServlet ( "/manager/returnTable" ) // 定义Servlet的URL映射
public class ReturnTable extends HttpServlet { // 继承HttpServlet类
@WebServlet ( "/manager/returnTable" )
public class ReturnTable extends HttpServlet {
@Override
protected void doGet ( HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException { // 重写doGet方法
resp . setContentType ( "application/json; charset=utf8" ) ; // 设置响应内容类型为JSON, 编码为UTF-8
protected void doGet ( HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
resp . setContentType ( "application/json; charset=utf8" ) ;
// 接收参数
String limit = req . getParameter ( "limit" ) ; // 获取请求中的limit参数
String page = req . getParameter ( "page" ) ; // 获取请求中的page参数
String condition = ( String ) req . getParameter ( "condition" ) ; // 获取请求中的condition参数
String conditionValue = ( String ) req . getParameter ( "conditionValue" ) ; // 获取请求中的conditionValue参数
String where = "" ; // 初始化where子句为空字符串
if ( page = = null ) { // 如果page参数为空
page = "1" ; // 默认设置为1
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参数为空
limit = "10" ; // 默认设置为10
if ( limit = = null ) {
limit = "10" ;
}
// 准备查询
Connection connection = null ; // 数据库连接对象
PreparedStatement pstmt = null ; // 预编译SQL语句对象
PreparedStatement countPstmt = null ; // 用于计数的预编译SQL语句对象
ResultSet resultSet = null ; // 结果集对象
ResultSet countSet = null ; // 计数结果集对象
String sql = "" ; // SQL查询语句
String countSql = "" ; // 计数SQL查询语句
Connection connection = null ;
PreparedStatement pstmt = null ;
PreparedStatement countPstmt = null ;
ResultSet resultSet = null ;
ResultSet countSet = null ;
String sql = "" ;
String countSql = "" ;
// 准备返回参数
int code = 1 ; // 返回代码, 默认为1表示失败
String msg = "无数据" ; // 返回消息,默认为"无数据"
int count = 0 ; // 总记录数
int code = 1 ;
String msg = "无数据" ;
int count = 0 ;
JSONObject jsonData = new JSONObject ( ) ; // JSON对象, 用于存储单条记录的数据
JSONArray jsonArray = new JSONArray ( ) ; // JSON数组, 用于存储所有记录的数据
JSONObject jsonResult = new JSONObject ( ) ; // JSON对象, 用于返回最终结果
JSONObject jsonData = new JSONObject ( ) ;
JSONArray jsonArray = new JSONArray ( ) ;
JSONObject jsonResult = new JSONObject ( ) ;
// 进行查询
try {
connection = ( Connection ) Base . getConnection ( ) ; // 获取数据库连接
sql = "select * from borrow_books where manager_id is null" ; // 基础查询语句
if ( condition ! = null & & conditionValue ! = null & & ! condition . equals ( "" ) & & ! conditionValue . equals ( "" ) ) { // 如果存在条件
where = " and " + condition + " like '%" + conditionValue + "%' " ; // 拼接where子句
sql + = where ; // 将where子句添加到查询语句中
connection = ( Connection ) Base . getConnection ( ) ;
sql = "select * from borrow_books where manager_id is null" ;
if ( condition ! = null & & conditionValue ! = null & & ! condition . equals ( "" ) & & ! conditionValue . equals ( "" ) ) {
where = " and " + condition + " like '%" + conditionValue + "%' " ;
sql + = where ;
}
sql + = " limit ?,?" ; // 添加分页限制
System . out . println ( "???" + 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" ) ) ; // 获取并存储card_id字段
jsonData . put ( "book_id" , resultSet . getString ( "book_id" ) ) ; // 获取并存储book_id字段
jsonData . put ( "borrow_date" , resultSet . getString ( "borrow_date" ) ) ; // 获取并存储borrow_date字段
jsonData . put ( "end_date" , resultSet . getString ( "end_date" ) ) ; // 获取并存储end_date字段
sql + = " limit ?,?" ; // 1 10 (1-1)*10
System . out . println ( "???" + sql ) ;
pstmt = connection . prepareStatement ( sql ) ;
pstmt . setInt ( 1 , ( Integer . parseInt ( page ) - 1 ) * Integer . parseInt ( limit ) ) ;
pstmt . setInt ( 2 , Integer . parseInt ( limit ) ) ;
resultSet = pstmt . executeQuery ( ) ;
while ( resultSet . next ( ) ) {
jsonData . put ( "id" , resultSet . getString ( "id" ) ) ;
jsonData . put ( "card_id" , resultSet . getString ( "card_id" ) ) ;
jsonData . put ( "book_id" , resultSet . getString ( "book_id" ) ) ;
jsonData . put ( "borrow_date" , resultSet . getString ( "borrow_date" ) ) ;
jsonData . put ( "end_date" , resultSet . getString ( "end_date" ) ) ;
jsonArray . add ( jsonData ) ; // 将当前记录添加到JSON数组中
jsonArray . add ( jsonData ) ;
}
countSql = "select count(*) as count from borrow_books where manager_id is null" ; // 基础计数查询语句
countSql + = where ; // 添加where子句到计数查询语句中
countPstmt = connection . prepareStatement ( countSql ) ; // 预编译计数SQL语句
countSet = countPstmt . executeQuery ( ) ; // 执行计数查询
if ( countSet . next ( ) ) { // 如果计数结果集有数据
count = countSet . getInt ( "count" ) ; // 获取总记录数
countSql = "select count(*) as count from borrow_books where manager_id is null" ;
countSql + = where ;
countPstmt = connection . prepareStatement ( countSql ) ;
countSet = countPstmt . executeQuery ( ) ;
if ( countSet . next ( ) ) {
count = countSet . getInt ( "count" ) ;
}
if ( ! jsonArray . isEmpty ( ) ) { // 如果JSON数组不为空
code = 0 ; // 设置返回代码为0表示成功
msg = "查询成功" ; // 设置返回消息为"查询成功"
if ( ! jsonArray . isEmpty ( ) ) {
code = 0 ;
msg = "查询成功" ;
}
} catch ( ClassNotFoundException e ) { // 捕获类未找到异常
msg = "class没找到" ; // 设置错误消息
} catch ( SQLException e ) { // 捕获SQL异常
msg = "sql错误" ; // 设置错误消息
} 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 ) { // 捕获SQL异常
msg = "关闭资源失败" ; // 设置错误消息
Base . closeResource ( null , pstmt , resultSet ) ;
Base . closeResource ( connection , countPstmt , countSet ) ;
} catch ( SQLException e ) {
msg = "关闭资源失败" ;
}
}
// 返回数据
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结果
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 ( ) ) ;
}
/ * *