jsp分页的问题

dwxq 2003-10-14 03:55:30
从书上抄个jsp分页的例子运行,第一页的数据不能正常显示,而第二页的数据能够正常显示,不知原因,
jsp代码如下:
<jsp:useBean id="aaaBean" scope="page" class="firm.firm"/>
<%
//定义变量初始化
int pageLine=10; //指导定行数
int totalRec=0; //总记录数
int intPage=1; //当前页数
int i; //循环次数
if (request.getParameter("page")!=null)
intPage=Integer.parseInt(request.getParameter("page"));
//把从页面跳转表单中获取的数值类型(字符型)转换为整数型
try {
ResultSet countrs=null;
//取得总记录数
countrs=aaaBean.executeQuery("select count(*) as cnt from firm");
if (countrs.next())
totalRec=countrs.getInt("cnt");
countrs.close();
aaaBean.closeStmt();
}
catch(Exception e){//异常处理
e.printStackTrace();
}
//取得总页数
int intPageCount=0;
intPageCount=(totalRec+pageLine-1)/pageLine;
%>
<%
ResultSet rs=aaaBean.executeQuery("select * from firm");
//定义变量
String name;
String userid;
%>
<table border="1" width="650" align="center" cellspacing="0" bgcolor="#d7e3b9" bordercolorlight="#green" bordercolordark="ecf5ff">
<tr bgcolor="c8cc98" align="center">
<td width="20%">编号</td>
<td width="60%">姓名</td>
<td width="20%">删除</td>
</tr>
<%
if (intPageCount>0) //如果总页数大于0,则执行下面的语句
{
for (i=1;i<=(intPage-1)*pageLine;i++){ //每页的记录循环
rs.next();
for (i=1;i<=pageLine;i++)
if (rs.next())
{
name=rs.getString("name");
userid=rs.getString("id");
%>
<tr>
<td width="20%" align="center"><%=userid%></td>
<td width="60%" align="center"><%=name%></td>
<td width="20%" align="center"><a href="delete.jsp?id=<%=userid%>" onclick="return newwin(this.href)">删除</a></td>
</tr>
<%
}
}
rs.close();
}
%>
<%
//以下用于分页显示
out.print("<table border='0' align='center'><tr><td>");
if(intPageCount*pageLine<totalRec)
intPageCount++;
//如果总页数*记录行数总记录数,即页数加1
if (intPage>intPageCount)
intPage=intPageCount;
//如果当前页数大于总页数,则当前等于总页数
if (intPage<1)
intPage=1; //如果当前页数小于1,则把它设置为1
out.print("<form method='post' name='fpagenum' action='show.jsp' align='center'>");
out.print("<input type='hidden' name='page' value="+intPage+">");
//若使用跳转页数,则执行此句
out.print("<p align='left'>分页 ");
if (intPage<2) //如果当前页数小于2,则首页和上一页无连接
out.print("<font color='999966'>首页 【上一页】</font> ");
else{
out.print("<a href='show.jsp?page="+1+"'>【首页】</a> ");
out.print("<a href='show.jsp?page="+(intPage-1)+"'>【上一页】</a> ");
}
if (intPage-intPageCount>=0)//如果当前页数小于总页数,则下一页和尾页有连接
out.print("<font color='999966'>【下一页】 【尾页】</font>");
else{
out.print("<a href='show.jsp?page="+(intPage+1)+"'>【下一页】</a> ");
out.print("<a href='show.jsp?page="+(intPageCount)+"'>【尾页】</a>");
}
out.print(" 页次:<font color='red'><strong>"+intPage+"</strong></font>/<font color='red'><strong>"+intPageCount+"</strong></font>页");
out.print("  共<font color='red'><b>"+totalRec+"</b></font>条记录<b>  <font color='red'>"+pageLine+"</font></b> 条/页  ");
out.print("转到第 <input type='text' name='page' size='2' maxlength='10' class='smallinput' value="+intPage+">");//跳转页
out.print(" 页 <input class='buttongace' type='submit' value='转到' size='5' name='cndok'></span></p></form>");

out.print("</td>");
out.print("</tr>");
out.print("</table>");
%>
</table>
...全文
24 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
一把吉他 2004-02-24
  • 打赏
  • 举报
回复
好东西当然要分享啊!

看我的:

经典分页程序
jsp+javaBean

http://www.52free.com/bbs/index.php?act=ST&f=8&t=148&st=0#entry298

如果数据库不同只需修改javaBean中的驱动即可,页面显示数可以自己控制。
yanxibang 2004-02-21
  • 打赏
  • 举报
回复
package hzdq.fdjc.Common;

import java.sql.*;
import java.util.*;

/**
* Title:分页
* Description:
* Copyright: Copyright (c) 2004
* Company:
* author:颜喜班
* @version 1.0
*/
public class SplitPager
{
/*
* _sql_str:传入的sql语句
* _total_records: 总记录数目
* _pageSize: 每页显示的记录数目
* _page: 所分的逻辑页数
*/
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
private ResultSetMetaData rsmd=null;
private String _sql_str;
private int _total_records;
private int _pages;
private int _pagesize;
public void setConnection(Connection con)
{
this.con=con;
if (this.con == null)
System.out.println("Failure to get a connection!");
else
System.out.println("Success to get a connection!");
}
public void initialize(String sqlStr,int pageSize)
{
this._sql_str=sqlStr;
this._pagesize=pageSize;
try{
stmt=this.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(this._sql_str);
rsmd=rs.getMetaData();
if (rs!=null)
{
rs.last();
this._total_records = rs.getRow();
rs.first();
this._pages = (this._total_records - 1) / this._pagesize + 1;
}
}
catch(SQLException e){System.out.println(e.toString()); }
}
public Vector getPage(int ipage){
Vector vData=new Vector();
int n=ipage;
int m=0;
m=(n-1)*this._pagesize+1;
try{
if (rs!=null)
{
rs.absolute(m);
for(int i=0;i<this._pagesize;i++){
String[] sData=new String[rsmd.getColumnCount()];
for(int j=0;j<rsmd.getColumnCount();j++)
{
sData[j]=rs.getString(j+1);
}
if (sData==null)
{
break;
}
vData.addElement(sData);
rs.next();
}
}
rs.close();
stmt.close();
}
catch(SQLException e){System.out.println(e.toString()); }
return vData;
}
public int getPages()
{
return this._pages;
}
public int getTotalRecords()
{
return this._total_records;
}

}

81,095

社区成员

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

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