欢迎来到天天文库
浏览记录
ID:37922993
大小:30.00 KB
页数:6页
时间:2019-06-02
《Java Web分页技术》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、我们这里,先看从数据库中读取的情况操作数据库就需要tsql语句,mssqlserver2005新推出了一个row_number()很好用,还有就是mysql的limit也非常好使。mssqlserver2005的如下:select*from(selectrow_number()over(orderbyename)asrn,f.*fromempf)bwhereb.rnbetween6and10;mysql的:select*fromemplimit5,5mysql的应注意,使用limit时,表中必须用主键,还有limit后的两个参数分别代表(标识位,长度),标识位
2、从0开始现在开始一步步完成,首先完成model模块,建立pagebeanimportjava.util.*;publicclassPageBean{privateCollectionobjs;//从数据库中读的集合privateinttotalCount;//总的条数privateintpageNo;//当前的页数privateintpageCount;//每页的条数publicintgetPageCount(){returnpageCount;}publicvoidsetPageCount(intpageCount){this.pageCount=pageC
3、ount;}publicvoidsetPageNo(intpageNo){this.pageNo=pageNo;}publicintgetTotalPage(){if(totalCount%pageCount==0){returntotalCount/pageCount;}else{returntotalCount/pageCount+1;}}//多写一个判断下一页的方法publicbooleanisNext(){returnpageNo4、o>1;}publicCollectiongetObjs(){returnobjs;}publicvoidsetObjs(Collectionobjs){this.objs=objs;}publicintgetPageNo(){returnpageNo;}publicintgetTotalCount(){returntotalCount;}publicvoidsetTotalCount(inttotalCount){this.totalCount=totalCount;}publicPageBean(Collectionobjs,inttotalCount,i5、ntpageNo,intpageCount){this.objs=objs;this.totalCount=totalCount;this.pageNo=pageNo;}}之后开始实现biz模块我们写一个具体分页的逻辑importjava.util.*;importjava.sql.*;publicclassEmpBiz{publicEmpBiz(){}//具体实现分页的方法,传递两个参数,一个第几页,一个每页的数量publicPageBeanlistEmps(intpageNo,intpageCount){Connectioncon=null;Stateme6、ntstmt=null;ResultSetrs=null;ArrayListemps=newArrayList();//次数我使用mssqlserver的方式,所以计算前后两个范围,用a,b表示inta=pageNo*pageCount;intb=(pageNo-1)*pageCount;try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databas7、eName=Master;","sa","sa");stmt=con.createStatement();rs=stmt.executeQuery("select*from(selectrow_number()over(orderbyename)asrn,f.*fromempf)bwhereb.rnbetween"+a+"and"+b);while(rs.next()){Employeee=newEmployee();e.setEmpno(rs.getInt("empno"));e.setSal(rs.getDouble("sal"));emps.add(e)8、;}rs=stmt.executeQu
4、o>1;}publicCollectiongetObjs(){returnobjs;}publicvoidsetObjs(Collectionobjs){this.objs=objs;}publicintgetPageNo(){returnpageNo;}publicintgetTotalCount(){returntotalCount;}publicvoidsetTotalCount(inttotalCount){this.totalCount=totalCount;}publicPageBean(Collectionobjs,inttotalCount,i
5、ntpageNo,intpageCount){this.objs=objs;this.totalCount=totalCount;this.pageNo=pageNo;}}之后开始实现biz模块我们写一个具体分页的逻辑importjava.util.*;importjava.sql.*;publicclassEmpBiz{publicEmpBiz(){}//具体实现分页的方法,传递两个参数,一个第几页,一个每页的数量publicPageBeanlistEmps(intpageNo,intpageCount){Connectioncon=null;Stateme
6、ntstmt=null;ResultSetrs=null;ArrayListemps=newArrayList();//次数我使用mssqlserver的方式,所以计算前后两个范围,用a,b表示inta=pageNo*pageCount;intb=(pageNo-1)*pageCount;try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databas
7、eName=Master;","sa","sa");stmt=con.createStatement();rs=stmt.executeQuery("select*from(selectrow_number()over(orderbyename)asrn,f.*fromempf)bwhereb.rnbetween"+a+"and"+b);while(rs.next()){Employeee=newEmployee();e.setEmpno(rs.getInt("empno"));e.setSal(rs.getDouble("sal"));emps.add(e)
8、;}rs=stmt.executeQu
此文档下载收益归作者所有