JSP中分页显示的问题
问题是: 数据的内容不随着页码的变化而变化, 老是第一页. (数据内容足够多, 页数的参数页传递到位.)<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.lang.Math.*"%>
<jsp:useBean id="connDbBean" scope="page" class="bean.javaBeanJdbc" />
<%! ResultSet rs=null;
String sql="";
int PageSize=6;
int Page=1;
int totalPage=1;
String str="";
public String ShowOnePage(ResultSet rs, int Page,int PageSize){
str="";
try{
rs.absolute((Page-1)*PageSize+1);
}catch(SQLException e){
}
for(int iPage=1; iPage<=PageSize; iPage++){
str+=RsToGbook(rs);
try{
if(!rs.next()) break;
}catch(Exception e){}
}
return str;
}
public String RsToGbook(ResultSet rs){
String tt="";
try{
tt+="<tr>"+rs.getString("name")+"</tr><br>";
tt+="<tr>"+rs.getString("title")+"</tr><br>";
tt+="<tr>"+rs.getString("content")+"</tr>***<br>";;
}catch(SQLException e){}
return tt;
}
%>
<% sql="select * from leavewords";
try{
rs=connDbBean.executeQuery(sql);
}catch(Exception e){
out.println("访问数据库出错");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>分页浏览数据库的技巧</title>
</head>
<body>
<br>
<hr>
<center>
jsp中的分页控制:<br>
<table bgcolor="gray">
<th>作者名字</th>
<th>标题</th>
<th>内容</th>
<br><br>
<%
ResultSet rsTmp=connDbBean.executeQuery("select count(*) as mycount from leavewords");
rsTmp.next();
int totalrecord=rsTmp.getInt("mycount");
if(totalrecord % PageSize==0) totalPage=totalrecord/PageSize;
else totalPage=(int)Math.floor(totalrecord/PageSize)+1;
if(totalPage==0) totalPage=1;
if(request.getParameter("Page")==null || request.getParameter("Page").equals(""))
Page=1;
if(Page<1) Page=1;
if(Page>totalPage) Page=totalPage;
%>
</table>
<form method="post" action="divPage.jsp">
<%
String pages="";
if(request.getParameter("Page")!=null || !request.getParameter("Page").equals(""))
{ pages=request.getParameter("Page");
Page=Integer.parseInt(pages);
}
out.println("<br>");
out.println("Page="+Page);
out.println("<br>");
out.println(ShowOnePage(rs,Page,PageSize));
out.println("<br><br>");
if(Page!=0){
out.println("<a href=divPage.jsp?Page=1>第一页</a>");
out.println("<a href=divPage.jsp?Page="+(Page-1)+">上一页</a>");
}
if(Page!=totalPage){
out.println("<a href=divPage.jsp?Page="+(Page+1)+">下一页</a>");
out.println("<a href=divPage.jsp?Page="+totalPage+">最后一页</a>");
}
%><br>
<p>输入页数:</p>
<input type="text" name="Page" size="3">页数:<%=Page%>/<%=totalPage%>
</form>
</center>
</body>
</html>
问题是: 数据的内容不随着页码的变化而变化, 老是第一页. (数据内容足够多, 页数的参数页传递到位.)