分页讲解


一、在java中

        final int PAGESIZE = 10;//每页显示的具体条数
	public List<User> findNowPageInfo(int nowpage) throws Exception {
		List<User> entities = new ArrayList<User>();
		String sql = "select id,name,pass from user limit ?,?";

		conn = JdbcUtil.getConn();
		pstmt = conn.prepareStatement(sql);
		int index = 1;
		pstmt.setInt(index++, (nowpage - 1) * PAGESIZE);
		pstmt.setInt(index++, PAGESIZE);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			User entity = new User();
			entity.setId(rs.getInt("id"));
			entity.setName(rs.getString("name"));
			entity.setPass(rs.getString("pass"));
			entities.add(entity);
		}
		return entities;

	}

	// 总页数
	public Integer getCountPage() throws Exception {
		String sql = "select count(*) as c from user";

		conn = JdbcUtil.getConn();
		pstmt = conn.prepareStatement(sql);

		rs = pstmt.executeQuery();
		while (rs.next()) {
			int countsize = rs.getInt("c");
			return countsize % PAGESIZE == 0 ? countsize / PAGESIZE : countsize
					/ PAGESIZE + 1;
		}
		return null;
	}


封装分页工具类Pagination.java

package util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class Pagination<T> {

	private Integer pageSize;// 每页显示的记录数
	private Integer nowPage; // 当前页
	private Integer countSize;// 总记录数
	private Integer countPage;// 总页数

	// 数据库中开始记录数和结束记录数
	private Integer startIndex;// 开始记录数
	private Integer endIndex;// 结束记录数

	// 页面中数据
	private Integer startRow;
	private Integer endRow;
	
	//封装的每页显示的记录
	private List<T> rows;
	
	
	public Pagination(Integer nowPage, Integer pageSize, String table) {
		// 当前页
		this.nowPage = nowPage;
		this.pageSize = pageSize;

		// 判断当前页是否小于1
		if (this.nowPage < 1) {
			this.nowPage = 1;
		}
		// 获取总记录数
		this.countSize = this.selectCountSize(table);
		// 计算总页数
		this.countPage = this.countSize % this.pageSize == 0 ? this.countSize
				/ this.pageSize : this.countSize / this.pageSize + 1;
		// 判断当前页是否大于等于总页数
		if (this.nowPage >= this.countPage) {
			this.nowPage = this.countPage;
		}

		// 计算开始的记录数和结束的记录数
		// select * from table limit ?,?
		this.startIndex = (this.nowPage - 1) * this.pageSize;
		this.endIndex = this.pageSize;

		// 计算出页面中开始记录数和结束记录数 //当前从1记录到4记录结束,共4记录,当前是1页,共1页
		// 7 3
		// 开始记录
		this.startRow = (this.nowPage - 1) * this.pageSize + 1;
		// 结束记录
		this.endRow = this.nowPage * this.pageSize;
		// 判断结束的记录数 是否大于总记录数
		if (this.endRow >= this.countSize) {
			this.endRow = this.countSize;
		}

	}
	
	

	public Integer getPageSize() {
		return pageSize;
	}

	public Integer getNowPage() {
		return nowPage;
	}

	public Integer getCountSize() {
		return countSize;
	}

	public Integer getCountPage() {
		return countPage;
	}

	public Integer getStartIndex() {
		return startIndex;
	}

	public Integer getEndIndex() {
		return endIndex;
	}

	public Integer getStartRow() {
		return startRow;
	}

	public Integer getEndRow() {
		return endRow;
	}
	
	
	

	public List<T> getRows() {
		return rows;
	}

	public void setRows(List<T> rows) {
		this.rows = rows;
	}

	

	/**
	 * 获取总记录数
	 * 
	 * @param table
	 * @return
	 */
	public Integer selectCountSize(String table) {
		int countSize = 0;
		String sql = "select count(*) as c from " + table;
		Connection conn = DBConn.getConn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				countSize = rs.getInt("c");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBConn.release(rs, pstmt);
		}
		return countSize;
	}



	@Override
	public String toString() {
		return "Pagination [pageSize=" + pageSize + ", nowPage=" + nowPage
				+ ", countSize=" + countSize + ", countPage=" + countPage
				+ ", startIndex=" + startIndex + ", endIndex=" + endIndex
				+ ", startRow=" + startRow + ", endRow=" + endRow + ", rows="
				+ rows + "]";
	}

	

	
	
}

二、在js中怎样求总页数

<!DOCTYPE html>
<html>
<head>
<title>demo.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
	window.onload = function() {

               第一种方案

           var pagesize = 3;
           var countrecode = 5;
           var jieguo = countrecode / pagesize;alert(Math.ceil(jieguo));
          /*page();第二种方案*/
};
    function page() {
       var pagesize = 3;
       var countrecode =7;
       var qy = countrecode % pagesize;
      if (qy!= 0) {
         countrecode = countrecode + (pagesize - qy);
      } else{
         countrecode=countrecode;
      }
      var countpage=countrecode/pagesize;
      alert(countpage);
      }
 </script>
</head>
<body>
This is my HTML page.<br>
</body>
</html>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值