Java分页

JavaWeb分页的实现

2016/5/18 21:53:35 SeventeenWen

思路就是根据当前页码和每页记录数查询数据库,计算出总记录数,根据总记录数和每页记录数算出总页数,最后在页面上做分页处理。

    import java.util.List;

    public class PageBean {

    //  当前页码
        private int pageCode;
    //  总记录数
        private int totalCode;
    //  每页记录数
        private int paginal;
    //  当前页记录
        private List beanList;
    //  
        private String url;
        @Override
        public String toString() {
            return "PageBean [pageCode=" + pageCode + ", totalCode=" + totalCode + ", paginal=" + paginal + ", beanList="
                    + beanList + ", url=" + url + "]";
        }
        public int getPageCode() {
            return pageCode;
        }
        public void setPageCode(int pageCode) {
            this.pageCode = pageCode;
        }
        public int getTotalCode() {
            return totalCode;
        }
        public void setTotalCode(int totalCode) {
            this.totalCode = totalCode;
        }
        public int getPaginal() {
            return paginal;
        }
        public void setPaginal(int paginal) {
            this.paginal = paginal;
        }
        public List getBeanList() {
            return beanList;
        }
        public void setBeanList(List beanList) {
            this.beanList = beanList;
        }
        public String getUrl() {
            return url;
        }
        public void setUrl(String url) {
            this.url = url;
        }

        public int getTp(){

    //      计算总页数tp 总记录数除以每页记录数
            int tp = totalCode/paginal;
    //      总记录%每页记录数 如果有余说明有还需要加一页显示
            return totalCode%paginal==0 ? tp :tp+1;
        }




    }

数据库查询:

/**
 * 查询页数
 * @return count
 */
public int findCount(){
    int count=0;
    try {
        Connection con = JdbcUtils.getConnection();
        String sql = "SELECT COUNT(*) FROM t_goods";
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        if(rs.next()){
            count=rs.getInt(1);
        }
        rs.close();
        con.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return count;
}
/**
 * 
 * 传递两个参数当前页码数,和每页记录数
 * @param pageCode
 * @param paginal
 * @return
 */
public PageBean findAll(int pageCode,int paginal){
    PageBean pb = new PageBean();

    pb.setPageCode(pageCode);
    pb.setPaginal(paginal);
    int totalCode = this.findCount();
//      得到总记录数
    pb.setTotalCode(totalCode);
    List list = new ArrayList();
    try {
        String sql = "select *from t_goods limit ?,?";
        Connection con =JdbcUtils.getConnection();
        PreparedStatement ps =con.prepareStatement(sql);
        //表示查询从第value条数据到paginal条数据
        int value =  (pageCode-1)*paginal;
        System.out.println(value);
        ps.setInt(1,value);
        System.out.println(paginal);
        ps.setInt(2, paginal);
        ResultSet re = ps.executeQuery();
        while(re.next()){
            /从数据库得到的数据保存在Bean对象中
        }
        re.close();
        ps.close();
        con.close();

    } catch (SQLException e) {
        e.printStackTrace();
    }
    pb.setBeanList(list);

    return pb;

}

页面:
取出在pageBean中的页码数,当前页pageBean.pageCode,全部页pageBean.tp根据这两个值就可以计算所有的需要了;

你可能感兴趣的:(JavaEE)