<%@ page import="java.sql.*"  pageEncoding="utf-8"%>
<%@ page language="java" import="java.lang.Math.*" %><%@ page contentType="text/html"%>
<jsp:useBean id="conn" scope="page"  class="db.db"/>  <%!
   
  ResultSet rs=null;
  ResultSet rsTmp=null;
  String sql="";
  int PageSize=6;
  int Page=1;
  int totalPage=1;
  String str="";
   
   public String ShowOnePage(ResultSet rs,int Page,int PageSize){
   str="";
   //先将记录指针定位到相应的位置
   try{
   rs.absolute((Page-1)*PageSize+1);
   }catch(SQLException e){ 
   }
   for(int iPage=1;iPage<=PageSize;iPage++ ){
   str+=RsToGbook(rs);
   try{
       if(!rs.next())break;
   }catch(Exception e){
         System.out.println("Locate Current Page Error!");
      }
   }
   return str;
   }
  //显示单行记录子模块
  
  
  public String RsToGbook(ResultSet rs){
    String tt="";
    String email="";
    String homepage="";
    String name="";
    String sex="";
    String subject="";
    String memo="";
    String gtime="";
    try{
    email=rs.getString("email");
    homepage=rs.getString("homepage");
    name=rs.getString("name");
    sex=rs.getString("gender");
    subject=rs.getString("title");
    memo=rs.getString("content");
    gtime=rs.getString("gtime");
 
     if(!email.equals("")){
     email="<A href='mailto:"+email+"'><img src='images/email.gif' border='0' alt='电子邮件信箱'></A>";
     }
     if(homepage.equals("http://")||homepage.equals("")){
     homepage="";
     }else{
     homepage="<A href='"+homepage+"'><img src='images/homepage.gif' border='0' alt='首页'></A>";
     }
       
       tt+="<table border='0' width='70%'>";
       tt+="<tr><td><table border='0'><tr><td><img src='images/buttom-g.gif'>";
       tt+=name+"</td><td>";
       tt+=email+homepage;
       tt+="</td><td><font style='font:9pt 宋体'>[</font><font style='font:9pt 宋体'color='bule'>"+sex;
       
       tt+="</font><font style='font:9pt 宋体'>]</font></td></tr></table></td></tr>";
       tt+="<tr><td></td></tr>";
       tt+="<tr><td colspan='4'>"+"<img src='images/file.gif' border='0'alt='主题'>"+subject+"</td></tr>";
       tt+="<tr><td colspan='4'>"+"<img src='images/write.gif' border='0'alt='留言'>"+memo+"</td></tr>";
       tt+="<tr><td colspan='4'><font face='Arial' size='1'>["+gtime+"]</td></tr>";
       tt+="<hr border='1' width='100%'></table>";
      }catch(SQLException e){}
       return tt;
       }
  %>
  <%
  
  
  sql="select * from gbook order by gtime desc";
  try{
  rs=conn.getBySql(sql);//修正
  }catch(Exception e){
  out.println("访问数据库出错!");
  }
  rsTmp=conn.getBySql("select count(*) as mycount from gbook");//修正
   rsTmp.next();
   int totalrecord=rsTmp.getInt("mycount");
   if(totalrecord%PageSize==0)totalPage=totalrecord/PageSize;//如果是当前页的整数倍
   else totalPage=(int)Math.floor(totalrecord/PageSize)+1;
     if(totalPage==0)totalPage=1;
      rsTmp.close();
       try{
       if(request.getParameter("page")==null||request.getParameter("page").equals(""))//修改page
            Page=1;
         else
           Page=Integer.parseInt(request.getParameter("page"));// 修改。。
           }catch(java.lang.NumberFormatException e){
           //捕获用户从浏览器地址栏直接输入Page=sdfsdf所造成的异常
               Page=1;
           }
           if(Page<1)Page=1;
           if(Page>totalPage)Page=totalPage;
          
            %>
            <html>
            <body bgcolor="#ffffff">
            <div align="center">
            <img src="images/gbook.gif" width="374" height="93"><br>
            [<a href="form.html"><font color="red" onmouseover="this.style.color='#0000bb'" onmouseout="this.style.color='red'">我要留言</font></a>]
            <%
               out.println((ShowOnePage(rs,Page,PageSize)));
              %>
             <form action="liuyan.jsp" method="get">
             <%
             if(Page!=1){
             out.println("<A href=liuyan.jsp?page=1>第一页</A>");
             out.println("<A href=liuyan.jsp?page="+(Page-1)+">上一页</A>");
             
             }
             if(Page!=totalPage){
             out.println("<A href=liuyan.jsp?page="+(Page+1)+">下一页</A>");
             out.println("<A href=liuyan.jsp?page="+totalPage+">最后一页</A>");
             } 
            rs.close();
             %>
             <p>输入页数:<input type="text" name="Page" size="3">页数:<font color="red"><%=Page%>/<%=totalPage%></font>
             </p>
             </form>
             
             <a href="form.html">返回留言表单</a><br><br>
             </div>
             
  </body>
