小弟作jsp不久,没有经验,身边没有人教,
这几天对分页很是困扰,求各位高手给小弟贴一个
通用分页类
由于本人很菜,请写出完整注释和使用实例 小弟不胜感激
谢谢先!!

解决方案 »

  1.   

    那个问题和我的不一样啊 我想要的是java的分页bean 
    写一个通用的类 分页这个东西我第一次接触所请大家帮忙
    希望给我一个完整的代码学习一下
      

  2.   

    String sql="select * from Guestbook where content like '%"+keyword+"%' or recontent like '%"+keyword+"%'  order by id DESC";
      ResultSet Rs=stmt.executeQuery(sql);
      int intPageSize; //一页显示的记录数 
      int intRowCount; //记录总数 
      int intPageCount; //总页数 
      int intPage; //待显示页码 
      String  strPage; 
      int i; 
      
    //设置一页显示的记录数 
     intPageSize = 1; //取得待显示页码 
    strPage = request.getParameter("page"); 
    if(strPage==null){//表明在QueryString中没有page这一个参数,此时显示第一页数据 
      intPage = 1; 
      } else{//将字符串转换成整型 
    intPage = java.lang.Integer.parseInt(strPage); 
    if(intPage< 1) intPage = 1; 

    //获取记录总数 
    Rs.last(); 
    intRowCount = Rs.getRow(); 
    //记算总页数 
    intPageCount = (intRowCount+intPageSize-1) / intPageSize; 
    //调整待显示的页码 
    if(intPage >intPageCount) intPage = intPageCount; 
    if(intPageCount >0){ 
    //将记录指针定位到待显示页的第一条记录上 
    Rs.absolute((intPage-1) * intPageSize+1); 
    //显示数据 
    i = 0; 
    while(i< intPageSize && !Rs.isAfterLast()){ 
    //读留言信息
    int id=Rs.getInt("id");
    String username=Rs.getString("username");
    String userface=Rs.getString("userface");
    String userurl=Rs.getString("userurl");
    String usermail=Rs.getString("usermail");
    String userqq=Rs.getString("userqq");
    String usertitle=Rs.getString("usertitle");
    String content=Rs.getString("content");
    String mydate=Rs.getString("mydate");
    String remydate=Rs.getString("remydate");
    String recontent=Rs.getString("recontent");
    String ip=Rs.getString("ip");
    %>
          <br>
          <table width="95%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#969696" class="f12">
            <tr> 
              <td width="200" rowspan="2" align="center" valign="top" bgcolor="#FFFFFF"> 
                <div align="center"> 
                  <table width="100%" border="0" cellpadding="2" cellspacing="0" class="f12">
                    <tr> 
                      <td colspan="2" align="center"><img src="images/face/<%=userface%>.gif" width="90" height="90"><br> 
                        <br>
                        <font color="#333333"><strong><%=username%></strong></font></td>
                    </tr>
                    <tr> 
                      <td colspan="2">&nbsp;</td>
                    </tr>
                    <tr> 
                      <td width="18"><img src="images/images/homepage.gif" width="18" height="18"></td>
                      <td width="181"><a href="<%=userurl%>" target="_blank"><%=userurl%></a></td>
                    </tr>
                    <tr> 
                      <td><img src="images/images/email.gif" width="18" height="18"></td>
                      <td><a href="mailto:<%=usermail%>"><%=usermail%></a></td>
                    </tr>
                    <tr> 
                      <td><img src="images/images/qq.gif" width="18" height="18"></td>
                      <td><%=userqq%></td>
                    </tr>
                  </table>
                </div></td>
              <td height="50" bgcolor="#FFFFFF"> 
                <table width="100%" border="0" cellpadding="2" cellspacing="2" class="f12">
                  <tr>
                    <td><font color="#333333">标题:<%=usertitle%><br>
                      时间:<%=mydate%></font></td>
                  </tr>
                </table>
                
              </td>
            </tr>
            <tr> 
              <td height="150" valign="top" bgcolor="#FFFFFF"> 
                <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0" class="f12">
                  <tr>
                    <td><font color="#333333"><%=content%></font></td>
                  </tr>
                </table>
                <%
    if(recontent!=null)
    {
    %>
                <table width="95%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#969696" class="f12">
                  <tr> 
                    <td bgcolor="#FFFFFF"><font color="5581d2">管理员回复:<%=recontent%></font><br>
                      <font color="#6699CC">--------------------------------------------------------------------------------</font> 
                      <br>
                      <font color="5581d2"><%=remydate%></font></td>
                  </tr>
                </table>
    <%
    }
    %>
              </td>
            </tr>
    <%
       if(a==null||a.booleanValue()==false)
       {}else{
    %>
            <tr> 
              <td colspan="2" bgcolor="#FFFFFF"><div align="right"><a href="javascript:go('Del.jsp?id=<%=id%>','您确定要删除?')">删除</a> 
                  <a href="JavaScript:openwin('Reply.jsp?id=<%=id%>',600,500)">回复/编辑回复</a> 
                  <a href="JavaScript:openwin('Edit.jsp?id=<%=id%>',600,500)">编辑</a> 
                  留言IP:<font color="#333333"><%=ip%></font></div></td>
            </tr>
    <%}%>
          </table>
          <br>
    <% 
      Rs.next(); 
       i++; 
    //读流言信息结束
      } 
    %>
          <br>
          <table width="100%" border="0" cellpadding="3" cellspacing="3" class="f12">
            <tr> 
              <td width="50%"><div align="right">第<%=intPage%>页 共<%=intRowCount%>条记录/共<%=intPageCount%>页 
                  <%
     if(intPage<2){
     out.print("首页 | 上一页");
     }else{%>
                  <a href="search.jsp?page=1&search=search&key=<%=request.getParameter("key")%>">首页 </a> | <a href="search.jsp?page=<%=intPage-1%>&search=search&key=<%=request.getParameter("key")%>">上一页</a> 
                  <%}%>
                  | 
                  <%
     if((intPageCount-intPage)<1)
    {
      out.print("下一页 | 尾页");
     }else{%>
                  <a href="search.jsp?page=<%=intPage+1%>&search=search&key=<%=request.getParameter("key")%>">下一页</a> | <a href="search.jsp?page=<%=intPageCount%>&search=search&key=<%=request.getParameter("key")%>">尾页</a> 
                  <%}
    }
    else{}
    %>
                </div></td>
            </tr>
          </table>
    <%}
    if(conn!=null)
    {
    conn.close();
    }
    if(stmt!=null)
    {
    stmt.close();
    }
    %></td>
      

  3.   

    精简一下看这个。
    String par_pic_name = request.getParameter("PAR_PIC_NAME");
    String qrypn="select *  from pic  where Name like '%"+par_pic_name+"%' order by Sid DESC";
    rs=stmt.executeQuery(qrypn);int intPageSize; 
    int intRowCount; 
    int intPageCount; 
    int intPage; 
    String strPage;
    int i;
    intPageSize = 3;
    strPage = request.getParameter("page");
    if(strPage==null){
    intPage = 1;
    }
    else
    {
    intPage = Integer.parseInt(strPage);
    if(intPage<1){ intPage = 1;}
    }
    rs.last(); 
    intRowCount = rs.getRow(); 
    intPageCount =(intRowCount+intPageSize-1) / intPageSize;
    if(intPage>intPageCount) intPage = intPageCount;
    if(intPageCount>0){
    rs.absolute((intPage-1) * intPageSize+1); 
    i = 0; 
    while(i< intPageSize && !rs.isAfterLast()){String rs_sid      =  rs.getString("Sid");
    String rs_name     =  rs.getString("Name");
    %><br />文字内容<% 
    rs.next(); 
       i++; 
      } 
      }
    rs.close();
    stmt.close(); 
    conn.close();
    %><br />
    <%if(intPage>1){%>
    <a href="do_search_name.jsp?PAR_PIC_NAME=<%=par_pic_name%>&amp;page=<%=intPage-1%>" >上页</a> 
    <%}else{%>
    上页 
    <% } %>
    <%if(intPage<intPageCount){%>
    <a href="do_search_name.jsp?PAR_PIC_NAME=<%=par_pic_name%>&amp;page=<%=intPage+1%>" >下页</a> 
    <%}else{%>
    下页 
    <%  }  %><%=intPage%>/<%=intPageCount%> 页<br />