昨天看到一篇《 纯JSP分页代码 》的文章,是用于mysql,稍微修改下,用于Sqlserver2005/2008,没有异常处理。没有考虑性能等。
现将代码贴出,以供初学者参考:
注:邀月使用环境Eclipse 3.4.2+Tomcat 6.18+Sqlserver2005 sp3调试成功。
页面pagelistDemo.jsp内容:
<!-- {cps..0}--> <% @pagelanguage = " java " contentType = " text/html;charset=UTF-8 "
pageEncoding = " UTF-8 " %>
<%
// 连接字符串
Stringurl = " jdbc:sqlserver:// " + Globals.Server
+ " ;databaseName= " + Globals.DbName;
Class.forName(Globals.driverName).newInstance();
Connectionconnection = DriverManager.getConnection(url,
Globals.username,Globals.pwd);
Statementstatement = connection.createStatement();
// 每页显示记录数
int PageSize = 10 ;
int StartRow = 0 ; // 开始显示记录的编号
int PageNo = 0 ; // 需要显示的页数
int CounterStart = 0 ; // 每页页码的初始值
int CounterEnd = 0 ; // 显示页码的最大值
int RecordCount = 0 ; // 总记录数;
int MaxPage = 0 ; // 总页数
int PrevStart = 0 ; // 前一页
int NextPage = 0 ; // 下一页
int LastRec = 0 ;
int LastStartRecord = 0 ; // 最后一页开始显示记录的编号
// 获取需要显示的页数,由用户提交
if (request.getParameter( " PageNo " ) == null ) { // 如果为空,则表示第1页
if (StartRow == 0 ) {
PageNo = StartRow + 1 ; // 设定为1
}
} else {
PageNo = Integer.parseInt(request.getParameter( " PageNo " )); // 获得用户提交的页数
StartRow = (PageNo - 1 ) * PageSize; // 获得开始显示的记录编号
}
// 因为显示页码的数量是动态变化的,假如总共有一百页,则不可能同时显示100个链接。而是根据当前的页数显示
// 一定数量的页面链接
// 设置显示页码的初始值!!
if (PageNo % PageSize == 0 ) {
CounterStart = PageNo - (PageSize - 1 );
} else {
CounterStart = PageNo - (PageNo % PageSize) + 1 ;
}
CounterEnd = CounterStart + (PageSize - 1 );
%>
<! DOCTYPEhtmlPUBLIC " -//W3C//DTDHTML4.01Transitional//EN " " http://www.w3.org/TR/html4/loose.dtd " >
<% @page import = " net.data.util.Globals " %>
<% @page import = " java.sql.ResultSet " %>
<% @page import = " java.sql.DriverManager " %>
<% @page import = " java.sql.Connection " %>
<% @page import = " java.sql.Statement " %>
< element >
< html >
< head >
< metahttp - equiv = " Content-Type " content = " text/html;charset=UTF-8 " >
< title > 分页显示记录 </ title >
< linkrel = " stylesheet " href = " style.css " type = " text/css " >
</ head >
<%
// 获取总记录数
ResultSetrs = statement
.executeQuery( " selectcount(P_ID)fromproduct " );
rs.next();
RecordCount = rs.getInt( 1 );
// 取特定页数的数据
StringstrColumn = " P_ID,P_Name,LoginID,modTime,P_SingleIntro " ;
StringstrTable = " Product " ;
StringstrSort = " p_IDdesc " ;
StringPKID = " P_ID " ;
StringstrSql = "" ;
StringstrWhere = "" ;
StringSTART_ID = Integer.toString((PageNo - 1 ) * PageSize + 1 );
StringEND_ID = Integer.toString(PageNo * PageSize);
strSql = " SELECT " + strColumn
+ " FROM(SELECTROW_NUMBER()OVER(ORDERBY " + strSort
+ " )ASrownum, " + strColumn + " FROM " + strTable
+ " WITH(NOLOCK) " + strWhere
+ " )ASDWHERErownumBETWEEN " + START_ID + " AND "
+ END_ID + " ORDERBY " + strSort;
rs = statement.executeQuery(strSql);
// 获取总页数
MaxPage = RecordCount % PageSize;
if (RecordCount % PageSize == 0 ) {
MaxPage = RecordCount / PageSize;
} else {
MaxPage = RecordCount / PageSize + 1 ;
}
%>
< body class = " UsePageBg " >
< tablewidth = " 100% " border = " 0 " class = " InternalHeader " >
< tr >
< tdwidth = " 24% " >< fontsize = 4 > 分页显示记录 </ font ></ td >
< tdwidth = " 76% " >< fontsize = 4 ><%= " 总共 " + RecordCount + " 条记录-当前页: " + PageNo + " / "
+ MaxPage %></ font ></ td >
</ tr >
</ table >
< br >
< tablewidth = " 100% " border = " 0 " class = " NormalTableTwo " >
< tr >
< td class = " InternalHeader " > 记录序号 </ td >
< td class = " InternalHeader " > 数据标识号 </ td >
< td class = " InternalHeader " > 产品名称 </ td >
< td class = " InternalHeader " > 作者 </ td >
< td class = " InternalHeader " > 提交时间 </ td >
< td class = " InternalHeader " > 简介 </ td >
</ tr >
<%
long i = 1 ;
while (rs.next()) {
long bil = i + (PageNo - 1 ) * PageSize;
%>
< tr >
< td class = " NormalFieldTwo " ><%= bil %></ td >
< td class = " NormalFieldTwo " ><%= rs.getString( 1 ) %></ td >
< td class = " NormalFieldTwo " ><%= rs.getString( 2 ) %></ td >
< td class = " NormalFieldTwo " ><%= rs.getString( 3 ) %></ td >
< td class = " NormalFieldTwo " ><%= rs.getString( 4 ) %></ td >
< td class = " NormalFieldTwo " ><%= rs.getString( 5 ) %></ td >
</ tr >
<%
i ++ ;
}
%>
</ table >
< br >
< tablewidth = " 100% " border = " 0 " class = " InternalHeader " >
< tr >
< td >
< divalign = " center " >
<%
out.print( " <fontsize=4> " );
// 显示第一页或者前一页的链接
// 如果当前页不是第1页,则显示第一页和前一页的链接
if (PageNo != 1 ) {
PrevStart = PageNo - 1 ;
out.print( " <ahref=pagelistDemo.jsp?PageNo=1>第一页</a>: " );
out.print( " <ahref=pagelistDemo.jsp?PageNo= " + PrevStart
+ " >前一页</a> " );
}
out.print( " [ " );
// 打印需要显示的页码
for ( int c = CounterStart;c <= CounterEnd;c ++ ) {
if (c < MaxPage) {
if (c == PageNo) {
if (c % PageSize == 0 ) {
out.print(c);
} else {
out.print(c + " , " );
}
} else if (c % PageSize == 0 ) {
out.print( " <ahref=pagelistDemo.jsp?PageNo= " + c + " > " + c
+ " </a> " );
} else {
out.print( " <ahref=pagelistDemo.jsp?PageNo= " + c + " > " + c
+ " </a>, " );
}
} else {
if (PageNo == MaxPage) {
out.print(c);
break ;
} else {
out.print( " <ahref=pagelistDemo.jsp?PageNo= " + c + " > " + c
+ " </a> " );
break ;
}
}
}
out.print( " ] " );
;
if (PageNo < MaxPage) { // 如果当前页不是最后一页,则显示下一页链接
NextPage = PageNo + 1 ;
out.print( " <ahref=pagelistDemo.jsp?PageNo= " + NextPage
+ " >下一页</a> " );
}
// 同时如果当前页不是最后一页,要显示最后一页的链接
if (PageNo < MaxPage) {
LastRec = RecordCount % PageSize;
if (LastRec == 0 ) {
LastStartRecord = RecordCount - PageSize;
} else {
LastStartRecord = RecordCount - LastRec;
}
out.print( " : " );
out.print( " <ahref=pagelistDemo.jsp?PageNo= " + MaxPage
+ " >最后一页</a> " );
}
out.print( " </font> " );
%>
</ div >
</ td >
</ tr >
</ table >
<%
rs.close();
statement.close();
connection.close();
%>
</ body >
</ html >
用到的类文件:Globals.java
<!-- {cps..1}--> /***/ /**
*
*/
package net.data.util;
/***/ /**
* @author Administrator
*
*/
public class Globals {
public static Stringusername = " sa " ;
public static Stringpwd = " sa " ;
public static StringDbName = " TestData " ;
// publicstaticStringServer="192.168.30.6";
public static StringServer = " 192.168.44.2:1433 " ;
public static StringPort = " 1433 " ;
public static StringdriverName = " com.microsoft.sqlserver.jdbc.SQLServerDriver " ;
// publicstaticStringconnectionURL{
//
// return"jdbc:sqlserver: // "+Server+":"+Port+";databaseName="+DbName;
// }
}
效果如图:
更多文章、技术交流、商务合作、联系博主
微信扫码或搜索:z360901061
微信扫一扫加我为好友
QQ号联系: 360901061
您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】元