谁可以帮我送100分一个简单的分页问题!在线!!急

刘云涛 2005-05-19 04:11:23
我在分页显示的过程,BEAN是参考别人的改了一下,现在的问题是,不分页
在页面下已经显示有几页了,多少个记录和我设的是对应的,可页面上的记录却显示所有的,就是不分页:
点上一页下一页,下边的值是变的,可页面显示的数据不动
总计31条记录,【共7页】【条5/页】 当前第4页(列出第16到第20条) 第一页 上一页 下一页 尾页 转到页

源码
--------------------------------------------------------
分页页面:
<%@ page contentType="text/html;charset=gb2312"%>
<jsp:useBean id="MydbCnn" scope="page" class="Database.ConnDataBase" />
<jsp:useBean id="MyStr" scope="page" class="strFormat.strFormat" />
<jsp:useBean id="Mypage" scope="page" class="Database.Lpage" />
<%@ page language ="java" import="java.sql.*"%>
<%
ResultSet AdoRst1 = null;
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<TABLE cellSpacing=1 cellPadding=4 width="100%" border=0>
<TR background="Img/top3.gif">
<TD width="4%"> </TD>
<TD width="4%"> </TD>
<TD width="47%" >标题</TD>
<TD width="14%" noWrap>作者</TD>
<TD width="6%" noWrap>回复</TD>
<TD width="6%" noWrap>查看</TD>
<TD width="19%" >最后发表</TD>
</TR>
<%
String strnowPage = "";
strnowPage = request.getParameter("pages");
Mypage.setPages(5);

AdoRst1 = Mypage.setQuerysql("bbs","","test.jsp",strnowPage);
while(AdoRst1.next()){
%>
<TR>
<TD bgColor=#fffbf7><IMG src="Img/red_folder.gif" border=0></TD>
<TD bgColor=#ffffff> <img src="<%=AdoRst1.getString("Currentemot")%>"> </TD>
<TD onmouseover="this.style.backgroundColor='#ffffff'" onmouseout="this.style.backgroundColor='#FFFBF7'" bgColor=#fffbf7>
<IMG src="Img/image.gif" border=0> <a href="bbsView.jsp?id=<%=AdoRst1.getString("id")%>&BBSsId=<%=AdoRst1.getString("BBSsId")%>&BBSpId=<%=AdoRst1.getString("BBSpId")%>" target="_blank"><%=AdoRst1.getString("subjectTitle")%> </a><IMG src="Img/firstnew.gif" border=0></TD>
<TD bgColor=#ffffff><%=AdoRst1.getString("SubUser")%>(<%=AdoRst1.getString("ptime")%>)</TD>
<TD bgColor=#fffbf7><%=AdoRst1.getString("hfnum")%></TD>
<TD bgColor=#ffffff><%=AdoRst1.getString("hit")%></TD>
<TD bgColor=#fffbf7><%=AdoRst1.getString("lasthf")%>
<FONT class=smalltxt1><IMG src="Img/lastpost.gif" border=0></FONT></TD>
</TR>
<%}AdoRst1.close();%>
<tr align='center'><td colspan='7'><%=Mypage.Showpages()%></td></td>
</TABLE>
</body>
</html>

---------------------------------------
分页bean:

-----------------------------------------------

package Database;

/**
* Title: 分页主程序
* Description:
* Copyright: Copyright (c) 2005
* Company:
* @author Administrator
* @version 1.0
*/
import Database.ConnDataBase;
import java.sql.*;

//import java.util.*;
//import java.io.*;
//import javax.servlet.*;
//import javax.servlet.http.*;

