分页技术.docx
- 文档编号:30388555
- 上传时间:2023-08-14
- 格式:DOCX
- 页数:18
- 大小:19.83KB
分页技术.docx
《分页技术.docx》由会员分享,可在线阅读,更多相关《分页技术.docx(18页珍藏版)》请在冰豆网上搜索。
分页技术
株洲北大青鸟作者:
凌智
分页查询功能一直是web编程中常用的技术,如何实现可重复使用
而又简单的分页技术呢,下面的代码可以提供一些参考,实现用户列表
的分页显示,当其它数据需分页显示时,可以复用其中的分页对象
(SplitPage.java),然后提供实现dao接口的类.
先列出这个例子中的接口类和页面文件清单:
1.数据库连接对象:
DBConnection,获取数据连接对象getConnection();
2.分页(类)对象:
SplitPage.java,提供页面参数(是否首页/尾页/
当前第n页等)
3.数据库访问接口:
dao,提供业务逻辑中对数据库操作接口,
用于分页查寻的接口方法是:
findAll(SplitPagesp)//方法的输入参数为分页对象
getRows()//提供总的记录数
UserDao.java用于实现这个接口.
4.用户JavaBean:
User.java,分页显示用户列表时的用户对象.
5.userList.jsp:
分页显示用户列表jsp页面.
============================================================
下面就给出详细介绍:
------------------------------------------------------------
1.关于数据库连接对象,实现下面的方法,为dao提供Connection对象
//获得数据库连接
publicConnectiongetConnection(){
Connectionconn=null;
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,user,pass);
}catch(Exceptione){
e.printStackTrace();
}
returnconn;
}
//关闭数据库连接
publicvoidclose(ResultSetrs,Statementst,Connectionconn){
try{
if(rs!
=null)rs.close();
if(st!
=null)st.close();
if(conn!
=null){
if(!
conn.isClosed()){
conn.close();
}
}
}catch(Exceptione){
e.printStackTrace();
}
}
------------------------------------------------------------
2.分页对象的实现:
publicclassSplitPage{
//分页请求时,请求标识参数
finalpublicstaticStringFIRSTPAGE="first";//请求第一页
finalpublicstaticStringPREVIOUSEPAGE="previous";//请求上一页
finalpublicstaticStringNEXTPAGE="next";//请求下一页
finalpublicstaticStringLASTPAGE="last";//请求最后一页
privateintpageRows=3;//每页显示记录数,默认3条,可以在页面设置
privateinttotalRows=0;//总的记录数,这个参数由数据库dao对象提供
privateintcurrentPage=1;//当前显示的页面编号,默认第一页
privateintfirstPage=1;//首页位置,默认第一页
privateinttotalPages=1;//总的页面数量,默认就一页
publicintgetCurrentPage(){
returncurrentPage;
}
publicvoidsetCurrentPage(intcurrentPage){
this.currentPage=currentPage;
}
publicintgetFirstPage(){
returnfirstPage;
}
publicvoidsetFirstPage(intfirstPage){
this.firstPage=firstPage;
}
publicintgetPageRows(){
returnpageRows;
}
publicvoidsetPageRows(intpageRows){
if(pageRows==0)thrownewArithmeticException();
this.pageRows=pageRows;//如果pageRows被设置为零,应当抛出异常.
//修改每页显示记录数,将会直接影响总页面数,所以要同时修改
this.totalPages=(this.totalRows%this.pageRows==0)?
this.totalRows/this.pageRows:
this.totalRows/this.pageRows+1;
}
publicintgetTotalRows(){
returntotalRows;
}
//设置分页对象的总记录属性后,就应该根据每页面显示记录数,计算得到总的页面数
publicvoidsetTotalRows(inttotalRows){
this.totalRows=totalRows;
//计算总的页面数(或最后的页面号),两个整数相除如果刚好除尽,值就是相除后的商,否则如果有余数,商应当还加1.
this.totalPages=(this.totalRows%this.pageRows==0)?
this.totalRows/this.pageRows:
this.totalRows/this.pageRows+1;
}
//不应该提供方法设置总页面数,它是计算得到的
//但应当提供获取总页面数的方法.
publicintgetTotalPages(){
returntotalPages;
}
//根据请求页面的标识参数,重新计算当前要显示的页面
//核心方法,实现分页显示功能.
publicintconfirmPage(Stringflag){
intnewPage=this.currentPage;
if(flag!
=null){//flag只可能是下面值之一
if(flag.equals(SplitPage.FIRSTPAGE)){
newPage=1;
}elseif(flag.equals(SplitPage.LASTPAGE)){
newPage=this.totalPages;
}elseif(flag.equals(SplitPage.NEXTPAGE)){
//页面总数和当前页面编号相等吗,如果是那么页面编号不往后走,否则页面编号加一
newPage=(this.totalPages==this.currentPage)?
this.currentPage:
this.currentPage+1;
}elseif(flag.equals(SplitPage.PREVIOUSEPAGE)){
//第一个页面和当前页面相等吗,如果是那么页面编号不往前走,否则页面编号减一
newPage=(this.firstPage==this.currentPage)?
this.currentPage:
this.currentPage-1;
}else{//否则是一个数字字符串
inttpage=Integer.parseInt(flag.trim());
newPage=tpage;
}
}else{//如果请求标识参数为空,那么当前页码不变
newPage=this.currentPage;
}
//在返回前设置当前页面
this.setCurrentPage(newPage);
returnnewPage;
}
}
------------------------------------------------------------
3.在UserDao.java中实现接口方法findAll(SplitPagesp)
提供数据集合的分页列表.只要实现了这个方法,总是可以正确提供
分页显示数据.
//分页查询
publicList
List
//获取数据库连接对象
Connectionconn=factory.getConnection();
Statementst=null;
ResultSetrs=null;
//在sqlserver数据中分页查询的sql语句,分页的参数来自
//入参SplitPage对象.
Stringsql="selecttop"+sp.getPageRows()+"*fromusers2whereid"+
"notin(selecttop("+sp.getPageRows()*(sp.getCurrentPage()-1)+")id"+"fromusers2orderbyid)orderbyid";
try{
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
Useruser=newUser();
user.setId(rs.getInt
(1));
user.setUserName(rs.getString
(2));
user.setPassWord(rs.getString(3));
user.setBirthday(rs.getString(4));
user.setSex(rs.getString(5));
user.setEmail(rs.getString(6));
user.setMarried(rs.getString(7));
user.setInterest(rs.getString(8));
list.add(user);
}
}catch(SQLExceptione){
e.printStackTrace();
}finally{
factory.close(rs,st,conn);
}
returnlist;
}
------------------------------------------------------------
4.剩下的工作就是在jsp中分页显示用户列表了,凡是需要分页浏览
显示的数据都可以通过dao和SplitPage对象实现了.
//这段代码实现在分页中
//切换到分页范围内的任何一个页面
functiongo(){
vargoPage=document.all.selectpage.value;
alert("我们将去页面:
userList.jsp?
flag="+goPage);
document.open("userList.jsp?
flag="+goPage,"_self","");
}
<%--
spage是分页对象,保存分页的详细信息,此对象存放在session中
每次查询或显示分页数据时只要设置此对象的当前页就可.
--%>
useBeanid="spage"class="com.accp.zl.util.SplitPage"scope="session">
useBean>
useBeanid="dao"class="com.accp.zl.dao.UserDao"scope="session">
useBean>
<%
//翻页时的方向值,即SplitPage中请求标识参数
Stringflag=request.getParameter("flag");
//每次刷新页面时都应当重新获得表中的记录数,
//因为翻页过程中表的记录可能随时都会更新
inttotalRows=dao.getRows();//总的记录数
spage.setTotalRows(totalRows);
//重新计算确定当前要显示的页面值,这是一次必要的调用,
//实现了翻页
intcurrentPage=spage.confirmPage(flag);
//spage.setPageRows(10);修改每页显示的记录数,默认3
%>
查看用户列表,用户管理
编号 用户名 出生日期 爱好 操作
<%
//翻页查询时需要传递翻页对象,翻页对象保存在session中
List
for(Useru:
list){
%>
userid=<%=u.getId()%>'>修改 userid=<%=u.getId()%>'> 删除
<%}%>
<%--下面页面分页操作,其它数据分页页面复用时拷贝这段
代码就可以了.
--%>
【
flag=<%=SplitPage.FIRSTPAGE%>">首页 flag=<%=SplitPage.PREVIOUSEPAGE%>">上一页 flag=<%=SplitPage.NEXTPAGE%>">下一页 flag=<%=SplitPage.LASTPAGE%>">最后页 go();"> <% for(inti=1;i<=spage.getTotalPages();i++){ %> "selected='selected'": ""%>><%=i%>/<%=spage.getTotalPages()%> <%}%> 】
jsp分页代码(jsp+JavaBean)(2009-09-1319:
19:
51)
转载标签:
jsp分页javabeanit分类:
jsp
ShowRecordByPage.java
packagedatabase.operation;
importjava.sql.*;
importcom.sun.rowset.*;
publicclassShowRecordByPage
{intpageSize=10;//每页显示的记录数
intpageAllCount=0;//分页后的总页数
intshowPage=1;//当前显示页
StringBufferpresentPageResult;//显示当前页内容
CachedRowSetImplrowSet;//用于存储ResultSet对象
StringdatabaseName="";//数据库名称
StringtableName="";//表的名字
Stringuser="";//用户
Stringpassword="";//密码
String字段[]=newString[100];
int字段个数=0;
publicShowRecordByPage()
{presentPageResult=newStringBuffer();
try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
}
catch(Exceptione){}
}
publicvoidsetPageSize(intsize)
{pageSize=size;
字段个数=0;
Stringuri="jdbc:
sqlserver:
//127.0.0.1:
1433;DatabaseName="+databaseName;
try{Connectioncon=DriverManager.getConnection(uri,user,password);
DatabaseMetaDatametadata=con.getMetaData();
ResultSetrs1=metadata.getColumns(null,null,tableName,null);
intk=0;
while(rs1.next())
{字段个数++;
字段[k]=rs1.getString(4);//获取字段的名字
k++;
}
Statementsql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSetrs=sql.executeQuery("SELECT*FROM"+tableName);
rowSet=newCachedRowSetImpl();//创建行集对象
rowSet.populate(rs);
con.close();//关闭连接
rowSet.last();
intm=rowSet.getRow();//总行数
intn=pageSize;
pageAllCount=((m%n)==0)?
(m/n):
(m/n+1);
}
catch(Exceptionexp){}
}
publicintgetPageSize()
{returnpageSize;
}
publicintgetPageAllCount()
{returnpageAllCount;
}
publicvoidsetShowPage(intn)
{showPage=n;
}
publicintgetShowPage()
{returnshowPage;
}
publicStringBuffergetPresentPageResult()
{if(showPage>pageAllCount)
showPage=1;
if(showPage<=0)
showPage=pageAllCount;
presentPageResult=show(showPage);
returnpresentPageResult;
}
publicStringBuffershow(intpage)
{StringBufferstr=newStringBuffer();
str.append("
str.append("
for(inti=0;i<字段个数;i++)
{str.append("
}
str.append("
try{rowSet.absolute((page-1)*pageSize+1);
for(inti=1;i<=pageSize;i++)
{str.append("
for(intk=1;k<=字段个数;k++)
{str.append("
}
str.append("
rowSet.next();
}
}
catch(SQLExceptionexp){}
str.append("");
returnstr;
}
publicvoidsetDatabaseName(Strings)
{databaseName=s.trim();
}
publicStringgetDatabaseName()
{returndatabaseName;
}
publicvoidsetTableName(Strings)
{tableName=s.trim();
}
publicStringgetTableName()
{returntableName;
}
publicvoidsetPassword(Strings)
{password=s.trim();;
}
publicvoidsetUser(Strings)
{user=s.trim();
}
publicStringgetUser()
{returnuser;
}
}
showByPage.jsp
<%@pagecontentType="tex
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 分页 技术