java 关于分页的实现
关于java实现分页
转自:http://www.cnblogs.com/slliang/archive/2012/08/22/2651053.html
1.分页工具类,封装分页信息
package com.student.util; import java.util.List; import com.student.entity.Person; /** * 封装分页信息 * @author Administrator * * @param <Person> */ public class PageModel<E> { //结果集 private List<E> list; //查询记录数 private int totalRecords; //第几页 private int pageNo; //每页多少条记录 private int pageSize; //总页数 public int getTotalPages(){ return (totalRecords + pageSize -1)/pageSize; } //首页 public int getTopPage(){ return 1; } //上一页 public int getPreviousPage(){ if(pageNo<=1){ return 1; } return pageNo-1; } //下一页 public int getNextPage(){ if(pageNo>=getBottomPage()){ return getBottomPage(); } return pageNo+1; } //尾页 public int getBottomPage(){ return getTotalPages(); } public List<E> getList() { return list; } public void setList(List<E> list) { this.list = list; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
2.测试类
public class PageModelTest{ public static void main(String[] args){ int pageNo=1; int pageSize=10; findUserList(pageNo,pageSize); } /** * 分页查询 * @param pageNo 第几页 * @param pageSize 每页多少条记录 * @return PageModel<E> */ public PageModel<Person> findUserList(int pageNo,int pageSize){ PageModel<Person> pageModel = null; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; //各数据库的分页语句不一样 /*oracle实现分页,三层嵌套,这里10应该为pageNo*pageSize ,0为(pageNo-1)*pageSize String sql=" select column1,column2,column3,column4,column5 from (select rownum rn,column1,column2,column3,column4,column5 from (select column1,column2,column3,column4,column5 from table_name order by column desc) where rownum<=10) where rn>0"; */ //mysql实现分页 String sql="select * from person order by id desc limit ?,? "; conn=DBUtil.getUtil().getConnection(); try { ps=conn.prepareStatement(sql); ps.setInt(1, (pageNo-1) * pageSize); ps.setInt(2, pageSize); rs=ps.executeQuery(); List<Person> personList = new ArrayList<Person>(); while(rs.next()){ Person person=new Person(); person.setName(rs.getString("stu_name")); person.setPassword(rs.getString("stu_psw")); person.setNumber(rs.getString("stu_number")); person.setBirthday(rs.getDate("stu_birth")); person.setSex(rs.getInt("stu_sex")); person.setPolity(rs.getInt("stu_polity")); person.setBrief(rs.getString("stu_brief")); person.setType(rs.getInt("type")); person.setState(rs.getInt("state")); personList.add(person); } pageModel = new PageModel<Person>(); pageModel.setList(personList); pageModel.setTotalRecords(getTotalRecords(conn)); pageModel.setPageSize(pageSize); pageModel.setPageNo(pageNo); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); } if(conn!=null){ conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return pageModel; } /** * 得到总记录数,私有方法,外部无法访问,本类中使用 * @param conn * @return */ private int getTotalRecords(Connection conn){ PreparedStatement ps = null; ResultSet rs = null; String sql="select count(*) from person"; conn=DBUtil.getUtil().getConnection(); int count=0; try { ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ //此时根据sql语句查出的只有一列,否则不建议用int标识字段 count = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); }finally{ try { if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return count; } }