或许我上次说的不够详细,其实,我现在已经完成了与数据库的连接,并且能够将rs 中的结果全部输出到网页,但是出于美观。我想将其设置为分页输出。可是,用For循环如何也控制不了。我明白分页的控制是用链接来实现,但是参数的变化我却不会。希望有同胞能够帮忙!!!

解决方案 »

  1.   

    你可以将rs定位到你要显示的第一个数据上(rs的一个方法可以实现记不清了),比如你要每页显示5条数据,现在要显示第2页,就可以先将rs定位到第5条数据,然后用一个for循环5次,就可以取出数据了了!
    你的这种写代码的方式不太好,把数据库操作都写道页面上去了,这对以后的维护不好,还是封装到bean里面去吧!!
      

  2.   

    给你贴段代码,希望对你有帮助
    <%
    Connection myConn      = null;
    Statement Stmt         = null;
    ResultSet rs           = null;
    String sSQL            = "";int iRowCount;         //总记录数
    int PageNumCount;      //总页数
    int PageSize=20;       //每页显示的记录数
    int PageNum;           //当前显示的页号
    ..
      Stmt = myConn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
      rs   = Stmt.executeQuery(sSQL);
      //获取记录数
      String strPageNum =StringUtils.nullToString(request.getParameter("page"));
      if( strPageNum.equals( "" ) ){
        //表明在QueryString中没有page这一个参数,此时显示第一页数据
        PageNum = 1;
      }else{
        PageNum = java.lang.Integer.parseInt( strPageNum );  //将字符串转换成整型
        if( PageNum < 1 ) PageNum = 1;
      }  rs.last();
      iRowCount=rs.getRow();
      PageNumCount = ( iRowCount + PageSize - 1 ) / PageSize;
      if( PageNum > PageNumCount ) PageNum = PageNumCount;
    %>.....
    <table width="98%" border="0" cellspacing="1" cellpadding="3" bgcolor="0088CE">
          <tr bgcolor="#DDEEFF">
          <td colspan="8">
          <div align="right">
          <a href="javascript:document.form1.page.value=1;document.form1.submit()">首页</a>
          <a href="javascript:document.form1.page.value=<%=PageNum-1%>;document.form1.submit()">上一页</a>
          <a href="javascript:document.form1.page.value=<%=PageNum+1%>;document.form1.submit()">下一页</a>
          <a href="javascript:document.form1.page.value=<%=PageNumCount%>;document.form1.submit()">尾页</a> 共<%=PageNumCount%>页 共<%=iRowCount%>主题 转到
          <input type="text" name="page" size="2" style="width:30">
           页</div>
          </td>
        </tr> <%
          rs.absolute((PageNum-1) * PageSize + 1);//定位到当前页的第一条记录,实现分页
          int i = 0;
          while ( i<PageSize && !rs.isAfterLast()){
         %>
              <tr bgcolor="#EFF7FF">
              <td align="center"><input type="checkbox" name="selectID" value="<%=rs.getString("applytblno")%>"></td>
              <td align="center"><%=i+1%></td>
              <td><a href="apply.jsp?ApplyTblNo=<%=rs.getString("applytblno")%>"><%=StringUtils.nullToString(rs.getString("applyitemname"))%></a></td>
              <td><%=StringUtils.nullToString(rs.getString("applyitemno"))%></td>
              <td><%=StringUtils.nullToString(rs.getString("cspecialtype"))%></td>
              <td><%=StringUtils.nullToString(rs.getString("cbuildtype"))%></td>
              <td><%=StringUtils.nullToString(rs.getString("investfee"))%></td>
              </tr>
         <%
           rs.next();
           i++;
          }
        %>
      

  3.   

    多谢以上两位,我是jsp初学者,以前是写Sql的。目前面临比较大的困难。多谢二位指教。
      

  4.   

    你最好是用一个bean来分野,你看你的java代码和html放在一起多难读啊。
    package sdk;/**
     * <p>Title: 分页</p>
     * <p>Description:分页 </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company:工科 </p>
     * @author not attributable
     * @version 1.0
     */
    import java.sql.*;
    import java.util.*;
    import sun.jdbc.rowset.CachedRowSet;public class Paging {
      private OpenDbBean db = new OpenDbBean();
      private Connection conn = null;
      private ResultSet rs = null;
      private Statement st = null;
      private String tableName = null;//需要分页的数据库表名称
      private String whereCondition = null;//查询语句的where条件
      private String orderby = null;//查询语句的orderby条件
      private int countPerPage = 20; //默认每页的数目
      //输入:无
      //输出:所有的页数
      public int getAllpage() {
        int i = 0;
        try {
          conn = db.getConnection();
          st = conn.createStatement();
          String sql = "select count(*) from " + this.tableName;
          if (this.whereCondition != null) {
            sql += " where " + this.whereCondition;
          }
          System.out.println(sql);
          rs = st.executeQuery(sql);
          if (rs.next()) {
            i = (rs.getInt(1) / this.countPerPage)+1;
          }
        }
        catch (SQLException e) {
          System.out.println(e.getMessage());
        }
        finally {
          try {
            db.CleanConnection(conn, st, rs);
          }
          catch (SQLException e) {
            System.out.println(e.getMessage());
          }
        }
        return i;
      }
      //输入:每页显示的条目数
      public void setCountPerPage(int i) {
        this.countPerPage = i;
      }
      //输入:要分页的表名
      public Paging(String tableName) {
        this.tableName = tableName;
      }
      //输入:查询的where条件
      public void setWhere(String str) {
        this.whereCondition = str;
      }
      //输入:当前页面号
      //输出:下一个页面号
      public int getNexepage(int pageNum){
        if (pageNum==this.getAllpage())
          return this.getAllpage();
        else{
          return pageNum+1;
        }
      }
      //输入:当前页面号
      //输出:前一个页面号
      public int getPreviouspage(int pageNum){
      if (pageNum==1)
        return 1;
      else{
        return pageNum-1;
      }
    }
      //输入:查询的orderby条件
      public void setOrderby(String str) {
        this.orderby = str;
      }
      //输出:查询的sql语句
      public String getSql() {
        String sql = "select * from " + this.tableName;
        if (this.whereCondition != null) {
          sql += " where " + this.whereCondition;
        }
        if (this.orderby != null) {
          sql += " order by " + this.orderby;
        }
        return sql;
      }
      //输入:当前页面号
      //输出:取得当前页面结果集的sql语句
      public String getSql(int pageNum) {
         String sql = "select * from " + this.tableName;
         if (this.whereCondition != null) {
           sql += " where " + this.whereCondition;
         }
         sql+=" limit "+(pageNum-1)*this.countPerPage+","+this.countPerPage;
         return sql;
       }
      //输入:当前页面号
      //输出:当前页面的结果集
      public CachedRowSet getPageRecord(int pageNum) throws SQLException{
        CachedRowSet cst=new  CachedRowSet();
        try {
          conn = db.getConnection();
          st = conn.createStatement();
          rs = st.executeQuery(this.getSql(pageNum));
          cst.populate(rs);
        }
        catch (SQLException e) {
          System.out.println(e.getMessage());
        }
        finally {
          try {
            db.CleanConnection(conn, st, rs);
          }
          catch (SQLException e) {
            System.out.println(e.getMessage());
          }
        }
        return cst;
      }
      //测试例子
      public static void main(String[] args) {
        Paging p = new Paging("table1");
        p.setCountPerPage(5);
        System.out.println(p.getSql());
        System.out.println("total page= " + p.getAllpage());
        try{
          CachedRowSet cs=p.getPageRecord(1);
          while(cs.next()){
            System.out.println(cs.getString(1));
          }
        }
        catch(Exception e){
          System.out.println(e.getMessage());
        }
      }}
    jsp调用代码
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <%
    sdk.Paging pageBean=new sdk.Paging("news");
    pageBean.setCountPerPage(20);
    pageBean.setWhere("n_onpub='1' and nt_id=1 and  n_isuse='1'");String str=request.getParameter("page_count");
    if (str==null) str="1";
    int p_num=Integer.parseInt(str);try{
          CachedRowSet cs=pageBean.getPageRecord(pageBean.getAllpage()-p_num+1);
          cs.afterLast();
          while(cs.previous()){
    %>
    <tr>
                          <td height="22"><img src="images/news01.gif" width="9" height="12">
                          <a href="viewnews.jsp?n_id=<%=cs.getString("n_id")%>">
    <%=sdk.Tools.toGBKString(cs.getString("n_title"))%></a>(<%=sdk.Tools.toGBKString(cs.getString("n_time"))%>) </td>
                        </tr>
    <%
       }
    }
    catch(Exception e){
      System.out.println(e.getMessage());
    }
    %>
                      </table></td>
                  </tr>
                  <tr>
                    <td height="13">&nbsp;</td>
                  </tr>
                  <tr>
                    <td height="22" bgcolor="#E8E8E8"> <p><img src="images/left.gif" width="7" height="11"><img src="images/left.gif" width="7" height="11">
                        <a href="news.jsp?page_count=1"> 第一页</a> <img src="images/left.gif" width="7" height="11">
                        <a href="news.jsp?page_count=<%=pageBean.getPreviouspage(p_num)%>">
                        上一页 </a> <img src="images/right.gif" width="7" height="11">
                        <a href="news.jsp?page_count=<%=pageBean.getNexepage(p_num)%>">
                        下一页</a> <img src="images/right.gif" width="7" height="11"><img src="images/right.gif" width="7" height="11">
                        <a href="news.jsp?page_count=<%=pageBean.getAllpage()%>">
                        最后一页</a>  第<font color="#FF0000"><%=p_num%> </font>页/共<font color="#FF0000"><%=pageBean.getAllpage()%> </font>页   </p></td>
                  </tr>
                </table></td>
            </tr>
          </table> </td>
        <td width="21" valign="top">&nbsp;</td>
      </tr>
    </table>
      

  5.   

    上面的兄弟,你的JAVABEAN编译通不过啊!
      

  6.   

    MyJAVA/Paging.java:13: package sun.jdbc.rowset does not exist
    import sun.jdbc.rowset.CachedRowSet;
                           ^
    MyJAVA/Paging.java:16: cannot resolve symbol
    symbol  : class OpenDbBean
    location: class MyJAVA.Paging
      private OpenDbBean db = new conn();
              ^
    MyJAVA/Paging.java:111: cannot resolve symbol
    symbol  : class CachedRowSet
    location: class MyJAVA.Paging
      public CachedRowSet getPageRecord(int pageNum) throws SQLException{
             ^
    MyJAVA/Paging.java:16: cannot access MyJAVA.conn
    bad class file: c:\jdk1.4\bin\MyJAVA\conn.class
    class file contains wrong class: com.conn
    Please remove or make sure it appears in the correct subdirectory of the classpa
    th.
      private OpenDbBean db = new conn();
                                  ^
    4 errors