- package bean;
- import java.util.ArrayList;
- /**
- * 分页bean
- *
- *@author micron E-mail: 328074337@qq.com
- *
- */
- public class checkPage implements java.io.Serializable{
- public checkPage() {
- try {
- jbInit();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public int queryPageNo; // 请求的页号
- public int rowsPerPage ; // 每页的行数
- public String querySql ; // 查询的Sql语句
- public int totalPage; // 总页数
- public int totalRows; // 总行数
- public int lastPageRows; // 最后一页的行数
- public ArrayList resultDataVec; // 显示在当前页面中的数据
- public int getQueryPageNo() {
- return queryPageNo;
- }
- public int getTotalPage() {
- return totalPage;
- }
- public int getTotalRows() {
- return totalRows;
- }
- public int getRowsPerPage() {
- return rowsPerPage;
- }
- public String getQuerySql() {
- return querySql;
- }
- public ArrayList getResultDataVec() {
- return resultDataVec;
- }
- public int getLastPageRows() {
- return lastPageRows;
- }
- public void setQueryPageNo(int queryPageNo) {
- this.queryPageNo = queryPageNo;
- }
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- public void setTotalRows(int totalRows) {
- this.totalRows = totalRows;
- }
- public void setRowsPerPage(int rowsPerPage) {
- this.rowsPerPage = rowsPerPage;
- }
- public void setQuerySql(String querySql) {
- this.querySql = querySql;
- }
- public void setResultDataVec(ArrayList resultDataVec) {
- this.resultDataVec = resultDataVec;
- }
- public void setLastPageRows(int lastPageRows) {
- this.lastPageRows = lastPageRows;
- }
- private void jbInit() throws Exception {
- }
- }
-
- package bean;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.Vector;
- /**
- * 数据操作bean
- *
- *@author micron E-mail: 328074337@qq.com
- *
- */
- public class doBossBean {
- private Connection conn = null;
- private ResultSet rs = null;
- private PreparedStatement pst = null;
- private Statement stmt=null;
- public doBossBean() {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- conn = DriverManager.getConnection("jdbc:odbc:newhotel");
- }
- catch (SQLException ex)
- {
- System.out.println(ex.getMessage() + "路径错误");
- }
- catch (ClassNotFoundException ex)
- {
- System.out.println(ex.getMessage() + "驱动错误");
- }
- }
- public int getTotalRows(String sql) {
- int count = 0;
- try {
- this.stmt = conn.createStatement();
- rs = stmt.executeQuery(sql);
- while (rs.next()) {
- count++;
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- System.out.print("getTotalRows errors"+ex.getMessage());
- }
- return count;
- }
- public Vector list_room(String username, int queryPageNo,checkPage pageBean,int rowsPerPage ){
- Vector vc=new Vector();
- 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";
- queryPageNo=pageBean.getQueryPageNo(); // 请求的页号
- System.out.print("queryPageNo:"+queryPageNo+"||");
- rowsPerPage=pageBean.getRowsPerPage();// 每页的行数
- if( pageBean.getQuerySql()!= null )
- sql = pageBean.getQuerySql(); // 如果有查询语句,则用指定的查询语句
- // 统计总行数
- int totalRows = getTotalRows(sql); //向getTotalRows传递sql语句,返回总行数
- pageBean.setTotalRows(totalRows);
- // 统计总页数
- int totalPage = totalRows % rowsPerPage == 0 ? totalRows / rowsPerPage :totalRows / rowsPerPage + 1;
- pageBean.setTotalPage(totalPage);
- // 统计最后一页的行数
- int lastPageRows = totalRows % rowsPerPage == 0 ? rowsPerPage :totalRows % rowsPerPage;
- pageBean.setLastPageRows(lastPageRows);
- try{
- this.stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
- rs=stmt.executeQuery(sql);
- int skipRows = (queryPageNo - 1) * rowsPerPage;
- for (int i = 0; i < skipRows; i++)
- rs.next();
- int count = rowsPerPage;
- while( rs!=null && rs.next() && count>0)
- {
- bossBean bos=new bossBean();
- bos.setR_number(rs.getString("r_number"));
- bos.setR_photo(rs.getString("r_photo"));
- bos.setR_area(rs.getString("r_area"));
- bos.setR_price(rs.getString("r_price"));
- bos.setR_facility(rs.getString("r_facility"));
- bos.setInsert_time(rs.getString("insert_time"));
- bos.setIf_book(rs.getString("if_book"));
- vc.add(bos);
- count--;
- }
- }
- catch(Exception e)
- {e.printStackTrace();}
- return vc;
- }
- }
-
- package servlet;
- import java.io.IOException;
- /**
- * 控制跳转的servlet
- *
- *@author micron E-mail: 328074337@qq.com
- *
- */
- import bean.*;
- import java.util.ArrayList;
- import java.util.Vector;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class select_list extends HttpServlet {
- private static final String CONTENT_TYPE = "text/html; charset=GBK";
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- }
- public void service(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {response.setContentType(CONTENT_TYPE);
- HttpSession session=request.getSession();
- String username=null;
- if(request.getParameter("send").equals("list_room")){
- username=(String)session.getAttribute("username");
- checkPage pageBean=new checkPage();
- int queryPageNo=1; // 请求的页号
- int rowsPerPage=1; // 每页的行数
- pageBean.setRowsPerPage(rowsPerPage);
- if(request.getParameter("action")!=null){
- if(request.getParameter("queryPageNo")!=null)
- {
- queryPageNo= Integer.parseInt(request.getParameter("queryPageNo"));
- }
- if(request.getParameter("action").equals("first")){
- queryPageNo=1;
- pageBean.setQueryPageNo(queryPageNo);
- }
- else if(request.getParameter("action").equals("back"))
- {
- queryPageNo -= 1;
- pageBean.setQueryPageNo(queryPageNo);
- }
- else if(request.getParameter("action").equals("next"))
- {
- queryPageNo += 1;
- pageBean.setQueryPageNo(queryPageNo);
- }
- else if(request.getParameter("action").equals("check"))
- { String check=request.getParameter("queryPageNo");
- queryPageNo=Integer.valueOf(check);
- pageBean.setQueryPageNo(queryPageNo);
- }
- else if(queryPageNo<=1){
- queryPageNo=1;
- pageBean.setQueryPageNo(queryPageNo);
- }
- else if(request.getParameter("action").equals("last"))
- { String no=request.getParameter("queryPageNo");
- queryPageNo=Integer.valueOf(no);
- System.out.print("queryPageNo"+queryPageNo);
- pageBean.setQueryPageNo(queryPageNo);
- }
- }
- Vector result=db.list_room(username,queryPageNo,pageBean,rowsPerPage);
- request.getSession().setAttribute("list_room",(Object)result);
- request.setAttribute("queryPageNo",queryPageNo);
- request.setAttribute("totalPage",pageBean.getTotalPage());
- int pagecount=pageBean.getTotalPage();//得到总页数
- request.setAttribute("pagecount",pagecount);
- int currentPage=pageBean.getQueryPageNo();//得到当前页
- request.setAttribute("currentPage",currentPage);
- request.getSession().setAttribute("boss_info",null);
- request.getSession().setAttribute("search",null);
- request.getSession().setAttribute("addroom",null);
- request.getSession().setAttribute("upload_success",null);
- request.getSession().setAttribute("nullresult",null);
- RequestDispatcher rd=request.getRequestDispatcher("trader_index.jsp");
- rd.forward(request,response);
- }
- }
- }
- }
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <%@ page contentType="text/html;charset=GBK"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <div align="center">
- <c:if test="${not empty sessionScope.list_room}">
- <c:forEach items="${sessionScope.list_room}" var="listRoom">
- <table width="80%" border="1" align="center" bordercolor="#FF6600">
- <tr>
- <td width="89" height="31">房间编号:</td>
- <td width="92">
- ${pageScope.listRoom.r_number}
- </td>
- <td width="82">预定信息:</td>
- <c:if test="${pageScope.listRoom.if_book ==null}">
- <td width="99" >
- <font color="#e70000">没有预定</font><img src="Image/sad.jpg" height="25"
- width="25"></img>
- </td>
- </c:if>
- <c:if test="${pageScope.listRoom.if_book !=null}">
- <td width="99" bgcolor="Yellow">
- <font color="#101010">已经预定</font><img src="Image/happy.jpg" height="25"
- width="25"></img>
- </td>
- </c:if>
- </tr>
- <tr>
- <td height="33">房间价格:</td>
- <td>
- ${pageScope.listRoom.r_price}
- </td>
- <td>房间面积:</td>
- <td>
- ${pageScope.listRoom.r_area}
- </td>
- </tr>
- <tr>
- <td height="36">发布时间:</td>
- <td colspan="3">
- ${pageScope.listRoom.insert_time}
- </td>
- </tr>
- <tr>
- <td height="56">房间描述:</td>
- <td colspan="3">
- ${pageScope.listRoom.r_facility}
- </td>
- </tr>
- <tr>
- <td height="113">图片:</td>
- <td colspan="3"><img src="${pageScope.listRoom.r_photo}" height="150"
- width="150"></img></td>
- </tr>
- </table>
- </c:forEach>
- <table width="80%">
- <tr>
- <td bgcolor="#E0FFFF" onmouseover="javascript:this.bgColor='#D3D3D3'" onmouseout="javascript:this.bgColor='#E0FFFF'">
- <a href="select_list?send=list_room&action=first&queryPageNo=${requestScope.queryPageNo}">首页</a>
- </td>
- <td bgcolor="#E0FFFF" onmouseover="javascript:this.bgColor='#D3D3D3'" onmouseout="javascript:this.bgColor='#E0FFFF'">
- <c:if test="${requestScope.currentPage>1}">
- <c:if test="${requestScope.queryPageNo==1}">上一页</c:if>
- <c:if test="${requestScope.queryPageNo!=1}">
- <a href="select_list?send=list_room&action=back&queryPageNo=${requestScope.queryPageNo}">上一页</a>
- </c:if>
- </c:if>
- </td>
- <c:forEach begin="1" end="${requestScope.totalPage}" varStatus="status">
- <td width="5%" bgcolor="#E0FFFF" onmouseover="javascript:this.bgColor='#D3D3D3'" onmouseout="javascript:this.bgColor='#E0FFFF'">
- <c:if test="${status.index == pageNo}">
- <span class="currentPage">${status.index}</span>
- </c:if>
- <c:if test="${status.index != pageNo}">
- <span><a href="select_list?send=list_room&action=check&queryPageNo=${status.index}" >${status.index}</a></span>
- </c:if>
- </td>
- </c:forEach>
- <td bgcolor="#E0FFFF" onmouseover="javascript:this.bgColor='#D3D3D3'" onmouseout="javascript:this.bgColor='#E0FFFF'">
- <c:if test="${requestScope.currentPage<requestScope.totalPage}">
- <c:if test="${requestScope.queryPageNo!=requestScope.pagecount}">
- <a href="select_list?send=list_room&action=next&queryPageNo=${requestScope.queryPageNo}">下一页</a>
- </c:if>
- </c:if>
- </td>
- <td bgcolor="#E0FFFF" onmouseover="javascript:this.bgColor='#D3D3D3'" onmouseout="javascript:this.bgColor='#E0FFFF'">
- <a href="select_list?send=list_room&action=last&queryPageNo=${requestScope.totalPage}">
- 尾页
- </a>
- </td>
- <td bgcolor="#E0FFFF">
- 共${requestScope.totalPage}页
- </td>
- <td>
- <select name="pageNo">
- <c:forEach begin="1" end="${requestScope.totalPage}" varStatus="status">
- <option value="${status.index}" ${status.index == pageNo ? 'selected' : ''}>
- ${status.index}
- </option>
- </c:forEach>
- </select>
- </td>
- </tr>
- </table>
- </c:if>
- </p>
- </div>
JSP+servlet+javabean实现分页
最新推荐文章于 2022-03-07 13:45:26 发布