算了,我再贴一遍吧,累啊
<%@ page contentType="text/html;charset=gb2312"%>
<% 
//变量声明 
java.sql.Connection sqlCon; //数据库连接对象 
java.sql.Statement sqlStmt; //SQL语句对象 
java.sql.ResultSet sqlRst; //结果集对象 
java.lang.String strCon; //数据库连接字符串 
java.lang.String strSQL; //SQL语句 
int intPageSize; //一页显示的记录数 
int intRowCount; //记录总数 
int intPageCount; //总页数 
int intPage; //待显示页码 
java.lang.String strPage; 
int i,j,k; //设置一页显示的记录数 
intPageSize = 5; //取得待显示页码 
strPage = request.getParameter("page"); 
if(strPage==null){
//表明在QueryString中没有page这一个参数,此时显示第一页数据 
intPage = 1; 
} else{
//将字符串转换成整型 
intPage = java.lang.Integer.parseInt(strPage); 
if(intPage<1) intPage = 1; }
//装载JDBC-ODBC驱动程序 
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
//设置数据库连接字符串 
strCon = "jdbc:odbc:Test_DB"; 
//连接数据库 
sqlCon = java.sql.DriverManager.getConnection(strCon,"sa",""); 
//创建SQL语句对象 
sqlStmt = sqlCon.createStatement(); 
//获取记录总数 
strSQL = "select count(*) from guestbook"; 
sqlRst = sqlStmt.executeQuery(strSQL); 
//执行SQL语句并取得结果集 
sqlRst.next(); //记录集刚打开的时候,指针位于第一条记录之前 
intRowCount = sqlRst.getInt(1); 
sqlRst.close(); //关闭结果集 
//记算总页数 
intPageCount = (intRowCount+intPageSize-1) / intPageSize; 
//调整待显示的页码 if(intPage>intPageCount) intPage = intPageCount; 
//设置获取数据SQL语句 
strSQL = "select name,email,body from guestbook"; 
//执行SQL语句并取得结果集 
sqlRst = sqlStmt.executeQuery(strSQL);
//将记录指针定位到待显示页的第一条记录上 
i = (intPage-1) * intPageSize; 
for(j=0;j<i;j++) sqlRst.next(); %> 
<html> 
<head>
<title>JSP数据库操作例程 - 数据分页显示 - JDBC-ODBC</title>
</head> 
<body> 
<p align=center>jdbc-odbc留言版</p> 
<table border="1" cellspacing="0" cellpadding="0" width=600 align=center> 
<% 
//显示数据 
i = 0; 
while(i<intPageSize && sqlRst.next()){ %> 
<tr> 
<td>姓名:<%=sqlRst.getString(1)%></td>
<td>邮件:<%=sqlRst.getString(2)%></td>
</tr> 
<tr> 
<td colspan=2><%=sqlRst.getString(3)%></td>
</tr>
<% i++; } %>
<tr> 
<td colspan=2 align=center> 
第<%=intPage%>页 共<%=intPageCount%>页 
<%if(intPage<intPageCount){%>
<a href="mssql.jsp?page=<%=intPage+1%>">下一页</a>
<%
}
%> 
<%if(intPage>1){%>
<a href="mssql.jsp?page=<%=intPage-1%>">上一页</a>
<%
}
%> 
</td> 
</tr>
</table> </body> 
</html> 
<% 
//关闭结果集 
sqlRst.close(); 
//关闭SQL语句对象 
sqlStmt.close(); 
//关闭数据库
sqlCon.close();
%>

