已验证可以执行的代码。
初学,谨作为个人学习记录。
请大家给出意见或建议。谢谢!
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
//驱动程序名,比较旧了,如果你用mysql5,自己改。
String driverName="com.mysql.jdbc.Driver";
String userName="root";//数据库用户名
String userPasswd="12345";//密码
String dbName="ziyuan";//数据库名
String tableName="zyd_user"; //表名
//连接字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName(driverName).newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
//每页显示记录数
int PageSize = 4; //每页显示记录数
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">
<style type="text/css">
<!--
.STYLE13 {font-family: "幼圆"}
.STYLE18 {font-size: 14px}
-->
</style>
</head>
<%
//获取总记录数
ResultSet rs = statement.executeQuery("select count(*) from zyd_user" );
rs.next();
RecordCount = rs.getInt(1);
//rs = statement.executeQuery("SELECT usercode,username,password,comcode,flag_level,flag_status FROM zyd_user ORDER BY usercode DESC LIMIT "+StartRow+", "+PageSize);
rs = statement.executeQuery("SELECT usercode,username,password FROM zyd_user ORDER BY usercode LIMIT "+StartRow+", "+PageSize);
//获取总页数
MaxPage = RecordCount % PageSize;
if(RecordCount % PageSize == 0){
MaxPage = RecordCount / PageSize;
}else{
MaxPage = RecordCount/PageSize+1;
}
%>
<body class="UsePageBg STYLE13 STYLE18">
<table width="100%" border="0" bordercolor="#000000" class="InternalHeader">
<tr>
<td width="24%"><span class="STYLE13">分页显示记录</span></td>
<td width="76%">
<span class="STYLE13"><%="总共"+RecordCount+"条记录 - 当前页:"+PageNo+"/"+MaxPage %> </span></td>
</tr>
</table>
<br>
<table width="100%" border="0" bordercolor="#000000" class="NormalTableTwo">
<tr>
<td width="18%" align="center" valign="middle" bordercolor="#000000" class="InternalHeader STYLE13 " >代码</td>
<td width="20%" align="center" valign="middle" bordercolor="#000000" class="InternalHeader STYLE13 " >姓名</td>
</tr>
<%
int i = 1;
while (rs.next()) {
int bil = i + (PageNo-1)*PageSize;
%>
<tr>
<td align="center" valign="middle" bordercolor="#000000" class="NormalFieldTwo" ><span class="STYLE13"><%=rs.getString(1)%></span></td>
<td align="center" valign="middle" bordercolor="#000000" class="NormalFieldTwo" ><span class="STYLE13"><%=rs.getString(2)%></span></td>
</tr>
<%
i++;
}%>
</table>
<br>
<table width="100%" border="0" class="InternalHeader">
<tr>
<td class="STYLE13"><div align="center">
<%
out.print("<font size=4>");
//显示第一页或者前一页的链接
//如果当前页不是第1页,则显示第一页和前一页的链接
if(PageNo != 1){
PrevStart = PageNo - 1;
out.print("<a href=main_data.jsp?PageNo=1>第一页 </a>: ");
out.print("<a href=main_data.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=main_data.jsp?PageNo="+c+">"+c+"</a>");
}else{
out.print("<a href=main_data.jsp?PageNo="+c+">"+c+"</a> ,");
}
}else{
if(PageNo == MaxPage){
out.print(c);
break;
}else{
out.print("<a href=main_data.jsp?PageNo="+c+">"+c+"</a>");
break;
}
}
}
out.print("]");;
if(PageNo < MaxPage){ //如果当前页不是最后一页,则显示下一页链接
NextPage = PageNo + 1;
out.print("<a href=main_data.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=main_data.jsp?PageNo="+MaxPage+">最后一页</a>");
}
out.print("</font>");
%>
</div></td>
</tr>
</table>
<span class="STYLE13">
<%
rs.close();
statement.close();
connection.close();
%>
</span>
</body>
</html>
</style>