分页代码

 

 

分页类:
package com.zhao.page;

public class PageCut {

	private int pageSize;
	private int countRecord;
	private int countPage;
	private int startIndex;
	private int endIndex;
	private int nowPage;

	public PageCut(int pageSize, int countRecord, int nowPage) {
		super();
		this.pageSize = pageSize;
		this.countRecord = countRecord;
		this.nowPage = nowPage;
		getPaginationPage();
	}

	private void getPaginationPage() {
		// 计算开始索引
		this.startIndex = (nowPage - 1) * pageSize + 1;
		// 计算总页数
		this.countPage = (countRecord % pageSize == 0) ? (countRecord / pageSize)
				: (countRecord / pageSize + 1);
		this.endIndex = (nowPage * pageSize)>= countRecord ? countRecord:nowPage*pageSize;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCountRecord() {
		return countRecord;
	}

	public void setCountRecord(int countRecord) {
		this.countRecord = countRecord;
	}

	public int getCountPage() {
		return countPage;
	}

	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}
	
	public int getEndIndex() {
		return endIndex;
	}

	public void setEndIndex(int endIndex) {
		this.endIndex = endIndex;
	}
	/*
	 * 
	 * public Map<String, String> getPagination(Integer pageSize, Integer
	 * currentPage, Integer totalCount) { Map<String, String> pageMap = new
	 * HashMap<String, String>();
	 * 
	 * if (currentPage == null || totalCount < pageSize) { currentPage = 1; }
	 * else { //总页数 int totalPage = (totalCount % pageSize == 0) ?
	 * (totalCount/pageSize) : (totalCount / pageSize + 1); //每页开始记录 int
	 * startIndex = (currentPage - 1) * pageSize + 1; //每页结束记录 int endIndex =
	 * currentPage * pageSize >= totalCount ? totalCount : currentPage *
	 * pageSize;
	 * 
	 * pageMap.put("totalPage", "" + totalPage); pageMap.put("currentPage", "" +
	 * currentPage); pageMap.put("startIndex", "" + startIndex);
	 * pageMap.put("endIndex", "" + endIndex); }
	 * 
	 * 
	 * return pageMap; }
	 */

}

 


获取总记录数:
package com.zhao.page;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SelectCount {
	private static final String DRIVIER = "com.mysql.jdbc.Driver";
	private static final String URL = "jdbc:mysql://localhost:3306/test";
	private static final String NAME = "root";
	private static final String PASSWORD = "root";
	private PreparedStatement stmt;
	private Connection con;

	public int querForCount() {
		int count = -1;
		try {
			Class.forName(DRIVIER);
			con = DriverManager.getConnection(URL, NAME, PASSWORD);
			stmt = con.prepareStatement("select count(*) from user");
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				count = rs.getInt(1);
				System.out.println(count);
			}

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return count;
	}

}

测试代码
package com.zhao.page;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class TestPage {
    
	private static int countRecord ;
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		SelectCount selectCount = new SelectCount();
		countRecord = selectCount.querForCount();
		PageCut pageCut = new PageCut(4, countRecord, 8);
		System.out.println(pageCut.getCountPage()+"======" +pageCut.getPageSize()+
				"======" +pageCut.getStartIndex()+"======" +pageCut.getNowPage()+"======" +pageCut.getEndIndex());
		/*PageCut pageCut = new PageCut();
		Map<String, String> map = pageCut.getPagination(pageSize, currentPage, totalCount);
		Iterator<Entry<String, String>> iterator  = map.entrySet().iterator();
		while (iterator.hasNext()) {
			@SuppressWarnings({ "unchecked", "rawtypes" })
			Map.Entry<String, String> entry = (Map.Entry)iterator.next();
			System.out.println(entry.getKey()+"======" + entry.getValue());
		}*/
	}
}

你可能感兴趣的:(java)