我今天刚刚搞成功的希望对你有用:
<%@  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  =  3;  //取得待显示页码      
 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("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
 //设置数据库连接字符串      
 strCon  =  "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";      
 //连接数据库      
 sqlCon  =  java.sql.DriverManager.getConnection(strCon,"dike8523$","dike8523$");      
 //创建SQL语句对象      
 sqlStmt  =  sqlCon.createStatement();      
 //获取记录总数      
 strSQL  =  "select count(*) from  ly1";      
 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  lname,lemail,content  from  ly1 ORDER BY id DESC";      
 //执行SQL语句并取得结果集      
 sqlRst  =  sqlStmt.executeQuery(strSQL);    
 //将记录指针定位到待显示页的第一条记录上      
 i  =  (intPage-1)  *  intPageSize;      
 for(j=0;j<i;j++)  sqlRst.next();  %>      
 <html>      
 <head>    
 <title>分页显示</title>    
 </head>      
 <body>      
<table  border="0"  cellspacing="0"  cellpadding="0"  width=500  align=center>     
<%      
 //显示数据      
 i  =  0;      
 while(i<intPageSize  &&  sqlRst.next()){  %>      
<tr bgcolor=#336699>
<td colspan=2 >~~~留言~~~</td>
</tr> 
<tr>      
<td>用户名:<%=sqlRst.getString("lname")%></td>    
</tr>
<tr>
<td>邮件:<%=sqlRst.getString("lemail")%></td>    
</tr>
<tr>
<td>内容:<%=sqlRst.getString("content")%></td>    
</tr>  
<tr><td><hr width=500 boder=1></td></tr>
<%  i++;  }  %>    
 <tr>      
<td  colspan=2  align=center>      
 第<%=intPage%>页    共<%=intPageCount%>页        
 <%if(intPage<intPageCount){%>    
 <a  href="ly3.jsp?page=<%=intPage+1%>">下一页</a><%    
 }    
 %>        
 <%if(intPage>1){%>    
 <a  href="ly3.jsp?page=<%=intPage-1%>">上一页</a><%    
 }    
 %>      
 </td>      
 </tr>    
 </table>  
<hr width=500 boder=1>
</body>      
 
</html>      
 
<%      
 
//关闭结果集      
 
sqlRst.close();      
 
//关闭SQL语句对象      
 
sqlStmt.close();      
 
//关闭数据库    
 
sqlCon.close();    
 
%>

解决方案 »

  1.   


    俺也是在 jsp 页面里面实现的分页处理,不是通过 SQL 语句……
    -----------------------------------------------------------<%
    // -------------------------------------------------------------------------
    // for 分页处理……
    // -------------------------------------------------------------------------
    int NumberPerPage  = 30; // 每页多少“条”记录
    int TotalPageNumber  = 1; // 总共有多少“页”;默认为 1:总共 1 页 int CurrentPageNO  = 1; // 当前页的编号; 默认为 1:第一页;
    int BeginIndexOfRecords  = 1; // “本页”起始记录编号; 默认为 1:第一页;
    int EndIndexOfRecords  = NumberPerPage; // int EndIndexOfRecords = (PageNO+1)*NumberPerPage; // “本页”终止记录编号; 默认为 30:第一页。 try {
    String CurrentPageNO_String  = null;
    CurrentPageNO_String = (String)request.getParameter("page");
    CurrentPageNO  = Integer.parseInt(CurrentPageNO_String);
    }
    catch (ClassCastException theException) {
    //(String)null;
    }
    catch (NumberFormatException theException) {
    // Integer.parseInt ("abc");
    }
    catch (NullPointerException theException) {
    }
    try {
    java.util.Collection theOperationLogReport = (java.util.Collection)session.getAttribute("theOperationLogReport");
    TotalPageNumber = theOperationLogReport.size()/NumberPerPage + 1;
    }
    catch (ClassCastException theException) {
    //(String)null;
    }
    catch (NumberFormatException theException) {
    // Integer.parseInt ("abc");
    }
    catch (NullPointerException theException) {
    } if (CurrentPageNO<=0) CurrentPageNO = 1;
    if (CurrentPageNO>TotalPageNumber) CurrentPageNO = TotalPageNumber; BeginIndexOfRecords  = (CurrentPageNO-1) * NumberPerPage + 1;
    EndIndexOfRecords  = CurrentPageNO * NumberPerPage;%><logic:present name="theOperationLogReport" scope="session">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td>查询时间段:<bean:write name="reportForm" property="beginTime_String"/><%----%> ~ <bean:write name="reportForm" property="endTime_String"/> <%----%>
    </td> <td>第 <%= CurrentPageNO %> 页,共 <%= TotalPageNumber %> 页</td>
    <%--
    <td><%= BeginIndexOfRecords %>~<%= EndIndexOfRecords %></td>
    <td>每页 <%= NumberPerPage %> 条记录</td>
    --%>
    <td>
    <% if (CurrentPageNO != 1) {%><a href="OperationLogReport.jsp?page=1">首页</a>
    <%}else {%><font color=gray>首页</font><%}%>
    </td>
    <td>
    <% if (CurrentPageNO != 1) {%>
    <a href="OperationLogReport.jsp?page=<%=(CurrentPageNO-1)<=0?1:(CurrentPageNO-1)%>">上一页</a>
    <%}else {%><font color=gray>上一页</font><%}%>
    </td>
    <td>
    <% if (CurrentPageNO != TotalPageNumber) {%>
    <a href="OperationLogReport.jsp?page=<%=(CurrentPageNO+1)%>">下一页</a>
    <%}else {%><font color=gray>下一页</font><%}%>
    </td>
    <td>
    <% if (CurrentPageNO != TotalPageNumber) {%>
    <a href="OperationLogReport.jsp?page=<%=TotalPageNumber%>">尾页</a>
    <%}else {%><font color=gray>尾页</font><%}%>
    </td>
    </tr>
    </table>
    <br>
      <table width="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#999999">
       <tr class="tableTitleYellow">
    <td height="22">序号</td>
    <td>操作时间</td>
    <td>用户</td>
    <td>操作</td>
    <td>IP地址(操作地点)</td>
       </tr>
    <%
    int RecordCount = 0; // for 分页处理……
    %>
       <logic:iterate name="theOperationLogReport" scope="session" id="aReportItem">
    <%
    // 分页处理
    RecordCount ++;
    //if (i<BeginIndexOfRecords || i>EndIndexOfRecords) continue;
    if (RecordCount>=BeginIndexOfRecords && RecordCount<=EndIndexOfRecords)
    {
    %>
       <tr class="tableRow">
    <td><%=RecordCount%>
    <td><bean:write name="aReportItem" property="timeStamp"/> <%--  显示<时间段> --%> </td>
    <td><bean:write name="aReportItem" property="userID_String"/> <%--  <用户ID> --%> </td>
    <td><%--  <操作类型> --%>
    <%
    BeanClass_HtmlLabelValue theLableValueBean = null;
    BeanClass_OperationLogReportItem theReportItem = (BeanClass_OperationLogReportItem)aReportItem;
    for (i=0; i<AllOperationTypes.size(); i++)
    {
    theLableValueBean = (BeanClass_HtmlLabelValue)AllOperationTypes.get(i);
    if (theLableValueBean.getValue().equals(theReportItem.getOperationType()))
    {
    out.print(theLableValueBean.getLabel());
    break;
    }
    }
    %>
    </td>
    <td><bean:write name="aReportItem" property="IPAddress"/> <%--  <IP 地址> --%> </td>
       </tr>
    <%
    } // 分页处理
    %>
       </logic:iterate>
      </table>
    </logic:present>