JSP分頁教程
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>設計家園jsp教程</title>
<%
//連接字符串
Class.forName("org.gjt.mm.mysql.Driver");
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/sms","sms","sms");
Statement statement = connection.createStatement();
LinuxMySql lm = new LinuxMySql();
//每頁顯示記錄數
int PageSize = 8;
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);
%>
<html>
<head>
<title>分頁顯示記錄</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<%
//獲取總記錄數
ResultSet rs = statement.executeQuery("select count(*) from sm_asend" );
rs.next();
RecordCount = rs.getInt(1);
rs = statement.executeQuery("SELECT sm_da,sm_st,sm_ud FROM sm_asend ORDER BY sm_no DESC LIMIT "
+StartRow+", "+PageSize);
//獲取總頁數
MaxPage = RecordCount % PageSize;
if(RecordCount % PageSize == 0){
MaxPage = RecordCount / PageSize;
}else{
MaxPage = RecordCount/PageSize+1;
}
%>
<body class="UsePageBg">
<table width="100%" border="0" class="InternalHeader">
<tr>
<td width="24%"><font size=4>分頁顯示記錄</font></td>
<td width="76%">
<font size=4><%="總共"+RecordCount+"條記錄 - 當前頁:"+PageNo+"/"+MaxPage %></font>
</td>
</tr>
</table>
<br>
<table width="100%" border="0" class="NormalTableTwo">
<tr>
<td class="InternalHeader" >對方電話</td>
<td class="InternalHeader" >發送日期</td>
<td class="InternalHeader" >發送內容</td>
</tr>
<%
int i = 1;
while (rs.next()) {
%>
<tr>
<td class="NormalFieldTwo" ><%=lm.split86(rs.getString(1))%></td>
<td class="NormalFieldTwo" ><%=lm.dateLog(rs.getString(2))%></td>
<td class="NormalFieldTwo" ><%=lm.unicode2String(rs.getString(3))%></td>
</tr>
<%
i++;
}%>
</table>
<br>
<table width="100%" border="0" class="InternalHeader">
<tr>
<td><div align="center">
<%
out.print("<font size=4>");
//顯示第一頁或者前一頁的鏈接
//如果當前頁不是第1頁,則顯示第一頁和前一頁的鏈接
if(PageNo != 1){
PrevStart = PageNo - 1;
out.print("<a href=fenye.jsp?PageNo=1>第一頁 </a>: ");
out.print("<a href=fenye.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("<a href=fenye.jsp?PageNo="+c+">"+c+"</a>");
}else{
out.print("<a href=fenye.jsp?PageNo="+c+">"+c+"</a> ,");
}
}else{
if(PageNo == MaxPage){
out.print(c);
break;
}else{
out.print("<a href=fenye.jsp?PageNo="+c+">"+c+"</a>");
break;
}
}
}
out.print("]");;
if(PageNo < MaxPage){ //如果當前頁不是最后一頁,則顯示下一頁鏈接
NextPage = PageNo + 1;
out.print("<a href=fenye.jsp?PageNo="+NextPage+">下一頁</a>");
}
//同時如果當前頁不是最后一頁,要顯示最后一頁的鏈接
if(PageNo < MaxPage){
LastRec = RecordCount % PageSize;
if(LastRec == 0){
LastStartRecord = RecordCount - PageSize;
}
else{
LastStartRecord = RecordCount - LastRec;
}
out.print(":");
out.print("<a href=fenye.jsp?PageNo="+MaxPage+">最后一頁</a>");
}
out.print("</font>");
%>
</div>
</td>
</tr>
</table>
<%
rs.close();
statement.close();
connection.close();
%>
</body>
</html>
新聞熱點
疑難解答