这是我从网上摘抄的jsp分页代码,问题是我 分页出来的确实显示全部记录,打个比方,我一个有10条记录,第一页
它就会显示10条记录,第二页还是显示一摸一样的10条记录,我很郁闷,代码如下!
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><%@ page import="java.sql.*" %>
<%@ page import="java.lang.Integer" %>
<%@ page import="java.lang.Math" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<title>无标题文档</title>
</head><body>
<%
int i;
int ipage=5; //分页单位
int allpage; //总页数
int pages; //接受的页码变量
int cpage=1; //当前页
int spage; //开始页
int allCol=0;
Vector v=null;//载入JDBC驱动程序
Driver myDriver=(Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
//创建一个新连接
Connection myConn=DriverManager.getConnection("jdbc:odbc:first","","");
//创建一个Statement对象用来将查询和命令发送给数据库
Statement myStatement=myConn.createStatement();
//每页显示记录数
int PageSize = 5;
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 = myStatement.executeQuery("select count(*) from product" );
rs.next();
RecordCount = rs.getInt(1); rs = myStatement.executeQuery("SELECT name,context FROM product ORDER BY id DESC ");//获取总页数
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>
</tr><%
int j = 1;
while (rs.next()) {
int bil = j + (PageNo-1)*PageSize;
%>
<tr>
<td class="NormalFieldTwo" ><%=bil %></td>
<td class="NormalFieldTwo" ><%=rs.getString(1)%></td>
<td class="NormalFieldTwo" ><%=rs.getString(2)%></td>
</tr>
<%
j++;
}%>
</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=3.jsp?PageNo=1>第一页 </a>: ");
out.print("<a href=3.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=3.jsp?PageNo="+c+">"+c+"</a>");
}else{
out.print("<a href=3.jsp?PageNo="+c+">"+c+"</a> ,");
}
}else{
if(PageNo == MaxPage){
out.print(c);
break;
}else{
out.print("<a href=3.jsp?PageNo="+c+">"+c+"</a>");
break;
}
}
}out.print("]");;if(PageNo < MaxPage){ //如果当前页不是最后一页,则显示下一页链接
NextPage = PageNo + 1;
out.print("<a href=3.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=3.jsp?PageNo="+MaxPage+">最后一页</a>");
}
out.print("</font>");
%>
</div>
</td>
</tr>
</table>
<%
rs.close();
myStatement.close();
myConn.close();
%>
</body>
</html>
它就会显示10条记录,第二页还是显示一摸一样的10条记录,我很郁闷,代码如下!
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><%@ page import="java.sql.*" %>
<%@ page import="java.lang.Integer" %>
<%@ page import="java.lang.Math" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<title>无标题文档</title>
</head><body>
<%
int i;
int ipage=5; //分页单位
int allpage; //总页数
int pages; //接受的页码变量
int cpage=1; //当前页
int spage; //开始页
int allCol=0;
Vector v=null;//载入JDBC驱动程序
Driver myDriver=(Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
//创建一个新连接
Connection myConn=DriverManager.getConnection("jdbc:odbc:first","","");
//创建一个Statement对象用来将查询和命令发送给数据库
Statement myStatement=myConn.createStatement();
//每页显示记录数
int PageSize = 5;
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 = myStatement.executeQuery("select count(*) from product" );
rs.next();
RecordCount = rs.getInt(1); rs = myStatement.executeQuery("SELECT name,context FROM product ORDER BY id DESC ");//获取总页数
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>
</tr><%
int j = 1;
while (rs.next()) {
int bil = j + (PageNo-1)*PageSize;
%>
<tr>
<td class="NormalFieldTwo" ><%=bil %></td>
<td class="NormalFieldTwo" ><%=rs.getString(1)%></td>
<td class="NormalFieldTwo" ><%=rs.getString(2)%></td>
</tr>
<%
j++;
}%>
</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=3.jsp?PageNo=1>第一页 </a>: ");
out.print("<a href=3.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=3.jsp?PageNo="+c+">"+c+"</a>");
}else{
out.print("<a href=3.jsp?PageNo="+c+">"+c+"</a> ,");
}
}else{
if(PageNo == MaxPage){
out.print(c);
break;
}else{
out.print("<a href=3.jsp?PageNo="+c+">"+c+"</a>");
break;
}
}
}out.print("]");;if(PageNo < MaxPage){ //如果当前页不是最后一页,则显示下一页链接
NextPage = PageNo + 1;
out.print("<a href=3.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=3.jsp?PageNo="+MaxPage+">最后一页</a>");
}
out.print("</font>");
%>
</div>
</td>
</tr>
</table>
<%
rs.close();
myStatement.close();
myConn.close();
%>
</body>
</html>
又是JSP页面写JAVA代码 你调试比你抄代码更痛苦
你说的问题是在这里,你没有对rs进行控制啊,
<%
int j = 1;
while (rs.next()) {
int bil = j + (PageNo-1)*PageSize;
%>
<tr>
<td class="NormalFieldTwo" ><%=bil %></td>
<td class="NormalFieldTwo" ><%=rs.getString(1)%></td>
<td class="NormalFieldTwo" ><%=rs.getString(2)%></td>
</tr>
<%
j++;
}%>
看到了没,你只是循环把记录集里所有的东西都打印出来了这样,结果当然是你所描述的那样了
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@ page import="java.util.*" isELIgnored="false" %>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="tkl.download.dao.HibernateDAO"%>
<%@page import="tkl.download.dao.MessageBean"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>留言列表</title>
<style type="text/css">
<!--
.zong {
float: left;
width: 580px;
}body {
margin: 0px;
padding: 0px;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}form {
margin: 0px;
padding: 0px;
}.lylb {
float: left;
width: 600px;
}.lylb1 {
float: left;
width: 600px;
border: 1px solid #CCCCCC;
padding-top: 5px;
padding-bottom: 5px;
}.lylb2 {
float: left;
width: 600px;
border: 1px solid #CCCCCC;
padding-top: 10px;
padding-bottom: 10px;
}.lbnum1 {
float: left;
width: 115px;
text-align: center;
}.lbnum2 {
float: left;
width: 393px;
}.lbnum3 {
float: left;
width: 90px;
text-align: center;
}.li {
float: left;
line-height: 24px;
width: 598px;
}.lylb3 {
float: left;
width: 600px;
padding-top: 22px;
padding-bottom: 22px;
text-align: center;
}.a333 {
COLOR: #333333
}.a333 A:link {
COLOR: #333333;
TEXT-DECORATION: none
}.a333 A:visited {
COLOR: #333333;
TEXT-DECORATION: none
}.a333 A:hover {
COLOR: #f16c03;
TEXT-DECORATION: none
}.a333 A:active {
COLOR: #f16c03;
TEXT-DECORATION: none
}.red {
COLOR: #ff3300
}.red A:link {
COLOR: #ff3300;
TEXT-DECORATION: none
}.red A:visited {
COLOR: #ff3300;
TEXT-DECORATION: none
}.red A:hover {
LEFT: 1px;
COLOR: #ce1211;
POSITION: relative;
TOP: 1px;
TEXT-DECORATION: none
}.red A:active {
LEFT: 1px;
COLOR: #ce1211;
POSITION: relative;
TOP: 1px;
TEXT-DECORATION: none
}
-->
</style>
</head>
<body>${pageIndex}
<div class="lylb">
<div class="lylb1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="20%" align="center">时间</td>
<td width="34%" align="center">留言主题</td>
<td width="32%" align="center"> </td>
<td width="14%" align="center">留言人</td>
</tr>
</table>
</div><%
//<c:set var="maxRows" value="2" />
//<c:set var="pageCount" value="${totalSize % maxRows==0?(totalSize/maxRows+1):(totalSize/maxRows)}"/>
int totalSize = HibernateDAO.getInstance().getReplayedMessgeCount();
int maxRows = 10;
int pageCount = 0;
if(totalSize % maxRows==0)
{
pageCount =totalSize/maxRows ;
}
else
{
pageCount = totalSize/maxRows + 1;
}
//define max rows per page
pageContext.setAttribute("totalSize",totalSize);
pageContext.setAttribute("maxRows",maxRows);
pageContext.setAttribute("pageCount",pageCount);
%>
<c:if test="${totalSize>0}">
<c:set var="pageIndex" value="${empty param.pageIndex? 0 : param.pageIndex}"/>
<%
Object obj = pageContext.getAttribute("pageIndex");
List<MessageBean> queryresults = null;
try
{
int pageIndex = Integer.valueOf(obj.toString());
int startrow = pageIndex * maxRows;
queryresults = HibernateDAO.getInstance().retievePagedReplayedMessge(startrow,maxRows);
}catch(Exception ex)
{
queryresults = null;
}
pageContext.setAttribute("queryresults",queryresults);
%>
<c:choose>
<c:when test="${empty queryresults}">
<c:out value="选择的页面号码不对!"></c:out>
</c:when>
<c:otherwise>
<div class="lylb2 a333">
<c:set value="1" var="i"/>
<c:forEach items="${queryresults}" var="info">
<c:choose>
<c:when test="${i%2==0}" >
<div class="li" style="background: #FFFFFF">
<div class="lbnum1" style="background: #FFFFFF">${fn:substring(info.leaveTime,0,10)}</div>
<div class="lbnum2 red" style="background: #FFFFFF"><a href="messagesee.jsp?msgId=${info.msgId}"/>${fn:substring(info.leaveTitle,0,29)}</a>
</div>
<div class="lbnum3" style="background: #FFFFFF">${info.leaveName==null||info.leaveName==''?'游客':info.leaveName}</div>
</div>
</c:when>
<c:otherwise>
<div class="li" style="background: #EFEFF8">
<div class="lbnum1" style="background: #EFEFF8">${fn:substring(info.leaveTime,0,10)}</div>
<div class="lbnum2 red" style="background: #EFEFF8"><a href="messagesee.jsp?msgId=${info.msgId}"/>${fn:substring(info.leaveTitle,0,29)}</a>
</div>
<div class="lbnum3" style="background: #EFEFF8">${info.leaveName==null||info.leaveName==''?'游客':info.leaveName}</div>
</div>
</c:otherwise>
</c:choose>
<c:set value="${i + 1}" var="i"/>
</c:forEach>
</div> <div class="lylb3">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="74%" align="middle">
<c:if test="${pageIndex>0}">
<a class = "red" href="?pageIndex=0">首页</a>
<a class = "red" href="?pageIndex=${param.pageIndex-1}">上一页</a>
</c:if>
<c:if test="${pageCount>1}">
<c:forEach begin="0" end="${pageCount-1}" step="1" var="pageNum">
<c:if test="${pageIndex!=pageNum}">
<a class = "red" href="?pageIndex=${pageNum}">[${pageNum+1}]</a>
</c:if>
<c:if test="${pageIndex==pageNum}">
<strong>${pageNum+1}</strong>
</c:if>
</c:forEach>
</c:if>
<c:if test="${pageIndex<pageCount-1}">
<a class = "red" href="?pageIndex=${param.pageIndex+1}">下一页</a>
<a class = "red" href="?pageIndex=${pageCount-1}">末页</a>
</c:if> </tr>
</table>
</div>
</c:otherwise>
</c:choose>
</c:if></div>
</body>
</html>
要是楼主用别的数据量 分页sql得稍微调整一下
http://wenku.baidu.com/view/e465debbfd0a79563c1e72a3.html