JSP分页实现 代码

来源:互联网 发布:淘宝官网网址 编辑:程序博客网 时间:2024/06/09 00:13
[html] view plain copy
  1. <%@ page contentType="text/html" pageEncoding="GB2312" language="java"%>  
  2. <%@ page import="java.sql.*"%>  
  3. <html>  
  4.     <head>  
  5.         <title>hello</title>  
  6.     </head>  
  7.     <body>  
  8.     <table border="1" spacing="2">  
  9. <%!  
  10.     public static final String DRIVER = "com.mysql.jdbc.Driver";  
  11.     public static final String USER = "root";  
  12.     public static final String PASS = "12345";  
  13.     public static final String URL = "jdbc:mysql://localhost:3306/MLDN";  
  14.     public static final int PAGESIZE = 5;  
  15.     int pageCount;  
  16.     int curPage = 1;  
  17. %>  
  18. <%  
  19.     //一页放5个  
  20.     String user = null;  
  21.     String pass = null;  
  22.     try{  
  23.         Class.forName(DRIVER);  
  24.         Connection con = DriverManager.getConnection(URL,USER,PASS);  
  25.         String sql = "SELECT empno,ename,job,hiredate,sal,comm FROM emp";  
  26.         PreparedStatement stat = con.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);  
  27.         ResultSet rs = stat.executeQuery();  
  28.         rs.last();  
  29.         int size = rs.getRow();  
  30.         pageCount = (size%PAGESIZE==0)?(size/PAGESIZE):(size/PAGESIZE+1);  
  31.         String tmp = request.getParameter("curPage");  
  32.         if(tmp==null){  
  33.             tmp="1";  
  34.         }  
  35.         curPage = Integer.parseInt(tmp);  
  36.         if(curPage>=pageCount) curPage = pageCount;  
  37.         boolean flag = rs.absolute((curPage-1)*PAGESIZE+1);  
  38.         out.println(curPage);  
  39.         int count = 0;  
  40.           
  41.         do{  
  42.             if(count>=PAGESIZE)break;  
  43.             int empno = rs.getInt(1);  
  44.             String ename = rs.getString(2);  
  45.             String job = rs.getString(3);  
  46.             Date hiredate = rs.getDate(4);  
  47.             float sal = rs.getFloat(5);  
  48.             int comm = rs.getInt(6);  
  49.             count++;  
  50.             %>  
  51.         <tr>  
  52.             <td><%=empno%></td>  
  53.             <td><%=ename%></td>  
  54.             <td><%=job%></td>  
  55.             <td><%=hiredate%></td>  
  56.             <td><%=sal%></td>  
  57.             <td><%=comm%></td>  
  58.         </tr>  
  59.             <%  
  60.         }while(rs.next());  
  61.         con.close();  
  62.     }  
  63.     catch(Exception e){  
  64.           
  65.     }  
  66. %>  
  67. </table>  
  68. <a href = "multipage.jsp?curPage=1" >首页</a>  
  69. <a href = "multipage.jsp?curPage=<%=curPage-1%>" >上一页</a>  
  70. <a href = "multipage.jsp?curPage=<%=curPage+1%>" >下一页</a>  
  71. <a href = "multipage.jsp?curPage=<%=pageCount%>" >尾页</a>  
  72. <%=curPage%>页/共<%=pageCount%>页  
  73.   
  74. </body>  
  75. </html> 
0 0
原创粉丝点击