public class Lpage{

ResultSet adoRst=null; //初始化总记录数Rs变量
ResultSet PageAdoRst=null; //初始化分页时每页的记录集数Rs变量

private int intRecordCount=0; //总记录数
public int intPageSize; //即每页显示的记录总数
public int intPageCount; //总页数
public int intCurrentPage=0; //当前页数

//分页参数
private String Countsql=null;
private String Pagisql=null;
private String str=null;
private String str_where=null;
private String str_parameter="";

//public static int pages_n=1; //传分页参数值
//初始化当前页intPage变量,以准确便获取当前页,即获取当前页的具体页号。
private String nowPage;
//当前的地址栏的文件,即具体jsp文件。
private String HttpFile;

//连接数据库
ConnDataBase objCnn = new ConnDataBase();

//接收传分页参数
public void setPages(int n){
intPageSize = n;
}

/*功能:接收参数组织SQL语句
*str_table :分页显示的表名
*str_where:分页的where条件
*httpfile :具体jsp文件
*pages :获取地址栏传过来的pages参数
*/
public ResultSet setQuerysql(String str_table,String str_where, String httpfile,String pages)throws SQLException
{
ResultSet r=null;
this.nowPage=pages;
this.HttpFile=httpfile;//分页文件名
Countsql="select count(*) from "+str_table+" "+str_where;
Pagisql="select * from "+str_table+" "+str_where;
try{
r=querySql(Countsql,Pagisql);
}
catch(SQLException _ex){
System.out.println(_ex);
}
return r;
}

/*功能:接收参数进行首尾页判断
*Countsql:总记录的Query字符串。[形式为select count(*) from tablename]
*Pagisql :要分页的Query字符串。[形式为select * from tablename where ...]
*request :参数传递过程中的变量。[用来控制翻页时的pages变量]
*/

//,HttpServletRequest request
public ResultSet querySql(String Countsql,String Pagisql)throws SQLException{

//获取当前文件名。
//HttpFile=request.getRequestURI();
//获取当前页,将数值赋予intPage变量。[分页栏中必须要有pages参数]
//nowPage=request.getParameter("pages");//由参数HttpServletRequest request传递而来

if (nowPage==null){
intCurrentPage=1;
}
else{
intCurrentPage=Integer.parseInt(nowPage);
if (intCurrentPage<1)
intCurrentPage=1;
}//end else

//获取总记录数的结果集。
adoRst=objCnn.executeQuery(Countsql);

if (adoRst.next()){
intRecordCount=adoRst.getInt(1); //获取第一个字段的整型
}

//获取总页数。
intPageCount = (intRecordCount+intPageSize-1)/intPageSize;

//如果当前页大于总页数,则当前页等于总页数。//=最后一页
if (intCurrentPage>intPageCount){
intCurrentPage=intPageCount;
}

//关闭总主题数的数据集。
adoRst.close();

//获取执行分页的结果集。
PageAdoRst=objCnn.executeQuery(Pagisql);
return PageAdoRst;
}//end querySql function.

//获取记录总数。
public int getRecordCount(){
return intRecordCount;
}

//获取总页数。
public int getPageCount(){
return intPageCount;
}

//获取当前页数。
public int getCurrentPage(){
return intCurrentPage;
}

//设置分页参数
public void setPfoot(String str){
this.str_parameter+=str;
}

//分页栏函数。
public String Showpages() {
String str = "";
int next, prev;
prev=intCurrentPage-1;
next=intCurrentPage+1;
str +="<font style='font-size: 9pt'>总计<font color='red'>"+getRecordCount()+"</font>条记录,"+"【共<font color='red'>"+getPageCount()+"</font>页】";
str +="【条"+intPageSize+"/页】 当前第<font color='red'>"+getCurrentPage()+"</font>页(列出第"+((intPageSize*getCurrentPage()+1)-intPageSize)+"到第"+(getCurrentPage()*intPageSize)+"条)     ";
//getIntPage()*intPageSize
if(intCurrentPage>1)
str += " <A href=" + HttpFile + "?pages=1"+str_parameter+">第一页</A> ";
else str += " 第一页 ";

if(intCurrentPage>1)
str += " <A href=" + HttpFile + "?pages=" + prev +str_parameter+ ">上一页</A> ";
else str += " 上一页 ";

if(intCurrentPage<intPageCount)
str += " <A href=" + HttpFile + "?pages=" + next +str_parameter+ ">下一页</A> ";
else str += " 下一页 ";

if(intPageCount>1&&intCurrentPage!=intPageCount)
str += " <A href=" + HttpFile + "?pages=" + intPageCount +str_parameter+ ">尾页</A>";
else str += " 尾页 </font>";
str +=" 转到<INPUT TYPE='text'NAME='go' size='2'>页 <input type='submit' name='Submit' value='go'>";
return str;
}
//关闭数据库连接
public void closeConn(){
objCnn.closeStmt();
objCnn.closeConn();
}

} //end.

...全文
110 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liwenchao 2005-05-19
  • 打赏
  • 举报
回复
你的问题出在这里: class Lpage中的函数
ResultSet setQuerysql(String str_table,String str_where, String httpfile,String pages)
其中的Stirng Pagisql="select * from "+str_table+" "+str_where;
也就是说是你的目的就是去除数据库中的所有记录.
进而导致执行PageAdoRst=objCnn.executeQuery(Pagisql);//获取执行分页的结果集。
最终返回了一个错误的分页的结果集,return PageAdoRst.

解决的办法你可以控制一下:比如利用sql语句中加入关键字top,在加一些逻辑控制流程,使PageAdoRst最终得到的是分页后的结果集!
ling0 2005-05-19
  • 打赏
  • 举报
回复
这种方式不好吧,应该在bean中把每页需要的数据取出存到例如ArrayList中,把ArrayList回传到jsp,在jsp中检索ArrayList中的数据。而不是直接把ResultSet传回去了。
刘云涛 2005-05-19
  • 打赏
  • 举报
回复
各位大哥,指出问题在哪呢,小第实在是....555555
bluelily22 2005-05-19
  • 打赏
  • 举报
回复
Pagisql="select * from "+str_table+" "+str_where; 此句是返回所有结果集

PageAdoRst=objCnn.executeQuery(Pagisql);
return PageAdoRst;
这样执行的记录集当然是显示所有的记录

你的str_where 是不是忘了传进去了呀
jihanzhong 2005-05-19
  • 打赏
  • 举报
回复
自己控制,只显示要的记录
minisun2000 2005-05-19
  • 打赏
  • 举报
回复
头大了,估计是可能是Statement有问题,创建的是不可滚动的结果集!
代码实在是太长了,看着头疼!

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