@ -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 ( ) ) ;
}
}