[Servlet+Html5+ajax 实现简单分页]

原文:http://blog.csdn.net/zhouzhiwengang/article/details/38639607

Servlet ------dao层

package com.vixuan.dao;  
  
import java.sql.Connection;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  
import java.util.ArrayList;  
  
import com.vixuan.database.MySQLUtil;  
import com.vixuan.model.LocationInfo;  
  
public class LocationInfoDao {  
    private static Connection conn;  
    private static ResultSet resultset;  
    private static Statement statement;  
    private static int pagesize = 5; // 分页大小  
  
    // 静态模块  
    static {  
        conn = MySQLUtil.getConn();  
    }  
  
    // 公共静态方法  
    public static ResultSet ExecuteQuery(String sql) {  
        try {  
            statement = conn.createStatement();  
            resultset = statement.executeQuery(sql);  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
  
        return resultset;  
    }  
  
    // 分页逻辑-----参数:当前页码  
    public static ArrayList getLocationInfoList(int currentpageno) {  
        ArrayList LocationInfoList = new ArrayList();  
        int BeginRecord;  
          
            BeginRecord= (currentpageno - 1) * pagesize; // 开始位置  
          
          
      
        resultset = ExecuteQuery("select * from t_locationinfo limit "  
                + BeginRecord + "," + pagesize);  
        try {  
            while (resultset.next()) {  
                LocationInfo locationinfo = new LocationInfo();  
                locationinfo.setAddress(resultset.getString("address"));  
                locationinfo.setLat(resultset.getString("lat"));  
                locationinfo.setLng(resultset.getString("lng"));  
                locationinfo.settId(resultset.getInt("t_id"));  
                locationinfo.setUmobile(resultset.getString("umobile"));  
                locationinfo.setUpdateTime(resultset.getString("update_time"));  
                LocationInfoList.add(locationinfo);  
            }  
        } catch (SQLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return LocationInfoList;  
    }  
  
    // 分页统计  
    public static int getPageCount() {  
        int total = 0; // 总记录数  
        int PageCount = 0; // 页码总数  
        resultset = ExecuteQuery("select count(*) from t_locationinfo");  
        try {  
            if (resultset.next()) {  
                total = resultset.getInt(1);  
                PageCount = (total - 1) / pagesize + 1;  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return PageCount;  
    }  
      
    public static int geTotalPage() {  
        int total = 0; // 总记录数        
        resultset = ExecuteQuery("select count(*) from t_locationinfo");  
        try {  
            if (resultset.next()) {  
                total = resultset.getInt(1);                  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return total;  
    }  
  
}  

Servlet -----service服务层



package com.vixuan.service;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
import java.util.ArrayList;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import net.sf.json.JSONArray;  
import net.sf.json.JSONObject;  
  
import com.vixuan.dao.LocationInfoDao;  
import com.vixuan.model.LocationInfo;  
  
public class LocationServer extends HttpServlet {  
  
    /**  
     * 地理位置信息分页查询:Servlet  
     */  
    private static final long serialVersionUID = 1L;  
  
    @Override  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        // TODO Auto-generated method stub  
        //请求参数:当前页面  
        Integer pageNo=Integer.parseInt(request.getParameter("pageno"));  
        //返回相关数据信息  
        ArrayList list=LocationInfoDao.getLocationInfoList(pageNo);  
        //总页面数  
        Integer totalpage=LocationInfoDao.getPageCount();  
        //数据总记录数  
        Integer total=LocationInfoDao.geTotalPage();  
          
        StringBuilder builder=new StringBuilder();  
        builder.append("[");  
        for(int i=0;i
[HTML5](http://lib.csdn.net/base/html5)  相关页面和ajax函数

  
  
  
  
  
  
  
  
  
  
  
  
  
    
员工编号 员工姓名 经度 纬度 地理位置 操作
01 张三 112.35 39.85 中国北京 查看
02 李四 112.12 32.23 中国,武汉 查看
01 王五 110.89 31.67 中国,成都 查看

结果展示:


[Servlet+Html5+ajax 实现简单分页]_第1张图片

你可能感兴趣的:([Servlet+Html5+ajax 实现简单分页])