在项目中,分页是一个项目中必不可少的,它可以防止我们从数据库中进行大量数据查询时速度变慢,提高我们的查询效率
1、定义分页模型:PageModel
001 | package com.common.page; |
007 | * @author Administrator |
010 | public class PageModel<E> { |
016 | private int totalRecords; |
028 | public int getTotalPages() { |
029 | return (totalRecords + pageSize - 1 ) / pageSize; |
036 | public int getTopPageNo() { |
044 | public int getPreviousPageNo() { |
055 | public int getNextPageNo() { |
056 | if (pageNo >= getBottomPageNo()) { |
057 | return getBottomPageNo(); |
066 | public int getBottomPageNo() { |
067 | return getTotalPages(); |
070 | public List<E> getList() { |
074 | public void setList(List<E> list) { |
078 | public int getTotalRecords() { |
082 | public void setTotalRecords( int totalRecords) { |
083 | this .totalRecords = totalRecords; |
086 | public int getPageSize() { |
090 | public void setPageSize( int pageSize) { |
091 | this .pageSize = pageSize; |
094 | public int getPageNo() { |
098 | public void setPageNo( int pageNo) { |
099 | this .pageNo = pageNo; |
2、分页测试:在MySQL中建立admin表,里面有字段id、name、password
3、简历Admin的实体bean类:
01 | package com.common.page; |
06 | private String password; |
10 | public void setId( int id) { |
13 | public String getName() { |
16 | public void setName(String name) { |
19 | public String getPassword() { |
22 | public void setPassword(String password) { |
23 | this .password = password; |
4、测试调用:
01 | package com.common.page; |
03 | import java.sql.Connection; |
04 | import java.sql.PreparedStatement; |
05 | import java.sql.ResultSet; |
06 | import java.sql.SQLException; |
07 | import java.util.ArrayList; |
10 | import com.common.db.DbUtil; |
13 | public static PageModel findAdmins( int pageNo, int pageSize){ |
14 | Connection conn=DbUtil.getConnection(); |
15 | String sql= "select * from admin limit ?,?" ; |
16 | PageModel pageModel= null ; |
17 | PreparedStatement pstm= null ; |
20 | List<Admin> list= new ArrayList<Admin>(); |
22 | pstm=conn.prepareStatement(sql); |
23 | pstm.setInt( 1 , (pageNo- 1 )*pageSize); |
24 | pstm.setInt( 2 , pageNo*pageSize); |
25 | rs=pstm.executeQuery();; |
28 | admin.setId(rs.getInt( "a_id" )); |
29 | admin.setName(rs.getString( "a_name" )); |
30 | admin.setPassword(rs.getString( "a_pwd" )); |
33 | ResultSet rs2=pstm.executeQuery( "select count(*) from admin" ); |
38 | pageModel= new PageModel(); |
39 | pageModel.setPageNo(pageNo); |
40 | pageModel.setPageSize(pageSize); |
41 | pageModel.setTotalRecords(total); |
42 | pageModel.setList(list); |
43 | } catch (SQLException e) { |
53 | public static void main(String[] args) { |
54 | PageModel pageModel=Client.findAdmins( 2 , 4 ); |
55 | List<Admin> list=pageModel.getList(); |
57 | System.out.print( "ID:" +a.getId()+ ",用户名:" +a.getName()+ ",密码:" +a.getPassword()); |
60 | System.out.print( "当前页:" +pageModel.getPageNo()+ " " ); |
61 | System.out.print( "共" +pageModel.getTotalPages()+ "页 " ); |
62 | System.out.print( "首页:" +pageModel.getTopPageNo()+ " " ); |
63 | System.out.print( "上一页:" +pageModel.getPreviousPageNo()+ " " ); |
64 | System.out.print( "下一页:" +pageModel.getNextPageNo()+ " " ); |
65 | System.out.print( "尾页:" +pageModel.getBottomPageNo()+ " " ); |
66 | System.out.print( "共" +pageModel.getTotalRecords()+ "条记录" ); |
这样分页效果就实现了,我们要实现分页效果,只要传入相应的参数和相应的数据库执行语句即可实现,希望大家能灵活运用。
转自:http://blog.csdn.net/harderxin/article/details/7731313
jdbc demo 分页
不知道大家做项目做到最后有什么感觉没有,其实大家做来做去就是做一个列表加上分页和多条件的查询,只是不同的项目业务流程不一样而已,所以今天我想说说这里的分页。
1、 大家需要了解的是为什么我们需要分页?
因为当数据量太大时,会影响查询和传输的性能,并且我们从用户角度来考虑的话,如果让用户一次性看到成千上万条记录那用户也会疯掉的。
2、 对我们来说有哪些可实现的分页技术?
a、 存储过程分页,即在数据库中创建一个存储过程,传入SQL和页码获取当前页的记录,这个需要大家对存储过程有比较好的认识(我这块不行),当然这个从性能上来说是最好的,但是不能跨数据库平台。
b、 使用数据库专有SQL语句进行分页(Oracle的rownum、MSSQL的top、MySql的limit等),性能也很好,但是还是不能跨数据库(其实真实项目中没那么多项目要求都跨数据库)。
c、 JDBC分页,通过Statement的statement.setMaxRow(endIndex)和resultSet.absoulte(beginIndex)取得当前页范围内的记录。此种方式的性能依赖于厂商对JDBC规范的实现。这种方式的通用性是最好的,性能也不错,完全与数据库平台无关了。
d、 根据数据库类型自动生成数据库专有特性的sql语句,其实说白了也就是Hibernate的实现方式,这个自己也写过一个,其实就是根据数据库类型生成不同的数据库SQL专有语句而已。
下面我们需要写一个分页工具类,在写之前我们需要弄明白分页的原理。为了能够取得指定页码所对应的记录,我们是不是需要两个关键的参数:总记录数和每页的记录数;
每页的记录数我们可以设置一个默认值,10、15、20、25都无所谓,根据实际需求。
总记录数就没办法了,需要额外从数据库中利用Count函数取了,通过这两个参数我们是不是可以计算出总页数。同时我们也可以判断用户传过来的页码是否有效(小于第一页OR超出最后一页),然后我们再根据页码和每页记录数是不是就可以计算出当前页的的开始条数和终止条数了。
下面我们就需要来看看分页的工具类了
001 | package com.iflytek.page; |
006 | * @author xudongwang 2012-1-19 |
008 | * Email:xdwangiflytek@gmail.com |
015 | private int totalRow; |
020 | private int pageSize = 10 ; |
025 | private int currentCount; |
035 | private int beginIndex; |
040 | private int endIndex; |
047 | * @param currentCount |
050 | public Page( int totalRow, int currentCount) { |
051 | this .totalRow = totalRow; |
052 | this .currentCount = currentCount; |
061 | * @param currentCount |
066 | public Page( int totalRow, int currentCount, int pageSize) { |
067 | this .totalRow = totalRow; |
068 | this .currentCount = currentCount; |
069 | this .pageSize = pageSize; |
073 | private void calculate() { |
074 | total = totalRow / pageSize + ((totalRow % pageSize) > 0 ? 1 : 0 ); |
076 | if (currentCount > total) { |
077 | currentCount = total; |
078 | } else if (currentCount < 1 ) { |
082 | beginIndex = (currentCount - 1 ) * pageSize; |
083 | endIndex = beginIndex + pageSize; |
084 | if (endIndex > totalRow) { |
089 | public int getTotalRow() { |
093 | public int getPageSize() { |
097 | public int getCurrentCount() { |
101 | public int getTotal() { |
105 | public int getBeginIndex() { |
109 | public int getEndIndex() { |
继续
在后台获取前台传进来的页码 //从页面取得页码
3 | currentPage = Integer.parseInt(request.getParameter( "currentPage" )); |
4 | } catch (Exception ex) {} |
7 | int totalRow = studentDao.getAllStudents(); //通过select count 取得总记录数 |
8 | Page page = new Page(totalRow, currentPage); |
数据访问层, StduentDao.java
01 | public List<Stduent> getStudentsByPage(Page page) { |
02 | List<Stduent> students = new ArrayList<Stduent>(); |
04 | String sql = "SELECT id,name,email FROM tbl_stduent" ; |
05 | Connection conn = null ; |
07 | conn = DbUtil.getConnection(); |
08 | Statement statement = conn.createStatement(); |
09 | statement.setMaxRows(page.getEndIndex()); //关键代码,设置最大记录数为当前页记录的截止下标 |
10 | ResultSet resultSet = statement.executeQuery(sql); |
11 | if (page.getBeginIndex() > 0 ) { |
12 | resultSet.absolute(page.getBeginIndex()); //关键代码,直接移动游标为当前页起始记录处 |
14 | while (resultSet.next()) { |
15 | Stduent student = new Student(); |
17 | students.add(student); |
26 | } catch (SQLException e) { |