</html>

解决方案 »

  1.   

    <%@ page contentType="text/html;charset=GBK"%>
    <%@ page import="java.sql.*"%>
    <html>
    <head><title>分页数据显示</title></head>
    <body>
    <center>
    <%
    int curpage=0;//当前页
       int total_page=0;//总页数
       int max_rows=25;//每页显示30行数据
       int max_line=0;//总的行数
       int number=0;
       String str=request.getParameter("curpage");
       if(str==null || str.equals("")){
       number=(curpage)*max_rows;
       curpage=1;
       }else{
          curpage=Integer.parseInt(str);
      number=(curpage-1)*max_rows;
       }
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
      String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=News";
      Connection con=DriverManager.getConnection(url,"sa","");
      String sql="select count(*) from bookInfo";
      PreparedStatement pstmt=con.prepareStatement(sql);
      ResultSet rs=pstmt.executeQuery();
       rs.next();
       max_line=rs.getInt(1);
       System.out.println(max_line);   if(max_line%max_rows==0)
       total_page=max_line/max_rows;
       else
       total_page=max_line/max_rows+1;
     
       String page_sql="select top "+max_rows+" * from bookInfo where id not in(select top "+number+" id from bookInfo)";
       //String page_sql="select top ? * from bookInfo where id not in(select top ? id from bookInfo)";
       pstmt=con.prepareStatement(page_sql);
       //pstmt.setInt(1,max_rows);
       //pstmt.setInt(2,number);
       rs=pstmt.executeQuery();
    %>
     <h1>分页数据显示</h1>
     <table border=1>
      <tr>
      <td>id 号</td>
      <td>Title</td>
      <td>authors</td>
      <td>publish_date</td>
      </tr>
      <%
       while(rs.next()){
        %>
    <tr>
     <td><%=rs.getString("id")%></td>
     <td><%=rs.getString("title")%></td>
     <td><%=rs.getString("authors")%></td>
     <td><%=rs.getString("date")%></td>
    </tr>
      <%
       }
      %>
     </table>
      <a href="page.jsp?curpage=<%=1%>">第一页</a>&nbsp;&nbsp;
     <%
        if(curpage==1){
    %>
    上一页
    <%
     }else{
    %>
     <a href="page.jsp?curpage=<%=curpage-1%>">上一页</a>&nbsp;&nbsp;
     <%
     }
     %>
     
     
     <%
      if(curpage==total_page){
     %>
      下一页
     <%
     }else{
     %>
     <a href="page.jsp?curpage=<%=curpage+1%>">下一页</a>&nbsp;&nbsp;
     <%
     }
     %>
     <a href="page.jsp?curpage=<%=total_page%>">最后一页</a>&nbsp;&nbsp;
     </center>
    </body>
    </html>