解决方案 »

  1.   

    Web开发中一种用sql语句完成分页的高效率方法 
    出自:beyond_xiruo 2002年11月20日 16:14 
    一、Jsp方法如下:
    **********************
    <%@ page language="java" import="java.util.*,java.sql.*" %>
    <%@ page contentType="text/html;charset=gb2312"%>
    <jsp:useBean id="cn" scope="page" class="myConnection.Conn" /><!--引用数据库操作的bean,自己完成,这里不再赘述-->
    <%
    int curpage=1;//当前页
    int page_record=20;//每页显示的记录数
    //用下面的方法(sql查询完成,速度快)
    curpage=Integer.parseInt(request.getParameter("page"));//获取传递的值,需要显示的页
    ResultSet rs=cn.rsexecuteQuery("select top "+page_record+" * from tablename where id not in (select top "+(curpage*page_record)+" id from tablename order by id desc) order by id desc");
    //本查询语句得到的是所要显示的1000页的20条记录,大致思路为——子查询排除需要显示的记录前的所有记录,父查询则对余下的记录进行降序排列
    while(rs.next) {
      out.println(rs.getInt("id").toString());
    }
    rs.close();
    %>
      

  2.   

    我把所有的查询放到了类里,我想在不改动类的情况下,做成分页的形式,请问用JSP可以做到吗。
      

  3.   

    在bean里边加个这样的方法啊:
     public Vector selectFromTo(int intFrom ,int  intTo) throws CesException{}
    然后再页面控制每次要选择的from--to;比如   intFrom=1;intTo=20;
      

  4.   

    SQL语句: 
    strSQL="select  *     from  (select   temp.*,  rownum  rno   from  temp   where  rownum  < " +intTo+ "    order  by  file_id)  "+
                                 " where  rno  >="+intFrom;
      

  5.   

    SQL语句以在类里了,现在类不动,怎么用JSP来实现分页,谢谢各位,结决一定高分相送。
      

  6.   

    <%@ page contentType="text/html;charset=gb2312" %>
    <%@ page language="java" import="java.sql.*"%>
    <%@ page import="java.util.*"%>
    <%@ page import="com.hand.hayao.reports.InterfaceCuxxsfx2" %>
    <%@ page import="com.hand.hayao.reports.vo.InterfaceCuxxsfx2VO" %>
    <%@ page import="com.hand.common.db.*" %>
    <%@ page import="com.hand.common.db.DbUtils" %>
    <%@ page import="com.hand.hayao.AppManager" %>
    <%@ page import="java.text.*" %>
    <%@ include file="validation.jsp" %><%
            String dateStr ="";
    String currentDate ="";
        Vector itemSalesList = new Vector();
        Connection conn = null;
        boolean isValideDate = false;
        
    try {
     conn = ConnectionManager.getConnection(AppManager.JDBC_NAME);
             if (!sessionManager.hasPrivilege(conn, new Long(1))) {
                 sessionManager.showMsg(response, sessionManager.MSG_WARNING, "您没有权限查看此项内容");
                 return;
             }

            java.sql.Date current = DbUtils.getOracleCurrentDT(conn);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    currentDate = sdf.format(current);
    dateStr = request.getParameter("datestr")==null?currentDate: request.getParameter("datestr");
            
            isValideDate = DbUtils.isValideDateString(conn, dateStr);
            if(isValideDate) {
                itemSalesList = InterfaceCuxxsfx2.queryByDate(conn, dateStr);
            }

    } catch(Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        conn.close();     
    }%>
     <%
        int intPageSize; //一页显示的记录数 
        int intRowCount; //记录总数 
        int intPageCount; //总页数 
        int intPage; //待显示页码 
        int page1;
        String strPage; 
        int x,y,z;  
        intPageSize = 18;  //设置一页显示的记录数 //获取记录总数 
    //创建SQL语句对象 
    //记算总页数 

       
    intPageCount = (itemSalesList.size()+intPageSize-1) / intPageSize; 
    //调整待显示的页码

    for(intPage=1;intPage<=intPageCount;intPage++) {%><html>
    <head><title>哈药PDA Protal</title></head><script language="JavaScript"></script><body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0"><%@ include file="header.jsp"%>
    <form name="form1" method="post">
      <div align="center">
        <INPUT TYPE="text" NAME="datestr" SIZE="8" VALUE= <%=dateStr=="sysdate"?"today":dateStr%>>
        <INPUT TYPE="submit" VALUE="确定">
        (yyyymmdd) </div>
      <HR align="center" width="980">
    <%if(isValideDate) {%>
      
      <TABLE width="980" border="0" align="center" cellpadding="0" cellspacing="1" higth="15" >
        <TR bordercolor="#000000"  bgcolor="#4C83BB"> 
          <TD width="115" rowspan="3" align="center" valign="middle" nowrap><font color="#FFFFFF">品种</font></TD>
          <TD width="75" rowspan="3" align="center" valign="middle" nowrap><font color="#FFFFFF">年计划</font></TD>
          <TD width="75" rowspan="3" align="center" valign="middle" nowrap><font color="#FFFFFF">当日销量</font></TD>
          <TD colspan="3" align="center" nowrap><strong><font color="#FFFFFF">月累计销售</font></strong></TD>
          <TD colspan="3" align="center" nowrap><strong></strong><strong><font color="#FFFFFF">年累计销售</font></strong><strong></strong></TD>
          <TD colspan="3" align="center" nowrap><strong><font color="#FFFFFF">累计进度</font></strong></TD>
        </TR>
        <TR  bgcolor="#4C83BB"> 
          <TD width="80" nowrap align="center"><font color="#FFFFFF">本月至当</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">上月同期</font></TD>
          <TD width="80" rowspan="2" align="center" nowrap><font color="#FFFFFF">增减</font><font color="#FFFFFF">&nbsp; 
            </font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">本年至上月</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">去年同期</font></TD>
          <TD width="80" rowspan="2" align="center" nowrap><font color="#FFFFFF">为同期%</font><font color="#FFFFFF">&nbsp; 
            </font></TD>
          <TD width="80" rowspan="2" align="center" nowrap><font color="#FFFFFF">时间</font><font color="#FFFFFF">&nbsp; 
            </font></TD>
          <TD width="80" rowspan="2" align="center" nowrap><font color="#FFFFFF">销量</font><font color="#FFFFFF">&nbsp; 
            </font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">未完成</font></TD>
        </TR>
        <TR  bgcolor="#4C83BB"> 
          <TD width="80" nowrap align="center"><font color="#FFFFFF">日销量</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">销量</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">累计销量</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">销量</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">计划品种</font></TD>
        </TR>
        </TABLE>
       <%
    InterfaceCuxxsfx2VO vo = null;
    for(z=0;z<intPageSize;z++)
           { vo = (InterfaceCuxxsfx2VO) itemSalesList.get(intPageSize*(intPage-1)+z);
        %>
        <TABLE width="980" border="0" align="center" cellpadding="0" cellspacing="1" higth="15" >
        <TR bgcolor="#99CC99"> 
          <TD width="115" align="left" nowrap><%=vo.getItemDescription()%></TD>
          <TD width="75" align="right" nowrap><%=vo.getYEAR_PLAN_QTY()%></TD>
          <TD width="75" align="right" nowrap><%=vo.getTODAY_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getMON_TODAY_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getSAME_MON_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getINC_RED_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getTHIS_YEAR_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getSAME_YEAR_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getTHAN_LASTYEAR_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getCOMP_TIME_RATE()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getCOMP_QTY_RATE()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getCOMP_FLAG()%></TD>
        </TR>
       </TABLE> <%}%>
       
        <HR align="center" width="980">
    <%
    }
    else {
        out.println("您输入的日期无效,请重新输入!<HR>");
    }
    %>
    </form>
     <table border="0" cellspacing="0" cellpadding="0" width="100%" align=center height="100%">
              
    <tr> 
      <td align=center><div align="center">第<%=intPage%>页 共<%=intPageCount%>页 
          <%if(intPage<intPageCount){%>
          <a href="xsfx2.jsp?page1=<%=intPage+1%>">下一页</a>
          <%
      }
      %>
          <%if(intPage>1){%>
          <a href="xsfx2.jsp?page1=<%=intPage-1%>">上一页</a>
          <%
      }
      
        }
        %>
          <%@ include file="bottom.jsp"%>
        </div>
    </body>
    </html>
    这样加入后,页是分出来了,但在一个页里,点下一页,就是不好使,不知道是那错了,请帮助。
      

  7.   

    查询仅仅需要记住当前页,上下翻页利用sql语句在数据库查询就可以了。注意参数的传递!!
      

  8.   

    谢谢各位了,我用我的方法解决了问题,以下是我用的方法
    <%@ page contentType="text/html;charset=gb2312" %>
    <%@ page language="java" import="java.sql.*"%>
    <%@ page import="java.util.*"%>
    <%@ page import="com.hand.hayao.reports.InterfaceCuxxsfx2" %>
    <%@ page import="com.hand.hayao.reports.vo.InterfaceCuxxsfx2VO" %>
    <%@ page import="com.hand.common.db.*" %>
    <%@ page import="com.hand.common.db.DbUtils" %>
    <%@ page import="com.hand.hayao.AppManager" %>
    <%@ page import="java.text.*" %>
    <%@ include file="validation.jsp" %><%
            String dateStr ="";
    String currentDate ="";
        Vector itemSalesList = new Vector();
        Connection conn = null;
        boolean isValideDate = false;
        
    try {
     conn = ConnectionManager.getConnection(AppManager.JDBC_NAME);
             if (!sessionManager.hasPrivilege(conn, new Long(1))) {
                 sessionManager.showMsg(response, sessionManager.MSG_WARNING, "您没有权限查看此项内容");
                 return;
             }

            java.sql.Date current = DbUtils.getOracleCurrentDT(conn);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    currentDate = sdf.format(current);
    dateStr = request.getParameter("datestr")==null?currentDate: request.getParameter("datestr");
            
            isValideDate = DbUtils.isValideDateString(conn, dateStr);
            if(isValideDate) {
                itemSalesList = InterfaceCuxxsfx2.queryByDate(conn, dateStr);
            }

    } catch(Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        conn.close();     
    }%><html>
    <head><title>哈药PDA Protal</title></head><script language="JavaScript"></script><body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0">
    <%@ include file="header.jsp"%>
    <form name="form1" method="post">
      <div align="center">
        <INPUT TYPE="text" NAME="datestr" SIZE="8" VALUE= <%=dateStr=="sysdate"?"today":dateStr%>>
        <INPUT TYPE="submit" VALUE="确定">
        (yyyymmdd) </div>
      <HR align="center" width="980">
    <%if(isValideDate) {%>
      
      <TABLE width="980" border="0" align="center" cellpadding="0" cellspacing="1" higth="15" >
        <TR bordercolor="#000000"  bgcolor="#4C83BB"> 
          <TD width="115" rowspan="3" align="center" valign="middle" nowrap><font color="#FFFFFF">品种</font></TD>
          <TD width="75" rowspan="3" align="center" valign="middle" nowrap><font color="#FFFFFF">年计划</font></TD>
          <TD width="75" rowspan="3" align="center" valign="middle" nowrap><font color="#FFFFFF">当日销量</font></TD>
          <TD colspan="3" align="center" nowrap><strong><font color="#FFFFFF">月累计销售</font></strong></TD>
          <TD colspan="3" align="center" nowrap><strong></strong><strong><font color="#FFFFFF">年累计销售</font></strong><strong></strong></TD>
          <TD colspan="3" align="center" nowrap><strong><font color="#FFFFFF">累计进度</font></strong></TD>
        </TR>
        <TR  bgcolor="#4C83BB"> 
          <TD width="80" nowrap align="center"><font color="#FFFFFF">本月至当</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">上月同期</font></TD>
          <TD width="80" rowspan="2" align="center" nowrap><font color="#FFFFFF">增减</font><font color="#FFFFFF">&nbsp; 
            </font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">本年至上月</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">去年同期</font></TD>
          <TD width="80" rowspan="2" align="center" nowrap><font color="#FFFFFF">为同期%</font><font color="#FFFFFF">&nbsp; 
            </font></TD>
          <TD width="80" rowspan="2" align="center" nowrap><font color="#FFFFFF">时间</font><font color="#FFFFFF">&nbsp; 
            </font></TD>
          <TD width="80" rowspan="2" align="center" nowrap><font color="#FFFFFF">销量</font><font color="#FFFFFF">&nbsp; 
            </font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">未完成</font></TD>
        </TR>
        <TR  bgcolor="#4C83BB"> 
          <TD width="80" nowrap align="center"><font color="#FFFFFF">日销量</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">销量</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">累计销量</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">销量</font></TD>
          <TD width="80" nowrap align="center"><font color="#FFFFFF">计划品种</font></TD>
        </TR>
        </TABLE>
        <%
        int intPageSize; //一页显示的记录数 
        int intRowCount; //记录总数 
        int intPageCount; //总页数 
        int intPage; //待显示页码 
        int page1;
        String strPage; 
        int x,y,z;  
        intPageSize = 15;  //设置一页显示的记录数 //获取记录总数 
    //创建SQL语句对象 
    //记算总页数 

       
    intPageCount = (itemSalesList.size()+intPageSize-1) / intPageSize; 
    //调整待显示的页码


        
        InterfaceCuxxsfx2VO vo = null;
    for(intPage=1;intPage<=intPageCount;intPage++) {
    for(z=1;z<=intPageSize;z++)
           { vo = (InterfaceCuxxsfx2VO) itemSalesList.get(intPageSize*(intPage-1)+z);
        %>
        <TABLE width="980" border="0" align="center" cellpadding="0" cellspacing="1" higth="15" >
        <TR bgcolor="#99CC99"> 
          <TD width="115" align="left" nowrap><%=vo.getItemDescription()%></TD>
          <TD width="75" align="right" nowrap><%=vo.getYEAR_PLAN_QTY()%></TD>
          <TD width="75" align="right" nowrap><%=vo.getTODAY_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getMON_TODAY_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getSAME_MON_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getINC_RED_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getTHIS_YEAR_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getSAME_YEAR_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getTHAN_LASTYEAR_QTY()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getCOMP_TIME_RATE()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getCOMP_QTY_RATE()%></TD>
          <TD width="80" align="right" nowrap><%=vo.getCOMP_FLAG()%></TD>
        </TR>
       </TABLE> <%}
       
       %>
     <table border="0" cellspacing="0" cellpadding="0" width="100%" align=center height="100%">
              <tr> 
      <td align=center><br/> 
       第<%=intPage%>页  共<%=intPageCount%>页  
      <%if(intPage<intPageCount){%>
      <a href="xsfx2.jsp?page1=<%=intPage+1%>">下一页</a><%
      }
      %>  
      <%if(intPage>1){%>
      <a href="xsfx2.jsp?page1=<%=intPage-1%>">上一页</a><%
      }
      
        }
        %>
      
        
      
      <HR align="center" width="980">
    <%
    }
    else {
        out.println("您输入的日期无效,请重新输入!<HR>");
    }
    %><a href='menu.jsp'>返回</a></form><%@ include file="bottom.jsp"%></body>
    </html>