分页
package isa.controller; import isa.model.DeptBean; import isa.model.DeptBeanCl; import java.io.IOException; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DeptSAServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 希望得到用户的pageNow String p=request.getParameter("pageNow"); System.out.println("a"); // 创建DeptBeanCl方法 DeptBeanCl dbc=new DeptBeanCl(); System.out.println("b"); // 在跳转到xhshow.jsp页面时,就把要显示的数据给Welcome准备好 HashMap<String,DeptBean> hm=dbc.getDepts(); System.out.println("c"); // 将al,pageCount放入request中 request.setAttribute("result", hm); request.setAttribute("pageNow", p); System.out.println("d"); // 转发,效率高,request中的对象还可以在下一页面中使用 request.getRequestDispatcher("xhshow.jsp").forward(request, response); } }
<%@ page language="java" import="java.util.*,isa.model.DeptBean" contentType="text/html;charset=gb2312" pageEncoding="gb2312"%> <html> <head> <base href="<%=basePath%>"> <title></title> <script language="javascript"> function fsubmit(obj){ obj.submit(); } function freset(obj){ obj.reset(); } </script> </head> <body bgcolor="#ccffff"> <% int pageSize=5; //要显示的信息从Request中取 System.out.println(pageSize); HashMap<String,DeptBean> hm=(HashMap<String,DeptBean>)request.getAttribute("result"); int size=hm.size(); System.out.println(size); //接收用户希望显示的页数(pageNow) String s_pageNow=(String)request.getAttribute("pageNow"); System.out.println(s_pageNow); int pageNow=Integer.parseInt(s_pageNow); System.out.println(pageNow); int pageCount=(int)(size+4)/pageSize; System.out.println(pageCount); %> <table width="750" height="250" align="center" cellpadding="0" cellspacing="0" border="1"> <tr > <td align="center" valign="middle">部门号</td><td valign="middle" align="center">部门名</td><td align="center" valign="middle">部门人数</td><td align="center" valign="middle">负责人</td><td align="center" valign="middle">副负责人</td><td align="center" valign="middle">联系电话</td> </tr> <% for(int i=(pageNow-1)*pageSize+1;i<pageNow*pageSize+1;i++){ if (hm.containsKey(i+"")) { DeptBean db=(DeptBean)hm.get(i+""); %> <tr> <td align="center" valign="middle"><%=db.getdNo() %></td><td align="center" valign="middle"><%=db.getdName() %></td><td align="center" valign="middle"><%=db.getdNum() %></td><td align="center" valign="middle"><%=db.getdMinister() %></td><td align="center" valign="middle"><%=db.getdVice() %></td><td align="center" valign="middle"><%=db.getdTel() %></td> </tr> <%} }%> <tr> <td colspan="<%=6 %>" scope="row" align="center" valign="bottom"> <table align="center" border="0"> <tr> <td width="*"></td> <% if(pageNow!=1){ %> <td align="center" valign="middle"> <form id="form1" name="form1" method="post" action="DeptSAServlet"> <input type="hidden" name="pageNow" value="<%=(pageNow-1) %>" /> <a href="javascript:fsubmit(document.form1);"> <img border="0" src="imgs/pre.jpg" name="picture1" onmouseover="picture1.src='imgs/pre1.jpg'"onmouseout="picture1.src='imgs/pre.jpg'"></a> </form> </td> <% // out.println("<a href=DeptSBServlet?pageNow="+(pageNow-1)+">上一页</a>"); } for(int i=0;i<pageCount;i++){ %> <td align="center" valign="middle"> </td> <% // out.println("<a href=DeptSBServlet?pageNow="+i+">["+i+"]</a>"); } if(pageNow!=pageCount){ %> <td align="center" valign="middle"> <form id="form2" name="form2" method="post" action="DeptSAServlet"> <input type="hidden" name="pageNow" value="<%=(pageNow+1) %>" /> <a href="javascript:fsubmit(document.form2);"> <img border="0" src="imgs/next.jpg" name="picture2" onmouseover="picture2.src='imgs/next1.jpg'"onmouseout="picture2.src='imgs/next.jpg'"></a> </form> </td> <% // out.println("<a href=DeptSBServlet?pageNow="+(pageNow+1)+">下一页</a>"); } %> <td width="*"></td> </tr> </table> </td> </tr> </table> </body> </html>
package isa.model; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.HashMap; public class DeptBeanCl { private Connection ct=null; private PreparedStatement ps=null; private ResultSet rs=null; // private int pageSize=3; // private int rowCount=0;//该值从数据库中查询 // private int pageCount=0;//该值是通过pageSize和rpwCount //// 关闭资源 public void close(){ try{ if(rs!=null) { rs.close(); rs=null; } if(ps!=null) { ps.close(); ps=null; } if(ct!=null) { ct.close(); ct=null; } }catch(Exception e){ e.printStackTrace(); } } //// 返回分页的总页数 // public int getpageCount(){ // System.out.println("H"); // try{ // ct=(new ConnDB()).getConn(); // //得到链接 // System.out.println("I"); // ps=ct.prepareStatement("select count(*) from dept"); // System.out.println("J"); // rs=ps.executeQuery(); // System.out.println("K"); // if(rs.next()){ // System.out.println("L"); // rowCount=rs.getInt(1); // } // System.out.println(rowCount); // pageCount=(int)(rowCount+2)/pageSize; // }catch(Exception e){ // e.printStackTrace(); // }finally{ // this.close(); // } // return pageCount; // } // 得到用户需要显示的用户信息(分页) public HashMap<String,DeptBean> getDepts(){ HashMap<String,DeptBean> hm=new HashMap<String,DeptBean>(); System.out.println("A"); try{ int n=1; // 到数据库去验证 ct=(new ConnDB()).getConn(); ps=ct.prepareStatement("select dNo,dName,dNum,dMinister,dVice,dTel from dept"); System.out.println("D"); rs=ps.executeQuery(); System.out.println("E"); while(rs.next()){ System.out.println("F"); DeptBean db=new DeptBean(); db.setdNo(rs.getString(1)); db.setdName(rs.getString(2)); db.setdNum(rs.getInt(3)); db.setdMinister(rs.getString(4)); db.setdVice(rs.getString(5)); db.setdTel(rs.getString(6)); hm.put(n+"", db); n=n+1; System.out.println("G"); } }catch(Exception e){ System.out.println("WW"); e.printStackTrace(); }finally{ this.close(); } return hm; } public String getdNo(String n){ String no=null; try{ // 到数据库去验证用户 ct=(new ConnDB()).getConn(); ps=ct.prepareStatement("select dNo from dept where dName=?"); ps.setString(1, n); rs=ps.executeQuery(); while(rs.next()){ no=rs.getString(1); break; } }catch(Exception e){ e.printStackTrace(); }finally{ this.close(); } System.out.println(no); return no; } public boolean decreaseNum(String d){ boolean b=true; try{ // 到数据库去验证用户 ct=(new ConnDB()).getConn(); ps=ct.prepareStatement("update dept set dNum=dNum-1 where dNo=?"); ps.setString(1, d); ps.executeUpdate(); }catch(Exception e){ b=false; e.printStackTrace(); }finally{ this.close(); } return b; } public boolean addNum(String d){ boolean b=true; try{ // 到数据库去验证用户 ct=(new ConnDB()).getConn(); ps=ct.prepareStatement("update dept set dNum=dNum+1 where dName=?"); ps.setString(1, d); ps.executeUpdate(); }catch(Exception e){ b=false; e.printStackTrace(); }finally{ this.close(); } return b; } }