<%@ page language="java" contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.lang.*" %>
<html>
<head>
<title>分页显示留言</title>
<link rel=stylesheet href="../style1.css">
</head>
<body>
<%
//加载数据库驱动
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//建立数据库连接
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=swpt";
String user="dike103";
String password="852300";
Connection aa=DriverManager.getConnection(url,user,password);
//创建数据库操作对象
Statement bb=aa.createStatement();
int PageSize; //一页显示的记录数 
int RowCount; //记录总数 
int PageCount; //总页数 
int intPage; //待显示页码 
int i,j; 
//设置每页显示的记录数 
PageSize = 5;
String strPage=request.getParameter("page");      
if(strPage==null)
{    
//表明在QueryString中没有page这一个参数,此时显示第一页数据      
intPage=1;      
}  
else
{    
//将字符串转换成整型      
intPage=Integer.parseInt(strPage);      
if(intPage<1)  intPage=1;  
}    
//获取记录总数      
String SQL="SELECT count(*) FROM  lyb";
ResultSet cc=bb.executeQuery(SQL);
//执行SQL语句并取得结果集      
cc.next();  
//记录集刚打开的时候,指针位于第一条记录之前      
RowCount=cc.getInt(1);      
//关闭结果集
cc.close();  
//记算总页数      
PageCount=(RowCount+PageSize-1)/PageSize;
//调整待显示的页码
if(intPage>PageCount)  intPage  =  PageCount; 
//设置获取数据SQL语句      
String dd="SELECT*FROM  lyb ORDER BY id DESC";
//执行SQL语句并取得结果集      
ResultSet ee=bb.executeQuery(dd);
//将记录指针定位到待显示页的第一条记录上      
i=(intPage-1)*PageSize;      
for(j=0;j<i;j++)  ee.next(); 
//显示数据      
i  =  0;      
while(i<PageSize && ee.next())
{  
%> 
<table width="75%" border="0" align=center>
  <tr  bgcolor=#336699 height="21"> 
    <td colspan="3"><font color="#FFFFFF"><b>::<%=ee.getString("username")%>的留言::</b></font>
时间:<%=ee.getString("time")%></td>
  </tr>
  <tr> 
    <td width="15%" rowspan="3"></td>
    <td width="17%">主题:</td>
    <td width="68%"><%=ee.getString("title1")%></td>
  </tr>
  <tr > 
    <td>邮件:</td>
    <td><%=ee.getString("email")%></td>
  </tr>
  <tr> 
    <td>内容:</td>
    <td><%=ee.getString("info")%></td>
  </tr><tr><td colspan=3><hr width=100% boder=1></td></tr>
<%
i++;  
}  
%>    
<tr align=center>      
<td  colspan=2  align=center>      
 <form action=add2.jsp method=post name=form6>
 回车转到第<input name="page" size=3 class=input_3>页</form> 第<%=intPage%>页    共<%=PageCount%>页   共有<%=RowCount%>条记录 
 <%
 if(intPage<PageCount)
 {
 %><a  href="add2.jsp?page=<%=intPage+1%>">下一页</a><%    
 }    
 %>        
 <%
 if(intPage>1)
 {
 %><a  href="add2.jsp?page=<%=intPage-1%>">上一页</a><%    
 }    
 %> 
 </td> 
 </tr>
 </table>  
<hr width=500 boder=1>     
<%      
//关闭结果集      
ee.close();      
//关闭SQL语句对象      
cc.close();      
//关闭数据库    
aa.close();    
%>
</body>
</html>

