我是如何通过一 条SQL查出分页数据和总页数的

package org.http;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.utils.DB;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 获取cz xx rk个人信息
 * @author  test
 */
public class API extends HttpServlet {
 
 private static final long serialVersionUID = 1L;
 
 private static Logger log = Logger.getLogger(API.class);
 

 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  log.info("获取常住人口个人详细信息. " + req.getRemoteHost());
  
  //最终返回的数据对象
  JSONObject result = new JSONObject();
  
  String sql = "  SELECT SQL_CALC_FOUND_ROWS * from populace  WHERE 1=1  limit ?,? ";
  
  //性别
  String xb = req.getParameter("xb");
  
  //出生日期
  String beginTime = req.getParameter("beginTime");
  
  String endTime = req.getParameter("endTime");
  
  //页面大小
  String reqLimit = req.getParameter("limit");
  reqLimit = reqLimit == null ? "10" : reqLimit;
  
  //页码
  String offset = req.getParameter("offset");
  offset = offset == null ? "1" : offset;
  
  if(!"".equals(xb) && null != xb) {
   sql += " and xb = '"+ xb +"' ";
  }
  
  if(!"".equals(beginTime) && null != beginTime) {
   sql += " and csrq > '"+ beginTime +"' ";
  }
  
  if(!"".equals(endTime) && null != endTime) {
   sql += " and csrq < '"+ endTime +"' ";
  }
  
  
  //主要的代码是在在这里, SELECT FOUND_ROWS() as SHAO2TOTAL; 查询上一次 SQL 的总记录数
  sql += "; SELECT FOUND_ROWS() as SHAO2TOTAL;";
  
  Connection conn = DB.getConnection();
  
  

  try {
   PreparedStatement  statement = conn.prepareStatement(sql);
   
   //处理分页
   int startLimit = (Integer.parseInt(offset)-1) * Integer.parseInt(reqLimit);
   int endLimit =  Integer.parseInt(offset) * Integer.parseInt(reqLimit);
   statement.setInt(1, startLimit);
   statement.setInt(2, endLimit);
   
   
   ResultSet dataRs = statement.executeQuery(sql);
   
   JSONArray arr = new JSONArray();
   
   while(dataRs.next()) {
    JSONObject temp = new JSONObject();
    
    temp.put("xm", dataRs.getString("xm"));
    temp.put("xb", dataRs.getString("xb"));
    temp.put("gmsfhm", dataRs.getString("gmsfhm"));
    temp.put("csrq", dataRs.getString("csrq"));
    temp.put("xz", dataRs.getString("xz"));
    
    arr.add(temp);
   }
   
   result.put("data", arr);
   
   //处理数据总数
   int totalPage = 0;
   
   while(statement.getMoreResults()) {
    ResultSet pageRs = statement.getResultSet();
    
    while(pageRs.next()) {
     
     if(pageRs.findColumn("SHAO2TOTAL") > 0) {
      totalPage = pageRs.getInt("SHAO2TOTAL");
      continue;
     }
    }
   }
   
   //
   result.put("total", totalPage);
   
   
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
 }

}



评论