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 javabean.Base; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * Servlet implementation class Book */ @WebServlet("/reader/book") public class Book 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 = ""; // SQL查询条件字符串 // 默认分页参数 if (page == null) { page = "1"; } if (limit == null) { limit = "10"; } // 数据库连接和SQL语句准备 Connection connection = null; PreparedStatement pstmt = null; PreparedStatement countPstmt = null; ResultSet resultSet = null; ResultSet countSet = null; String sql = ""; String countSql = ""; // 返回数据结构初始化 int code = 1; // 状态码,1表示失败,0表示成功 String msg = "无数据"; // 消息提示 int count = 0; // 总记录数 JSONObject jsonData = new JSONObject(); // 单条记录的JSON对象 JSONArray jsonArray = new JSONArray(); // 所有记录的JSON数组 JSONObject jsonResult = new JSONObject(); // 最终返回的JSON对象 try { // 获取数据库连接 connection = (Connection) Base.getConnection(); sql = "select * from books "; // 根据条件拼接SQL查询语句 if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")) { where = " where " + condition + " like '%" + conditionValue + "%' "; sql += where; } // 分页查询 sql += " limit ?,?"; // 计算偏移量和限制数量 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()) { // 获取图书信息 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; PreparedStatement pstmt1 = connection.prepareStatement(sql1); ResultSet rs1 = pstmt1.executeQuery(); String lib = ""; while (rs1.next()) { lib = rs1.getString("name"); } jsonData.put("library_id", lib); // 获取分类名称 String sql2 = "select * from book_sort where ID =" + sortid; PreparedStatement pstmt2 = connection.prepareStatement(sql2); ResultSet rs2 = pstmt2.executeQuery(); String sort = ""; while (rs2.next()) { sort = rs2.getString("name"); } jsonData.put("sort_id", sort); // 将单条记录添加到JSON数组中 jsonArray.add(jsonData); } // 统计总记录数 countSql = "select count(*) as count from books "; countSql += where; countPstmt = connection.prepareStatement(countSql); countSet = countPstmt.executeQuery(); if (countSet.next()) { count = countSet.getInt("count"); } if (!jsonArray.isEmpty()) { code = 0; // 查询成功 msg = "查询成功"; } } catch (ClassNotFoundException e) { msg = "class没找到"; // 类未找到异常处理 } catch (SQLException e) { msg = "sql错误"; // SQL异常处理 } finally { // 关闭资源 try { Base.closeResource(null, pstmt, resultSet); Base.closeResource(connection, countPstmt, countSet); } catch (SQLException e) { msg = "关闭资源失败"; // 关闭资源异常处理 } } // 构建返回的JSON对象 jsonResult.put("code", code); jsonResult.put("count", count); jsonResult.put("msg", msg); jsonResult.put("data", jsonArray.toArray()); // 输出JSON响应 PrintWriter out = resp.getWriter(); out.print(jsonResult.toString()); } }