JSP 分页

在网上还是看到有人因为这个问题提问,我把我当时写出的分页代码粘贴出来.

  1. <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  6. <title>分页</title>
  7. </head>
  8. <body>
  9. <%
  10. try
  11. {
  12.     Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
  13.     String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs";   
  14.     Connection con=DriverManager.getConnection(url,"sa","sql");  
  15.     
  16.     int i=1;
  17.     int numPages=5;
  18.     String pages = request.getParameter("page");
  19.     int currentPage = 1;
  20.     int intPageCount=1;
  21.     currentPage=((pages==null)? 1 :(Integer.parseInt(pages)));
  22.     if (currentPage<1)
  23.         currentPage=1;
  24.         
  25.     String sql = "select count(au_id) as id from authors";
  26.     
  27.     Statement   stmt=con.createStatement();
  28.     ResultSet rs=stmt.executeQuery(sql);// 
  29.     if(rs.next())
  30.         intPageCount = rs.getInt("id");
  31.     rs.close();
  32.     
  33.     
  34.     int PageCount=(intPageCount%numPages==0)?(intPageCount/numPages):(intPageCount/numPages+1);
  35.     int nextPage;
  36.     int upPage;
  37.     nextPage = currentPage+1;
  38.     if (nextPage>=PageCount)
  39.         nextPage=PageCount;
  40.         
  41.     upPage = currentPage-1;
  42.     if (upPage<=1)
  43.         upPage=1;
  44.         
  45.     sql="select au_fname,contract,zip from authors";
  46.     
  47.     stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
  48.     rs=stmt.executeQuery(sql);
  49.     i=0;
  50.     while((i<numPages*(currentPage-1)) &&rs.next())
  51.     {
  52.         i++;
  53.     }
  54.     
  55.     while((i<numPages*currentPage)&&rs.next())
  56.     {
  57.         i++;
  58.         out.println("au_fname:"+rs.getString("au_fname")+"      contract"+rs.getString("contract")+"      zip:"+rs.getString("zip")+"<br/>");
  59.     }
  60.     rs.close(); 
  61.     stmt.close();                                   //(4)关闭
  62.     con.close(); 
  63.     
  64. %><p></p>合计:<%=currentPage%>/<%=PageCount%>
  65. <a href="fy.jsp?page=1">第一页</a><a href="fy.jsp?page=<%=upPage%>">上一页</a>
  66. <% 
  67. for(int j=1;j<=PageCount;j++)
  68. {
  69.     if(currentPage!=j)
  70.     { 
  71.     %> <a href="fy.jsp?page=<%=j%>">[<%=j%>]</a> <% 
  72.     }
  73.     else
  74.     { 
  75.         out.println(j);
  76.     }
  77. } %> 
  78. <a href="fy.jsp?page=<%=nextPage%>">下一页</a><a href="fy.jsp?page=<%=PageCount%>">最后页 </a>
  79. <%
  80. }
  81. catch(Exception e)
  82. {     
  83.     e.printStackTrace();    
  84. }
  85. %>
  86. </body>
  87. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值