解决方案 »

  1.   

    <%@ page contentType="text/html; charset=GBK"
    import="java.sql.*"
    %>
    <%@ page errorPage="qhd_no.jsp"%>
    <%
    Object nn = session.getAttribute("user");
    Object ff = session.getAttribute("ywbh");
    String theType = request.getParameter("setType");
    String theStr = "产品展示";
    %>
    <jsp:useBean id="bean" scope="page" class="gcwd.DBConnection"/>
    <jsp:useBean id="strBean" scope="page" class="gcwd.StringBean"/>
    <%
    int rowCount=0;
    int intPage = 1;
    int pageCount =1;
    int pageSize =0;
    int i=0;
    int j =1;
    String query="";
    if(bean.dbConnection())
    {
    /********************************************************
    *这里根据前面传来不同的值,来判断SQL语句
    *然后再执行查询,计算总行数和总的页数
    ********************************************************/
    query = "select dhlsh,dhrq,jhze,bmbh,ddrq,skbl,czy,ztbz from kc_dhdj where bmbh='"+ff+"' and subString(dhlsh,1,2)='DH' order by dhlsh DESC"; ResultSet resultset = bean.executeQuery(query); if(resultset.next())
    {
    resultset.last();
    rowCount = resultset.getRow();
    resultset.first();
    intPage =  1;
    //转化接收到的页面信息
    String strPage = request.getParameter("page");
    if(strPage==null)
    {
    intPage=1;
    }
    else
    {
    intPage = java.lang.Integer.parseInt(strPage);
    if(intPage<1) intPage=1;
    } pageSize = 15; pageCount = 0;
    //判断总页数,如果整除,则为整除结果,否则,就加一
    if(rowCount / pageSize==0)
    pageCount = rowCount / pageSize;
    else
    pageCount = rowCount / pageSize + 1;
    //如果显示结果不够一页,则在将每页显示记录数设置为总记录数
    if(rowCount < pageSize) pageSize = rowCount+1;
    if(intPage>pageCount) intPage=pageCount;
    j=(intPage-1)*pageSize+1; }
    //<meta http-equiv="refresh"  content="30";URL="qhd.jsp">
    %>
    <html>
    <head> <link rel="stylesheet" href="c
    ss\css.css" type="text/css">
    <style type="text/css">
    <!---->
    </style>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <link href="css/css.css" rel="stylesheet" type="text/css">
    </head>
    <Script Language="javaScript">
    function add_new()
    {
    parent.frames("main").location.href = "qhd_input.jsp";
    }
    function delTo(Str)
    {
    if(window.confirm("你确定要删除此单吗?"))
    {
    Url="qhd_del.jsp?changeLsh="+Str ;
    window.showModalDialog(Url,"","dialogwidth:300px;dialogheight:180px;tools:no;stattus:no");
    document.location.reload();
    }
    }
    //parent.frames("main").location.reload()
    </Script>
    <body bgcolor="#fcfffa" background="img/left/test4.gif" text="#000000" scroll=auto ><table width=100% border="0" cellspacing="0" cellpadding="0" class="frmBox" align="center">
    <tr><td>
    <table width="100%" border="0"  class="">
    <tr>
    <td bgcolor="" height="20" class="frmBox">
    <font  class="font"><strong>请货单处理</strong></font><hr>
    </td>
    </tr>

    <tr bgcolor="" height=10><td align="right" class="frmBox">
    <input type=button width=40 value="    添加请货单   " onclick="add_new()" onmouseover="this.className='style2'" onmouseout="this.className='style1'"  class="style1">
    </td></tr>
    </table>

    </td></tr>
    <tr><td align="center">
          <table width=99% border="0" cellspacing="1" cellpadding="0" style="border:2 inset;line-height:3.0" bgcolor="#CCCCCC" >
              <tr bgcolor="#E1E1E1">
                      <td width=10% class="ed_btnStyle"><Strong>单号</strong></td>
                      <td width=10% class="ed_btnStyle"><Strong>订货日期</strong></td>
                      <td width=10% class="ed_btnStyle"><Strong>金额</strong></td>
                      <td width=25% class="ed_btnStyle"><Strong>单位</strong></td>
                      <td width=10% class="ed_btnStyle"><Strong>入单人</strong></td>
                      <td width=10% class="ed_btnStyle"><Strong>入单日期</strong></td>
                      <td width=10% class="ed_btnStyle"><Strong>状态</strong></td>                  <td width=15% class="ed_btnStyle"><Strong>详细信息</strong></td>
               </tr>
    <%
    /***********************************************************
    *利用前面求出的pageCount来为查询叶面分页
    * 注意,后面翻页的时候,注意传递不同的查询类型值
    ***********************************************************/

    resultset.absolute(j);
    while(i < pageSize && !resultset.isAfterLast())
    {
    String theLsh=strBean.changeString(resultset.getString("dhlsh"),0,14);
    String theDhrq = strBean.formatDate(strBean.changeString(resultset.getString("dhrq"),0,8));
    String theJhze = resultset.getString("jhze");
    String theBmbh = resultset.getString("bmbh");
    String theRdrq = strBean.formatDate(strBean.changeString(resultset.getString("ddrq"),0,8));
    String theSl = resultset.getString("skbl");
    String theCzy = resultset.getString("czy");
    String theZtbz = resultset.getString("ztbz");
    String theZt = "11";
    if(theZtbz.trim().equals("0"))
    theZt = "修改状态";
    if(theZtbz.trim().equals("1"))
    theZt = "请货状态";
    if(theZtbz.trim().equals("2"))
    theZt = "配货状态";
    //***********************查询部门资料***********************
    String theBmmc = theBmbh;
    ResultSet theRs = null;
    String theQuery = "SELECT bmmc FROM xt_bmzl WHERE bmbh = '"+theBmbh+"'";
    theRs = bean.executeQuery(theQuery);
    if(theRs.next())
    {
    theBmmc = theRs.getString("bmmc");
    }
    theRs.close();%>
             <tr bgcolor="#D0CEFB">
                          <td width=10% class="ed_btnStyle"><%=theLsh%></td>
                          <td width=10% class="ed_btnStyle"><%=theDhrq%></td>
                          <td width=10% class="ed_btnStyle" align="right"><%=theJhze%></td>
                          <td width=25% class="ed_btnStyle"><%=theBmmc%></td>
                          <td width=10% class="ed_btnStyle"><%=theCzy%></td>
                          <td width=10% class="ed_btnStyle"><%=theRdrq%></td>
                          <td width=10% class="ed_btnStyle" align="right"><%=theZt%></td>                      <td width=15% class="ed_btnStyle">&nbsp;&nbsp;<a href="qhd_print_view.jsp?changeLsh=<%=theLsh%>">详细</a>&nbsp;&nbsp;<a href="qhd_change.jsp?changeLsh=<%=theLsh%>">修改</a>&nbsp;&nbsp;<span style="cursor:hand" onclick="javaScript:delTo('<%=theLsh%>')"><font color="red"><u>删除</u></font></span></td>
             </tr>
    <%
    i = i + 1;
    resultset.next();
                } //while结束
                resultset.close();
                
    }
    %>
    <%

    for(int h=0;h<15-i;h++)
    {
    %>
    <tr bgcolor="#D0CEFB">
                          <td width=10% class="ed_btnStyle">&nbsp;</td>
                          <td width=10% class="ed_btnStyle">&nbsp;</td>
                          <td width=10% class="ed_btnStyle" align="right">&nbsp;</td>
                          <td width=25% class="ed_btnStyle">&nbsp;</td>
                          <td width=10% class="ed_btnStyle">&nbsp;</td>
                          <td width=10% class="ed_btnStyle">&nbsp;</td>
                          <td width=10% class="ed_btnStyle" align="right">&nbsp;</td>                      <td width=15% class="ed_btnStyle">&nbsp;</td>
             </tr>
    <%
    }
    %>
          </table>
      <table width=100% bgcolor="#E1E1E1">
       <tr>
    <td width="30%">
    第<%=intPage%>页/共<%=pageCount%>页
    </td>
    <td width="70%" align="right">
    <%if(intPage!=1){%>
    <a href="qhd.jsp?page=1">首页</a>
    <%
    }
    else
    {
    out.print("首页");
    }%>
    <%if(intPage>1){%>
    <a href="qhd.jsp?page=<%=intPage-1%>">上一页</a>
    <%
    }
    else
    {
    out.print("上一页");
    }%>
    <%if(intPage < pageCount){%>
    <a href="qhd.jsp?page=<%=intPage+1%>">下一页</a>
    <%
    }
    else
    {
    out.print("下一页");
    }%>
    <%if(intPage!=pageCount){%>
    <a href="qhd.jsp?page=<%=pageCount%>">尾页</a>
    <%
    }
    else
    {
    out.print("尾页");
    }%>
    </td>
    </tr>
      </table>
        </td>
      </tr>
    </table>
    </body>
    </html>
    <%
    bean.close();
    %>
      

  2.   

    目前最好的JSP分页技术http://www.fooldock.com/artshow.jsp?typeid=5&id=96