常量类:
public interface Constants { /*************定义连接SQLServer2008字符串常量******************/ String DRIVER_NAME = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String URL = "jdbc:sqlserver://localhost:1433;databasename=HOTELDB"; String USERNAME = "sa"; String PASSWORD = "123456"; /**********定义业务类型常量***********/ /**********定义Servlet中对象key值常量********/ String ROOMS = "rooms"; String PAGE_BEAN = "pageBean"; String ROOMTYPES = "roomTypes"; String ROOMINFO = "roomInfo";}
util工具
/** * */package com.hotel.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.hotel.constant.Constants;/** * 数据库连接通用类 * 连接数据库的步骤: * 1. 导入驱动包 * 2. 加载驱动 * 3. 通过驱动管理器类获取数据库连接 * 4. 通过连接对象创建预编译对象 * 5. 通过编译对象执行SQL指令并处理返回结果 * 6. 关闭相关操作对象 * */public class DBUtil { /** * 定义获取连接对象的方法 * */ private static Connection getConn() { // 定义连接对象句柄 Connection conn = null; try { // 加载驱动 Class.forName(Constants.DRIVER_NAME); // 通过驱动管理器类获取数据库连接 conn = DriverManager.getConnection(Constants.URL, Constants.USERNAME, Constants.PASSWORD); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 定义执行简单SQL的增,删,改指令 * @param sql 调用传入的SQL指令 * @param objects 执行SQL指令需要的参数 * @return int 返回方法操作后受影响的行数 */ public static int executeMyUpdate(String sql,Object... objects) { // 定义接受受影响行数的变量 int row = 0; // 定义连接对象句柄 Connection conn = null; // 定义编译对象句柄 PreparedStatement pst = null; // 通过调用本类中的获取连接对象 conn = getConn(); try { // 通过连接对象创建编译对象 pst = conn.prepareStatement(sql); // 设置SQL命令所需的参数 if(objects != null) { for(int i =0 ;i> 返回查询构建集合对象 */ public static List
分页对象:
package com.hotel.entity;import java.util.List;public class PageBean { private int pageSize; // 每页显示数 private int totalRecords; // 总记录数 private int totalPages; // 总页数 private int currentPage;// 当前页数 private List list; // 存储当前页中所有的记录数 /** * @return the pageSize */ public int getPageSize() { return pageSize; } /** * @param pageSize the pageSize to set */ public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * @return the totalRecords */ public int getTotalRecords() { return totalRecords; } /** * @param totalRecords the totalRecords to set */ public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } /** * @return the totalPages */ public int getTotalPages() { return totalPages; } /** * @param totalPages the totalPages to set */ public void setTotalPages(int totalPages) { this.totalPages = totalPages; } /** * @return the currentPage */ public int getCurrentPage() { return currentPage; } /** * @param currentPage the currentPage to set */ public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } /** * @return the list */ public List getList() { return list; } /** * @param list the list to set */ public void setList(List list) { this.list = list; }}
房间信息实体类:
package com.hotel.entity;/** * 房间信息实体类 * @author Administrator * */public class RoomInfo { private String roomID=""; private String roomeType=""; private String roomPositon; private String roomDescrip; private String roomStatus=""; private String typeName; /** * @return the typeName */ public String getTypeName() { return typeName; } /** * @param typeName the typeName to set */ public void setTypeName(String typeName) { this.typeName = typeName; } /** * @return the roomID */ public String getRoomID() { return roomID; } /** * @param roomID the roomID to set */ public void setRoomID(String roomID) { this.roomID = roomID; } /** * @return the roomeType */ public String getRoomeType() { return roomeType; } /** * @param roomeType the roomeType to set */ public void setRoomeType(String roomeType) { this.roomeType = roomeType; } /** * @return the roomPositon */ public String getRoomPositon() { return roomPositon; } /** * @param roomPositon the roomPositon to set */ public void setRoomPositon(String roomPositon) { this.roomPositon = roomPositon; } /** * @return the roomDescrip */ public String getRoomDescrip() { return roomDescrip; } /** * @param roomDescrip the roomDescrip to set */ public void setRoomDescrip(String roomDescrip) { this.roomDescrip = roomDescrip; } /** * @return the roomStatus */ public String getRoomStatus() { return roomStatus; } /** * @param roomStatus the roomStatus to set */ public void setRoomStatus(String roomStatus) { this.roomStatus = roomStatus; }}
房间类型实体:
/** * 房间类型实体类 * @author Administrator * */public class RoomType { private String typeID; private String typeName; private String area; private String bedNum; private String price; private String airCondition; /** * @return the typeID */ public String getTypeID() { return typeID; } /** * @param typeID the typeID to set */ public void setTypeID(String typeID) { this.typeID = typeID; } /** * @return the typeName */ public String getTypeName() { return typeName; } /** * @param typeName the typeName to set */ public void setTypeName(String typeName) { this.typeName = typeName; } /** * @return the area */ public String getArea() { return area; } /** * @param area the area to set */ public void setArea(String area) { this.area = area; } /** * @return the bedNum */ public String getBedNum() { return bedNum; } /** * @param bedNum the bedNum to set */ public void setBedNum(String bedNum) { this.bedNum = bedNum; } /** * @return the price */ public String getPrice() { return price; } /** * @param price the price to set */ public void setPrice(String price) { this.price = price; } /** * @return the airCondition */ public String getAirCondition() { return airCondition; } /** * @param airCondition the airCondition to set */ public void setAirCondition(String airCondition) { this.airCondition = airCondition; }}
com.hotel.dao.RoomInfoDao:
package com.hotel.dao;import java.util.ArrayList;import java.util.List;import java.util.Map;import com.hotel.entity.PageBean;import com.hotel.entity.RoomInfo;import com.hotel.util.DBUtil;public class RoomInfoDao { /** * 查询所有的房间信息 * @return 返回查询的房间信息 */ public ListgetRoomInfos() { // 定义存储ROomInfo对象的集合 List rooms = new ArrayList (); // 定义查询所有房间信息的SQL命令 String sql = "select * from roomInfo where 1=1"; List > lst = DBUtil.executeQuery(sql); if(lst!=null && lst.size()>0) { for(Map map : lst) { // 创建一个房间对象 RoomInfo room = new RoomInfo(); room.setRoomID(map.get("RoomID").toString()); room.setRoomDescrip(map.get("RoomDescrip").toString()); room.setRoomeType(map.get("RoomeType").toString()); room.setRoomPositon(map.get("RoomPositon").toString()); room.setRoomStatus(map.get("RoomStatus").toString()); // 把房间对象添加到房间集合对象中 rooms.add(room); } } return rooms; } /** * 定义分页的查询方法 * @param page 接收分页对象封装的条件 * @return */ public List getRoomInfosByPaging(PageBean page,RoomInfo roomInfo) { // 定义存储ROomInfo对象的集合 List rooms = new ArrayList (); // 定义查询所有房间信息的SQL命令 String sql = "select top (?) * from (select row_number() " + "over(order by roomID) rowId,* from roomInfo where 1=1"; // 构建查询SQL命令 if(roomInfo != null) { if(!"".equals(roomInfo.getRoomID())) { sql+=" and roomID like '%"+roomInfo.getRoomID()+"%'"; } if(!"".equals(roomInfo.getRoomeType())) { sql+=" and roomeType='"+roomInfo.getRoomeType()+"'"; } if(!"".equals(roomInfo.getRoomStatus())) { sql+=" and roomStatus='"+roomInfo.getRoomStatus()+"'"; } } sql+=") room where rowId>?*(?-1)"; // 构建一个参数列表数组 Object[] params = { page.getPageSize(), page.getPageSize(), page.getCurrentPage() }; List > lst = DBUtil.executeQuery(sql,params); if(lst!=null && lst.size()>0) { for(Map map : lst) { // 创建一个房间对象 RoomInfo room = new RoomInfo(); room.setRoomID(map.get("RoomID").toString()); room.setRoomDescrip(map.get("RoomDescrip").toString()); room.setRoomeType(map.get("RoomeType").toString()); room.setRoomPositon(map.get("RoomPositon").toString()); room.setRoomStatus(map.get("RoomStatus").toString()); // 把房间对象添加到房间集合对象中 rooms.add(room); } } return rooms; } /** * 获取总的记录数 */ public int getTotalRecords(RoomInfo roomInfo) { // 定义查询总记录数的SQL命令 String sql = "select count(*) as totalRecords from roomInfo where 1=1"; // 构建查询SQL命令 if(roomInfo != null) { if(!"".equals(roomInfo.getRoomID())) { sql+=" and roomID like '%"+roomInfo.getRoomID()+"%'"; } if(!"".equals(roomInfo.getRoomeType())) { sql+=" and roomeType='"+roomInfo.getRoomeType()+"'"; } if(!"".equals(roomInfo.getRoomStatus())) { sql+=" and roomStatus='"+roomInfo.getRoomStatus()+"'"; } } List > list = DBUtil.executeQuery(sql); return list!=null && list.size()>0 ? Integer.parseInt( list.get(0).get("totalRecords").toString() ):0; } /** * 根据房间编号删除房间信息 */ public int deleteRoomInfoById(RoomInfo room) { int row = 0; // 定义删除的SQL命令 String sql = "delete from roomInfo where roomID=?"; row = DBUtil.executeMyUpdate(sql, room.getRoomID()); return row; } /** * 根据房间编号获取房间信息的方法 */ public RoomInfo getRoomById(RoomInfo room) { RoomInfo roomInfo = null; // 定义查询的SQL语句 String sql = "select * from roomInfo where roomID=?"; List > lst = DBUtil.executeQuery(sql,room.getRoomID()); if(lst!=null && lst.size()>0) { for(Map map : lst) { // 创建一个房间对象 roomInfo = new RoomInfo(); roomInfo.setRoomID(map.get("RoomID").toString()); roomInfo.setRoomDescrip(map.get("RoomDescrip").toString()); roomInfo.setRoomeType(map.get("RoomeType").toString()); roomInfo.setRoomPositon(map.get("RoomPositon").toString()); roomInfo.setRoomStatus(map.get("RoomStatus").toString()); } } return roomInfo; } /** * 修改房间信息 * @param room 已经更改的房间信息对象 * @return 返回是否修改成功 */ public int upadateRoomInfo(RoomInfo room) { int row = 0; // 定义修改的sql命令 String sql = "update roomInfo set " + "roomeType=?,RoomDescrip=?," + "RoomPositon=?,RoomStatus=? " + "where RoomID=?"; // 构建参数列表 Object[] params = { room.getRoomeType(), room.getRoomDescrip(), room.getRoomPositon(), room.getRoomStatus(), room.getRoomID() }; // 调用通用数据访问层的操作方法 row = DBUtil.executeMyUpdate(sql, params); return row; } /** * 添加房间信息的方法 * @param room * @return */ public int addRoomInfo(RoomInfo room) { int row = 0; // 定义添加的sql命令 String sql = "insert into RoomInfo values(?,?,?,?,?)"; // 构建参数列表 Object[] params = { room.getRoomID(), room.getRoomeType(), room.getRoomPositon(), room.getRoomDescrip(), room.getRoomStatus() }; // 调用通用数据访问层的操作方法 row = DBUtil.executeMyUpdate(sql, params); return row; }}
com.hotel.dao.RoomTypeDao:
package com.hotel.dao;import java.util.ArrayList;import java.util.List;import java.util.Map;import com.hotel.entity.RoomInfo;import com.hotel.entity.RoomType;import com.hotel.util.DBUtil;public class RoomTypeDao { /** * 通过类型编号获取类型名称 * @return */ public String getTypeNameById(RoomInfo room) { // 定义查询SQL命令 String sql = "select typeName from roomType where typeID=?"; List> list = DBUtil.executeQuery(sql, room.getRoomeType()); return list!=null && list.size()>0 ? list.get(0).get("typeName").toString() : null; } /** * 定义查询所有房间类型信息的方法 */ public List getTypes() { List roomTypes = new ArrayList (); // 定义查询所有类型信息的SQL命令 String sql = "select * from roomType"; List > list = DBUtil.executeQuery(sql); if(list != null && list.size()>0) { for(Map map : list) { RoomType type = new RoomType(); type.setTypeID(map.get("TypeID").toString()); type.setTypeName(map.get("TypeName").toString()); roomTypes.add(type); } } return roomTypes; }}
com.hotel.servlet.AddRoomInfoServlet:
package com.hotel.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.hotel.constant.Constants;import com.hotel.entity.RoomInfo;import com.hotel.entity.RoomType;import com.hotel.service.RoomInfoService;/** * Servlet implementation class AddRoomInfoServlet */public class AddRoomInfoServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public AddRoomInfoServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码格式 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String flag = request.getParameter("flag"); if("add".equals(flag)) { init(request,response); } else if("doAdd".equals(flag)) { add(request,response); } } //初始化添加页面的方法 private void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 初始化房间类型列表 Listtypes = new RoomInfoService().getTypes(); request.setAttribute(Constants.ROOMTYPES, types); request.getRequestDispatcher("pages/roomInfo/addRoomInfo.jsp").forward(request, response); } // 执行修改方法 private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); // 获取要修改的数据 String roomId = request.getParameter("roomId"); String roomType = request.getParameter("roomType"); String roomPosition = request.getParameter("roomPosition"); String roomState = request.getParameter("roomState"); String roomDesc = request.getParameter("roomDesc"); // 把获取的数据封装到RoomInfo对象中 RoomInfo room = new RoomInfo(); room.setRoomDescrip(roomDesc); room.setRoomeType(roomType); room.setRoomID(roomId); room.setRoomPositon(roomPosition); room.setRoomStatus(roomState); // 调用修改房间信息的业务方法 int row = new RoomInfoService().addRoomInfo(room); if(row>0) { response.sendRedirect("init.jsp"); } else { out.print(" "); } out.flush(); out.close(); }}
com.hotel.servlet.DeleteRoomInfoServelt:
package com.hotel.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.hotel.entity.RoomInfo;import com.hotel.service.RoomInfoService;/** * Servlet implementation class DeleteRoomInfoServelt */public class DeleteRoomInfoServelt extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DeleteRoomInfoServelt() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); // 获取要删除的编号 String roomId = request.getParameter("roomId"); RoomInfo room = new RoomInfo(); room.setRoomID(roomId); // 调用删除的业务方法 int row = new RoomInfoService().deleteRoomInfo(room); if(row>0) { response.sendRedirect("init.jsp"); } else { out.print(""); } out.flush(); out.close(); }}
com.hotel.servlet.QueryRoomInfoServlet:
package com.hotel.servlet;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.hotel.constant.Constants;import com.hotel.entity.PageBean;import com.hotel.entity.RoomInfo;import com.hotel.entity.RoomType;import com.hotel.service.RoomInfoService;/** * Servlet implementation class QueryRoomInfoServlet */public class QueryRoomInfoServlet extends HttpServlet { private static final long serialVersionUID = 1L; // 根据是否存储检索创建RoomInfo对象 public static RoomInfo room = null; /** * @see HttpServlet#HttpServlet() */ public QueryRoomInfoServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编格式 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); // 获取当前页 String currentPage = request.getParameter("currentPage"); // 调用查询房间信息的业务方法,创建业务对象的实例 RoomInfoService service = new RoomInfoService(); //Listrooms = service.getRoomInfos(); //request.setAttribute(Constants.ROOMS, rooms); // 获取房间编号,房间类型,房间状态 String roomId = request.getParameter("roomID")==null?"":request.getParameter("roomID"); String roomType = request.getParameter("roomType")==null?"":request.getParameter("roomType"); String roomState = request.getParameter("roomState")==null?"":request.getParameter("roomState"); // 判断是否存储分页条件 if(!"".equals(roomId)|| (!"-1".equals(roomType) && !"".equals(roomType)) || (!"-1".equals(roomState) && !"".equals(roomState))) { room = new RoomInfo(); if(!"".equals(roomId)) { room.setRoomID(roomId); } if(!"-1".equals(roomType)) { room.setRoomeType(roomType); } if(!"-1".equals(roomState)) { room.setRoomStatus(roomState); } } // 创建分页对象 PageBean page = new PageBean(); page.setPageSize(10); page.setCurrentPage(Integer.parseInt(currentPage)); PageBean pageBean = service.getRoomInfosByPaging(page,room); List types = service.getTypes(); request.setAttribute(Constants.PAGE_BEAN, pageBean); request.setAttribute(Constants.ROOMTYPES, types); request.getRequestDispatcher("pages/roomInfo/roomTypManager.jsp?currentPage=1").forward(request, response); }}package com.hotel.servlet;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.hotel.constant.Constants;import com.hotel.entity.PageBean;import com.hotel.entity.RoomInfo;import com.hotel.entity.RoomType;import com.hotel.service.RoomInfoService;/** * Servlet implementation class QueryRoomInfoServlet */public class QueryRoomInfoServlet extends HttpServlet { private static final long serialVersionUID = 1L; // 根据是否存储检索创建RoomInfo对象 public static RoomInfo room = null; /** * @see HttpServlet#HttpServlet() */ public QueryRoomInfoServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编格式 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); // 获取当前页 String currentPage = request.getParameter("currentPage"); // 调用查询房间信息的业务方法,创建业务对象的实例 RoomInfoService service = new RoomInfoService(); //List rooms = service.getRoomInfos(); //request.setAttribute(Constants.ROOMS, rooms); // 获取房间编号,房间类型,房间状态 String roomId = request.getParameter("roomID")==null?"":request.getParameter("roomID"); String roomType = request.getParameter("roomType")==null?"":request.getParameter("roomType"); String roomState = request.getParameter("roomState")==null?"":request.getParameter("roomState"); // 判断是否存储分页条件 if(!"".equals(roomId)|| (!"-1".equals(roomType) && !"".equals(roomType)) || (!"-1".equals(roomState) && !"".equals(roomState))) { room = new RoomInfo(); if(!"".equals(roomId)) { room.setRoomID(roomId); } if(!"-1".equals(roomType)) { room.setRoomeType(roomType); } if(!"-1".equals(roomState)) { room.setRoomStatus(roomState); } } // 创建分页对象 PageBean page = new PageBean(); page.setPageSize(10); page.setCurrentPage(Integer.parseInt(currentPage)); PageBean pageBean = service.getRoomInfosByPaging(page,room); List types = service.getTypes(); request.setAttribute(Constants.PAGE_BEAN, pageBean); request.setAttribute(Constants.ROOMTYPES, types); request.getRequestDispatcher("pages/roomInfo/roomTypManager.jsp?currentPage=1").forward(request, response); }}
com.hotel.servlet.UpdateServlet:
package com.hotel.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.hotel.constant.Constants;import com.hotel.entity.RoomInfo;import com.hotel.entity.RoomType;import com.hotel.service.RoomInfoService;/** * Servlet implementation class UpdateServlet */public class UpdateServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UpdateServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置参数 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String flag = request.getParameter("flag"); if("init".equals(flag)) { init(request,response); } else if("update".equals(flag)) { update(request, response); } } //初始化修改页面的方法 private void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取房间编号 String roomId = request.getParameter("roomId"); RoomInfo room = new RoomInfo(); room.setRoomID(roomId); // 调用根据房间编号查询房间信息的业务方法 RoomInfo roomInfo = new RoomInfoService().getRoomById(room); request.setAttribute(Constants.ROOMINFO, roomInfo); // 初始化房间类型列表 Listtypes = new RoomInfoService().getTypes(); request.setAttribute(Constants.ROOMTYPES, types); request.getRequestDispatcher("pages/roomInfo/updateRoomInfo.jsp").forward(request, response); } // 执行修改方法 private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); // 获取要修改的数据 String roomId = request.getParameter("roomId"); String roomType = request.getParameter("roomType"); String roomPosition = request.getParameter("roomPosition"); String roomState = request.getParameter("roomState"); String roomDesc = request.getParameter("roomDesc"); // 把获取的数据封装到RoomInfo对象中 RoomInfo room = new RoomInfo(); room.setRoomDescrip(roomDesc); room.setRoomeType(roomType); room.setRoomID(roomId); room.setRoomPositon(roomPosition); room.setRoomStatus(roomState); // 调用修改房间信息的业务方法 int row = new RoomInfoService().updateRoomInfo(room); if(row>0) { response.sendRedirect("init.jsp"); } else { out.print(" "); } out.flush(); out.close(); } }
com.hotel.service.RoomInfoService:
package com.hotel.service;import java.util.ArrayList;import java.util.List;import com.hotel.dao.RoomInfoDao;import com.hotel.dao.RoomTypeDao;import com.hotel.entity.PageBean;import com.hotel.entity.RoomInfo;import com.hotel.entity.RoomType;public class RoomInfoService { // 创建RoomInfoDao对象实例 private RoomInfoDao roomDao = new RoomInfoDao(); // 创建RoomTypeDao对象实例 private RoomTypeDao typeDao = new RoomTypeDao(); /** * 查询所有房间信息的业务方法 */ public ListgetRoomInfos() { // 创建存储带有类型名称的房间信息集合对象 List rooms = new ArrayList (); List roomInfos = roomDao.getRoomInfos(); System.out.println(roomInfos); for(RoomInfo room : roomInfos) { String typeName = typeDao.getTypeNameById(room); room.setTypeName(typeName); // 把处理好的房间信息对象填充到rooms集合中 rooms.add(room); } return rooms; } /** * 获取所有类型信息的业务方法 * @return */ public List getTypes() { return typeDao.getTypes(); } /** * 分页查询房间信息的方法 * @param page * @return */ public PageBean getRoomInfosByPaging(PageBean page,RoomInfo roomInfo) { // 设置分页对象的总记录数 int totalRecords = roomDao.getTotalRecords(roomInfo); // 计算中页数[如果不能被整除总页数加1] int totalPages = totalRecords%page.getPageSize()==0 ? totalRecords/page.getPageSize(): totalRecords/page.getPageSize()+1; int currentPage = page.getCurrentPage(); // 创建分页对象实例 PageBean pageBean = new PageBean(); pageBean.setPageSize(page.getPageSize()); pageBean.setCurrentPage(currentPage); pageBean.setTotalPages(totalPages); pageBean.setTotalRecords(totalRecords); // 获取当前页分页集合[把集合中房间类型编号转化为类型名称] List roomInfos = roomDao.getRoomInfosByPaging(pageBean,roomInfo); // 创建存储带有类型名称的房间信息集合对象 List rooms = new ArrayList (); for(RoomInfo room : roomInfos) { String typeName = typeDao.getTypeNameById(room); room.setTypeName(typeName); rooms.add(room); } pageBean.setList(rooms); return pageBean; } /** * 删除房间信息的业务方法 * @param room * @return */ public int deleteRoomInfo(RoomInfo room) { return roomDao.deleteRoomInfoById(room); } /** * 根据房间信息编号获取房间信息的业务方法 * @param room 房间对象 * @return 返回房间信息对象 */ public RoomInfo getRoomById(RoomInfo room) { return roomDao.getRoomById(room); } /** * 修改房间信息的业务方法 * @param room * @return */ public int updateRoomInfo(RoomInfo room) { return roomDao.upadateRoomInfo(room); } /** * 添加房间信息的业务方法 * @param room * @return */ public int addRoomInfo(RoomInfo room) { return roomDao.addRoomInfo(room); }}
/HOTELMANAGER/WebContent/init.jsp(初始化界面)
<%@page import="com.hotel.servlet.QueryRoomInfoServlet"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><% QueryRoomInfoServlet.room = null; response.sendRedirect("queryRoomInfoServlet?currentPage=1");%>
/HOTELMANAGER/WebContent/pages/roomInfo/addRoomInfo.jsp:
<%@page import="com.hotel.entity.RoomType"%><%@page import="java.util.List"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here
/HOTELMANAGER/WebContent/pages/roomInfo/roomTypManager.jsp:
<%@page import="com.hotel.entity.RoomType"%><%@page import="com.hotel.entity.PageBean"%><%@page import="com.hotel.entity.RoomInfo"%><%@page import="java.util.List"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>操作房间信息
/HOTELMANAGER/WebContent/pages/roomInfo/updateRoomInfo.jsp:
<%@page import="com.hotel.entity.RoomInfo"%><%@page import="com.hotel.entity.RoomType"%><%@page import="java.util.List"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here <% // 获取修改的房间信息对象 RoomInfo room = (RoomInfo)request.getAttribute("roomInfo"); if(room !=null) {%>