1. 程式人生 > >表單的分頁

表單的分頁

對於資料庫表的記錄太多時,需要進行分頁顯示。
1:在AdminOperate.java檔案中定義兩個方法分別獲取資料庫中表的總記錄數(totalrecoders)和特定記錄數的集合

public int getTotalRecorders() throws SQLException {
        int counts = 0;
        Connection conn = DAO.getCon();
        String SQL = "select count(*) from user";
        Statement st = conn.createStatement
(); ResultSet rs = st.executeQuery(SQL); rs.next(); counts = rs.getInt(1); return counts; } //選擇從第((pagenumber-1)*pagesize+1))條記錄開始到((pagenumber-1)*pagesize+1))+pagesize條記錄 public List<User> getPageUser(int pagenumber, int pagesize) { List<User> users = new ArrayList<User>();
ResultSet rs = null; Connection con = DAO.getCon(); PreparedStatement ps = null; String SQL = "select * from user limit ?,?"; try { ps = DAO.getPre(con, SQL); ps.setInt(1, ((pagenumber-1)*pagesize+1)); ps.setInt(2, pagesize);
rs = ps.executeQuery(); while(rs.next()) { User u = new User(); u.setAge(rs.getString("age")); u.setBirth(rs.getString("birth")); u.setId(rs.getString("id")); u.setMail(rs.getString("mail")); u.setName(rs.getString("name")); u.setSex(rs.getString("sex")); u.setPassword(rs.getString("password")); users.add(u); } } catch (SQLException e) { e.printStackTrace(); } finally { try { ps.close(); con.close(); rs.close(); } catch (SQLException e) { e.printStackTrace(); } } return users; }

2:在表單中顯示

<%@ page language="java" import="java.util.*" import="admin.*" import="usermanager.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>"> 
    <title>My JSP 'MyJsp.jsp' starting page</title> 
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <body>
      <%!
      private static final int pagesize = 6;
      %>
     <%--  使用<%! %>定義的變數編譯成servlet後成為servlet的私有成員變數,
      在servlet環境中,對於一個JSP頁只允許有一個結果servlet的例項在執行和響應所有的頁面請求。
      因此,在結果servlet中所有的成員變數可以被所有的請求所共享,所以只要成員變數沒有重新賦值,下次訪問該頁面時仍然不變。
      而在<% %>中定義的變數編譯成servlet後成為service()方法的一個本地變數,
      而service()方法中的本地變數只能每響應一次請求就重建一次。 --%>
      <% 
      //總的頁數
        int totalpages = 0;
        String strpgnb = request.getParameter("pagenumber");
        //頁碼初始化為1
        int pagenumber = 1;
        if(strpgnb != null && strpgnb != "null") {
            pagenumber = Integer.parseInt(strpgnb);
        }
        AdminOperate ao = new AdminOperate();
        //返還總的頁數 (totalrecoders+pagesize-1)/pagesize
        totalpages = (ao.getTotalRecorders()+pagesize-1)/pagesize;
        if(pagenumber>totalpages) {
            pagenumber = totalpages;
        }
        if(pagenumber<=0) {
            pagenumber = 1;
        }
        List<User> list = ao.getPageUser(pagenumber, pagesize);
       %>
     <center><h1>後臺管理</h1></center>
     <a href = "admin/adminlogin.jsp">返回</a>
     <H2>歡迎你:<%=admin %></H2>
     <table align = "center" width= "80%" border = "1">
       <tr>
            <td>工號</td><td>姓名</td><td>年齡</td><td>出生日期</td><td>性別</td><td>郵箱</td><td>處理</td>
       </tr>
    <%          
        if (list != null) {
            for(int i=0; i<list.size(); i++) {
                User u = list.get(i);
     %>
     <tr>
        <th><%=u.getId() %></th>
            <th><%=u.getName()%></th>
            <th><%=u.getAge() %></th>
            <th><%=u.getBirth() %></th>
            <th><%=u.getSex() %></th>
            <th><%=u.getMail() %>
        <th><a href="deluser?name=<%=u.getName()%>">刪除</a>
        <a href="modifyuser?name=<%=u.getName() %>">修改</a></th>
    </tr>
    <% 
    } 
    }
    %>
   </table>
   <center><%=totalpages%>頁&nbsp; 
  <form action="admin/adminloginsucessful.jsp"><input name="pagenumber" width="10px" type="text"value="<%=pagenumber %>" >頁 <input type="submit"  value="跳轉" ></form>&nbsp; 
   <a href="admin/adminloginsucessful.jsp?pagenumber=<%=(pagenumber-1)%>">上一頁</a>&nbsp; 
   <a href="admin/adminloginsucessful.jsp?pagenumber=<%=(pagenumber+1)%>">下一頁</a>&nbsp;
   <a href="admin/adminloginsucessful.jsp?pagenumber=<%=totalpages%>">最後一頁</a>&nbsp; </center>
  </body>
</html>

執行結果圖
執行結果圖