JSP+servlet+javabean实现分页

 
  1. package bean;
  2. import java.util.ArrayList;
  3. /**
  4. * 分页bean
  5. *
  6. *@author micron E-mail: 328074337@qq.com
  7. *
  8. */
  9. public class checkPage implements java.io.Serializable{
  10.     public checkPage() {
  11.         try {
  12.             jbInit();
  13.         } catch (Exception ex) {
  14.             ex.printStackTrace();
  15.         }
  16.     }
  17.     public int queryPageNo;       // 请求的页号
  18.     public int rowsPerPage ;    // 每页的行数
  19.     public String querySql ;   // 查询的Sql语句
  20.     public int totalPage;      // 总页数
  21.     public int totalRows;      // 总行数
  22.     public int lastPageRows;   // 最后一页的行数
  23.     public ArrayList resultDataVec;  // 显示在当前页面中的数据
  24.     public int getQueryPageNo() {
  25.         return queryPageNo;
  26.     }
  27.     public int getTotalPage() {
  28.         return totalPage;
  29.     }
  30.     public int getTotalRows() {
  31.         return totalRows;
  32.     }
  33.     public int getRowsPerPage() {
  34.         return rowsPerPage;
  35.     }
  36.     public String getQuerySql() {
  37.         return querySql;
  38.     }
  39.     public ArrayList getResultDataVec() {
  40.         return resultDataVec;
  41.     }
  42.     public int getLastPageRows() {
  43.         return lastPageRows;
  44.     }
  45.     public void setQueryPageNo(int queryPageNo) {
  46.         this.queryPageNo = queryPageNo;
  47.     }
  48.     public void setTotalPage(int totalPage) {
  49.         this.totalPage = totalPage;
  50.     }
  51.     public void setTotalRows(int totalRows) {
  52.         this.totalRows = totalRows;
  53.     }
  54.     public void setRowsPerPage(int rowsPerPage) {
  55.         this.rowsPerPage = rowsPerPage;
  56.     }
  57.     public void setQuerySql(String querySql) {
  58.         this.querySql = querySql;
  59.     }
  60.     public void setResultDataVec(ArrayList resultDataVec) {
  61.         this.resultDataVec = resultDataVec;
  62.     }
  63.     public void setLastPageRows(int lastPageRows) {
  64.         this.lastPageRows = lastPageRows;
  65.     }
  66.     private void jbInit() throws Exception {
  67.     }
  68. }
    1. package bean;
    2. import java.sql.*;
    3. import java.util.ArrayList;
    4. import java.util.Vector;
    5. /**
    6. * 数据操作bean
    7. *
    8. *@author micron E-mail: 328074337@qq.com
    9. *
    10. */
    11. public class doBossBean {
    12.     private Connection conn = null;
    13.     private ResultSet rs = null;
    14.     private PreparedStatement pst = null;
    15.     private Statement stmt=null;        
    16.     public doBossBean() {
    17.         try
    18.                 {
    19.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    20.                     conn = DriverManager.getConnection("jdbc:odbc:newhotel");
    21.                 }
    22.                 catch (SQLException ex)
    23.                 {
    24.                     System.out.println(ex.getMessage() + "路径错误");
    25.                 }
    26.                 catch (ClassNotFoundException ex)
    27.                 {
    28.                     System.out.println(ex.getMessage() + "驱动错误");
    29.                 }    
    30.     }  
    31.      
    32.     
    33.     public int getTotalRows(String sql) {
    34.            int count = 0;
    35.            try {
    36.                this.stmt = conn.createStatement();
    37.                rs = stmt.executeQuery(sql);
    38.                while (rs.next()) {
    39.                    count++;
    40.                }
    41.            } catch (SQLException ex) {
    42.             ex.printStackTrace();
    43.              System.out.print("getTotalRows errors"+ex.getMessage());
    44.            } 
    45.            return count;
    46.        }
    47.    public Vector list_room(String username, int queryPageNo,checkPage pageBean,int rowsPerPage ){
    48.         Vector vc=new Vector();
    49.       
    50.        String sql="select r_number,r_photo,if_book,r_area,r_price,r_facility,insert_time from room_info where username='"+username+"' order by insert_time desc";
    51.       
    52.           queryPageNo=pageBean.getQueryPageNo(); // 请求的页号
    53.           System.out.print("queryPageNo:"+queryPageNo+"||");
    54.           rowsPerPage=pageBean.getRowsPerPage();// 每页的行数
    55.          if( pageBean.getQuerySql()!= null )
    56.              sql = pageBean.getQuerySql();  // 如果有查询语句,则用指定的查询语句
    57.                  // 统计总行数
    58.          int totalRows = getTotalRows(sql);  //向getTotalRows传递sql语句,返回总行数
    59.          pageBean.setTotalRows(totalRows);
    60.                  // 统计总页数
    61.          int totalPage = totalRows % rowsPerPage == 0 ? totalRows / rowsPerPage :totalRows / rowsPerPage + 1;
    62.          pageBean.setTotalPage(totalPage);
    63.                  // 统计最后一页的行数
    64.          int lastPageRows = totalRows % rowsPerPage == 0 ? rowsPerPage :totalRows % rowsPerPage;        
    65.          pageBean.setLastPageRows(lastPageRows);
    66.       
    67.        try{
    68.          this.stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
    69.          rs=stmt.executeQuery(sql);   
    70.           int skipRows = (queryPageNo - 1) * rowsPerPage;
    71.           for (int i = 0; i < skipRows; i++)
    72.           rs.next();
    73.           int count = rowsPerPage;
    74.          while( rs!=null && rs.next() && count>0)         
    75.          {  
    76.             bossBean bos=new bossBean();
    77.             bos.setR_number(rs.getString("r_number"));
    78.             bos.setR_photo(rs.getString("r_photo"));
    79.             bos.setR_area(rs.getString("r_area"));
    80.             bos.setR_price(rs.getString("r_price"));
    81.             bos.setR_facility(rs.getString("r_facility"));
    82.             bos.setInsert_time(rs.getString("insert_time"));
    83.             bos.setIf_book(rs.getString("if_book"));          
    84.             vc.add(bos);  
    85.             count--;  
    86.             }
    87.        
    88.        }       
    89.        catch(Exception e)
    90.        {e.printStackTrace();}
    91.        return vc;
    92.       
    93.    }
    94.     
    95. }
      1. package servlet;
      2. import java.io.IOException;
      3. /**
      4. * 控制跳转的servlet
      5. *
      6. *@author micron E-mail: 328074337@qq.com
      7. *
      8. */
      9. import bean.*;
      10. import java.util.ArrayList;
      11. import java.util.Vector;
      12. import javax.servlet.*;
      13. import javax.servlet.http.*;
      14. public class select_list extends HttpServlet {
      15.     private static final String CONTENT_TYPE = "text/html; charset=GBK";
      16.     public void init(ServletConfig config) throws ServletException {
      17.         super.init(config);
      18.     }
      19.     public void service(HttpServletRequest request, 
      20.                         HttpServletResponse response) throws ServletException, IOException {response.setContentType(CONTENT_TYPE);
      21.          
      22.         HttpSession session=request.getSession();
      23.         String username=null;  
      24.        if(request.getParameter("send").equals("list_room")){
      25.                 username=(String)session.getAttribute("username");            
      26.                 
      27.                 checkPage pageBean=new checkPage();                                            
      28.                 int queryPageNo=1;  // 请求的页号
      29.                 int rowsPerPage=1;    // 每页的行数
      30.                 pageBean.setRowsPerPage(rowsPerPage);
      31.                 if(request.getParameter("action")!=null){
      32.                                      
      33.                     if(request.getParameter("queryPageNo")!=null)
      34.                     {
      35.                         queryPageNo= Integer.parseInt(request.getParameter("queryPageNo"));
      36.                     }
      37.                    
      38.                         if(request.getParameter("action").equals("first")){
      39.                               queryPageNo=1;
      40.                               pageBean.setQueryPageNo(queryPageNo);
      41.                         }                    
      42.                      else if(request.getParameter("action").equals("back"))
      43.                         {
      44.                             queryPageNo -= 1;
      45.                             pageBean.setQueryPageNo(queryPageNo);
      46.                         }                      
      47.                         else if(request.getParameter("action").equals("next"))
      48.                         { 
      49.                             queryPageNo += 1;
      50.                             pageBean.setQueryPageNo(queryPageNo);
      51.                         }
      52.                         else if(request.getParameter("action").equals("check"))
      53.                         {   String check=request.getParameter("queryPageNo");
      54.                             queryPageNo=Integer.valueOf(check);
      55.                             pageBean.setQueryPageNo(queryPageNo);
      56.                         }
      57.                         else if(queryPageNo<=1){
      58.                             queryPageNo=1;
      59.                             pageBean.setQueryPageNo(queryPageNo);
      60.                         }
      61.                         else if(request.getParameter("action").equals("last"))
      62.                         {   String no=request.getParameter("queryPageNo");
      63.                             queryPageNo=Integer.valueOf(no);
      64.                             System.out.print("queryPageNo"+queryPageNo);
      65.                             pageBean.setQueryPageNo(queryPageNo);
      66.                         }
      67.                        
      68.                     }
      69.                 
      70.                 Vector result=db.list_room(username,queryPageNo,pageBean,rowsPerPage);
      71.                 request.getSession().setAttribute("list_room",(Object)result); 
      72.                 request.setAttribute("queryPageNo",queryPageNo);
      73.                 request.setAttribute("totalPage",pageBean.getTotalPage());
      74.                 int pagecount=pageBean.getTotalPage();//得到总页数              
      75.                 request.setAttribute("pagecount",pagecount);
      76.                 int currentPage=pageBean.getQueryPageNo();//得到当前页
      77.                 request.setAttribute("currentPage",currentPage);
      78.                 
      79.                 request.getSession().setAttribute("boss_info",null);  
      80.                 request.getSession().setAttribute("search",null);
      81.                 request.getSession().setAttribute("addroom",null);
      82.                 request.getSession().setAttribute("upload_success",null);
      83.                 request.getSession().setAttribute("nullresult",null); 
      84.                 RequestDispatcher rd=request.getRequestDispatcher("trader_index.jsp");
      85.                 rd.forward(request,response);
      86.                 
      87.             }       
      88.             
      89.         }
      90.        
      91.                             
      92.     }
      93. }
        1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        2. "http://www.w3.org/TR/html4/loose.dtd">
        3. <%@ page contentType="text/html;charset=GBK"%>
        4. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
        5. <div align="center">     
        6.           
        7.     <c:if test="${not empty sessionScope.list_room}">
        8.       <c:forEach items="${sessionScope.list_room}" var="listRoom">
        9.       <table width="80%" border="1" align="center" bordercolor="#FF6600">
        10.   <tr>
        11.     <td width="89" height="31">房间编号:</td>
        12.     <td width="92">
        13.               ${pageScope.listRoom.r_number}
        14.             </td>
        15.     <td width="82">预定信息:</td>
        16.     
        17.     
        18.       <c:if test="${pageScope.listRoom.if_book ==null}">
        19.        <td width="99" >
        20.       <font color="#e70000">没有预定</font><img src="Image/sad.jpg" height="25"
        21.                                            width="25"></img>
        22.       </td>
        23.       </c:if>    
        24.        <c:if test="${pageScope.listRoom.if_book !=null}">
        25.          <td width="99" bgcolor="Yellow">
        26.       <font color="#101010">已经预定</font><img src="Image/happy.jpg" height="25"
        27.                                            width="25"></img>
        28.       </td>
        29.       </c:if>   
        30.     
        31.    
        32.   </tr>
        33.   <tr>
        34.     <td height="33">房间价格:</td>
        35.     <td>
        36.               ${pageScope.listRoom.r_price}
        37.             </td>
        38.     <td>房间面积:</td>
        39.     <td>
        40.               ${pageScope.listRoom.r_area}
        41.             </td>
        42.   </tr>
        43.   <tr>
        44.     <td height="36">发布时间:</td>
        45.     <td colspan="3">
        46.               ${pageScope.listRoom.insert_time}
        47.             </td>
        48.   </tr>
        49.   <tr>
        50.     <td height="56">房间描述:</td>
        51.     <td colspan="3">
        52.               ${pageScope.listRoom.r_facility}
        53.             </td>
        54.   </tr>
        55.   <tr>
        56.     <td height="113">图片:</td>
        57.     <td colspan="3"><img src="${pageScope.listRoom.r_photo}" height="150"
        58.                          width="150"></img></td>
        59.             
        60.           </tr>
        61. </table>
        62. </c:forEach>
        63. <table width="80%">
        64.       <tr>
        65.         <td bgcolor="#E0FFFF" onmouseover="javascript:this.bgColor='#D3D3D3'" onmouseout="javascript:this.bgColor='#E0FFFF'">
        66.           <a href="select_list?send=list_room&action=first&queryPageNo=${requestScope.queryPageNo}">首页</a>
        67.         </td>
        68.         <td bgcolor="#E0FFFF" onmouseover="javascript:this.bgColor='#D3D3D3'" onmouseout="javascript:this.bgColor='#E0FFFF'">
        69.         <c:if test="${requestScope.currentPage>1}">
        70.              <c:if test="${requestScope.queryPageNo==1}">上一页</c:if>
        71.               <c:if test="${requestScope.queryPageNo!=1}">
        72.                 <a href="select_list?send=list_room&action=back&queryPageNo=${requestScope.queryPageNo}">上一页</a>
        73.                </c:if>
        74.         </c:if>
        75.         </td>
        76.         
        77.         <c:forEach begin="1" end="${requestScope.totalPage}" varStatus="status">
        78.          <td width="5%" bgcolor="#E0FFFF" onmouseover="javascript:this.bgColor='#D3D3D3'" onmouseout="javascript:this.bgColor='#E0FFFF'">
        79.             <c:if test="${status.index == pageNo}">
        80.               <span class="currentPage">${status.index}</span>
        81.             </c:if>
        82.             <c:if test="${status.index != pageNo}">
        83.             <span><a href="select_list?send=list_room&action=check&queryPageNo=${status.index}" >${status.index}</a></span>
        84.             </c:if>
        85.        </td>
        86.       </c:forEach>
        87.        
        88.         <td bgcolor="#E0FFFF" onmouseover="javascript:this.bgColor='#D3D3D3'" onmouseout="javascript:this.bgColor='#E0FFFF'">
        89.         <c:if test="${requestScope.currentPage<requestScope.totalPage}">       
        90.             <c:if test="${requestScope.queryPageNo!=requestScope.pagecount}">
        91.              <a href="select_list?send=list_room&action=next&queryPageNo=${requestScope.queryPageNo}">下一页</a>
        92.            </c:if>
        93.        </c:if>
        94.         </td>
        95.           <td bgcolor="#E0FFFF" onmouseover="javascript:this.bgColor='#D3D3D3'" onmouseout="javascript:this.bgColor='#E0FFFF'">
        96.             <a href="select_list?send=list_room&action=last&queryPageNo=${requestScope.totalPage}">
        97.               尾页
        98.             </a>
        99.           </td>
        100.           <td bgcolor="#E0FFFF">
        101.           共${requestScope.totalPage}页
        102.         </td>
        103.         <td>   
        104.        
        105.         <select name="pageNo">
        106.          <c:forEach begin="1" end="${requestScope.totalPage}" varStatus="status">
        107.          <option value="${status.index}" ${status.index == pageNo ? 'selected' : ''}>
        108.          ${status.index}
        109.          </option>         
        110.         </c:forEach>
        111.         </select>     
        112.       
        113.         </td>
        114.       </tr>
        115.     </table>
        116. </c:if>
        117.   </p>
        118. </div> 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值