首页 > 其他 > 详细

酒店管理系统

时间:2015-03-23 20:03:46      阅读:198      评论:0      收藏:0      [点我收藏+]

常量类:

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<objects.length;i++) {
					pst.setObject(i+1, objects[i]);
				}
			}
			
			// 执行SQL指令并处理返回结果
			row = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeObjects(pst,conn);
		}
		
		// 返回受影响行数
		return row;
	}
	
	/**
	 * 定义执行简单查询语句的通用方法
	 * @param sql 调用传入查询的SQL指令
	 * @param objects 查询所需参数
	 * @return List<Map<String,Object>> 返回查询构建集合对象
	 */
	public static List<Map<String,Object>> executeQuery(String sql,Object...objects) {
		// 定义表集合对象
		List<Map<String,Object>> table = new ArrayList<Map<String,Object>>();
		// 定义连接对象句柄
		Connection conn = null;
		// 定义编译对象句柄
		PreparedStatement pst = null;
		// 定义结果集句柄
		ResultSet rs = null;
		
		// 通过调用本类中的获取连接对象
		conn = getConn();
		
		try {
			// 通过连接对象创建预编译对象
			pst = conn.prepareStatement(sql);
			// 为查询编译对象设置参数
			if(objects != null) {
				for(int i=0;i<objects.length;i++) {
					pst.setObject(i+1, objects[i]);
				}
			}
			// 通过编译对象执行SQL命令
			rs = pst.executeQuery();
			
			// 判断结果是否为空
			if(rs != null) {
				// 把得到结果集转化为一张虚拟表
				ResultSetMetaData rsd = rs.getMetaData();
				// 得到查询表有多少个字段(列)
				int count = rsd.getColumnCount();
				
				// 得到当前遍历的行Map(key,value)
				while(rs.next()) {
					// 定义存储行的Map集合对象
					Map<String,Object> row = new HashMap<String,Object>();
					for(int i=0;i<count;i++) {
						row.put(rsd.getColumnName(i+1), rs.getObject(rsd.getColumnName(i+1)));
					}
					
					// 把每次遍历的行存储到表集合中
					table.add(row);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeObjects(rs,pst,conn);
		}
		
		return table;
	}
	
	/**
	 * 释放操作对象的方法
	 * @param objects 需要释放对象的列表
	 */
	private static void closeObjects(Object...objects) {
		if(objects != null) {
			for(Object param : objects) {
				try {
					if(param instanceof ResultSet) {
						((ResultSet)param).close();
					}
					
					if(param instanceof PreparedStatement) {
						((PreparedStatement)param).close();
					}
					
					if(param instanceof Connection) {
						Connection conn = ((Connection)param);
						if(!conn.isClosed()) {
							conn.close();
							conn = null;
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}

分页对象:

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 List<RoomInfo> getRoomInfos() {
		// 定义存储ROomInfo对象的集合
		List<RoomInfo> rooms = new ArrayList<RoomInfo>();
		// 定义查询所有房间信息的SQL命令
		String sql = "select * from roomInfo where 1=1";
		
		List<Map<String,Object>> lst = DBUtil.executeQuery(sql);
		if(lst!=null && lst.size()>0) {
			for(Map<String,Object> 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<RoomInfo> getRoomInfosByPaging(PageBean page,RoomInfo roomInfo) {
		// 定义存储ROomInfo对象的集合
		List<RoomInfo> rooms = new ArrayList<RoomInfo>();
		// 定义查询所有房间信息的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<Map<String,Object>> lst = DBUtil.executeQuery(sql,params);
		if(lst!=null && lst.size()>0) {
			for(Map<String,Object> 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<Map<String,Object>> 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<Map<String,Object>> lst = DBUtil.executeQuery(sql,room.getRoomID());
		if(lst!=null && lst.size()>0) {
			for(Map<String,Object> 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<Map<String,Object>> list = DBUtil.executeQuery(sql, room.getRoomeType());
		
		return list!=null && list.size()>0 ? list.get(0).get("typeName").toString() : null;
	}
	
	/**
	 * 定义查询所有房间类型信息的方法
	 */
	public List<RoomType> getTypes() {
		List<RoomType> roomTypes = new ArrayList<RoomType>();
		// 定义查询所有类型信息的SQL命令
		String sql = "select * from roomType";
		List<Map<String,Object>> list = DBUtil.executeQuery(sql);
		if(list != null && list.size()>0) {
			for(Map<String,Object> 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 {
		// 初始化房间类型列表
		List<RoomType> types = 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("<script>alert(‘添加失败‘);location.href=‘init.jsp‘;</script>");
		}
		
		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("<script>alert(‘删除失败‘);location.href=‘init.jsp‘;</script>");
		}
		
		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();
		//List<RoomInfo> 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<RoomType> 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<RoomInfo> 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<RoomType> 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);
		// 初始化房间类型列表
		List<RoomType> types = 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("<script>alert(‘修改失败‘);location.href=‘init.jsp‘;</script>");
		}
		
		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 List<RoomInfo> getRoomInfos() {
		// 创建存储带有类型名称的房间信息集合对象
		List<RoomInfo> rooms = new ArrayList<RoomInfo>();
		List<RoomInfo> 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<RoomType> 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<RoomInfo> roomInfos = roomDao.getRoomInfosByPaging(pageBean,roomInfo);
		// 创建存储带有类型名称的房间信息集合对象
		List<RoomInfo> rooms = new ArrayList<RoomInfo>();
		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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<fieldset style="width: 50%">
		<legend>房间信息添加</legend>
		<form action="addRoomInfoServlet?flag=doAdd" method="post">
			<table width="100%">
				<tr>
					<td>房间编号:</td>
					<td>
						<input type="text" name="roomId" />
					</td>
				</tr>
				<tr>
					<td>房间类型:</td>
					<td>
						<select name="roomType">
							<option value="-1">==请选择==</option>
							<%
								List<RoomType> types = (List<RoomType>)request.getAttribute("roomTypes");
								if(types!=null) {
									for(RoomType type : types) {
							%>
							<option value="<%=type.getTypeID() %>"><%=type.getTypeName() %></option>
							<% }
							}%>
						</select>
					</td>
				</tr>
				<tr>
					<td>房间位置:</td>
					<td><input name="roomPosition" /></td>
				</tr>
				<tr>
					<td>房间状态:</td>
					<td>
						<select name="roomState">
							<option value="-1">==请选择==</option>
							<option value="入住">入住</option>
							<option value="空闲">空闲</option>
						</select>
					</td>
				</tr>
				<tr>
					<td>房间描述</td>
					<td><textarea name="roomDesc" cols="50" rows="5"></textarea></td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="submit" style="width: 100px;" value="添加" />
					</td>
				</tr>
			</table>
		</form>
	</fieldset>
</body>
</html>

/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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>操作房间信息</title>
<style type="text/css">
	* {
		margin: 0px;
		padding: 0px;
		text-decoration: none;
	}
</style>
</head>
<body>
	<fieldset style="width: 50%;">
		<legend>房间信息检索</legend>
		<form action="queryRoomInfoServlet?currentPage=1" method="post">
			<div style="padding-bottom: 4px;">
				房间编号:<input type="text" style="width: 50px;" name="roomID" />
				房间类型:
				<select name="roomType">
					<option value="-1">==请选择==</option>
					<%
						List<RoomType> types = (List<RoomType>)request.getAttribute("roomTypes");
						if(types!=null) {
							for(RoomType type : types) {
					%>
					<option value="<%=type.getTypeID() %>"><%=type.getTypeName() %></option>
					<% }
						}%>
				</select>
				房间状态:
				<select name="roomState">
					<option value="-1">==请选择==</option>
					<option value="入住">入住</option>
					<option value="空闲">空闲</option>
				</select>
				<input type="submit" value="检索" />
				<input type="button" onclick="location.href=‘init.jsp‘" value="查询所有" />
			</div>
		</form>
	</fieldset>
	<fieldset style="width: 50%;text-align: center;">
		<legend>房间信息列表</legend>
		<table style="padding-bottom: 4px;" border="1" width="100%" cellpadding="0" cellspacing="0">
			<tr>
				<th>房间编号</th>
				<th>房间类型</th>
				<th>房间位置</th>
				<th>房间描述</th>
				<th>房间状态</th>
				<th>编辑</th>
			</tr>
			<%
				// 获取得到房间信息集合对象
				PageBean pageBean = (PageBean)request.getAttribute("pageBean");
				if(pageBean != null) {
					for(RoomInfo room : (List<RoomInfo>)pageBean.getList()) {
			%>
			<tr>
				<td><%=room.getRoomID() %></td>
				<td><%=room.getTypeName() %></td>
				<td><%=room.getRoomPositon() %></td>
				<td><%=room.getRoomDescrip() %></td>
				<td><%=room.getRoomStatus() %></td>
				<td>
					<a href="updateServlet?roomId=<%=room.getRoomID() %>&flag=init">修改</a>|
					<a href="deleteRoomInfoServelt?roomId=<%=room.getRoomID() %>">删除</a>
				</td>
			</tr>
			<%
					}
				}
			%>
			<tr>
				<td colspan="6"><a href="addRoomInfoServlet?flag=add">添加房间信息</a></td>
			</tr>
		</table>
	</fieldset>
	<div style="width: 50%;height: 17px;font-size:12px;text-align: right;">
		<a href="queryRoomInfoServlet?currentPage=1">首页</a>
		<a href="queryRoomInfoServlet?currentPage=<%=pageBean.getCurrentPage()-1<1? 1 : pageBean.getCurrentPage()-1 %>">上一页</a>
		<a href="queryRoomInfoServlet?currentPage=<%=pageBean.getCurrentPage()+1>pageBean.getTotalPages()?pageBean.getTotalPages():pageBean.getCurrentPage()+1 %>">下一页</a>
		<a href="queryRoomInfoServlet?currentPage=<%=pageBean.getTotalPages() %>">尾页</a>
		<span>
			总记录数:<%=pageBean.getTotalRecords() %>条
			当前第<%=pageBean.getCurrentPage() %>页
			总页数:<%=pageBean.getTotalPages() %>页
		</span>
	</div>
</body>
</html>

/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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
	// 获取修改的房间信息对象
	RoomInfo room = (RoomInfo)request.getAttribute("roomInfo");
	if(room !=null) {
%>
<body>
	<fieldset style="width: 50%">
		<legend>房间信息修改</legend>
		<form action="updateServlet?flag=update" method="post">
			<table width="100%">
				<tr>
					<td>房间编号:</td>
					<td>
						<%=room.getRoomID() %>
						<input type="hidden" value="<%=room.getRoomID() %>" name="roomId" />
					</td>
				</tr>
				<tr>
					<td>房间类型:</td>
					<td>
						<select name="roomType">
							<option value="-1">==请选择==</option>
							<%
								List<RoomType> types = (List<RoomType>)request.getAttribute("roomTypes");
								if(types!=null) {
									for(RoomType type : types) {
							%>
							<option <%=room.getRoomeType().equals(type.getTypeID())?"selected=‘selected‘":"" %>  value="<%=type.getTypeID() %>"><%=type.getTypeName() %></option>
							<% }
							}%>
						</select>
					</td>
				</tr>
				<tr>
					<td>房间位置:</td>
					<td><input name="roomPosition" value="<%=room.getRoomPositon() %>" /></td>
				</tr>
				<tr>
					<td>房间状态:</td>
					<td>
						<select name="roomState">
							<option value="-1">==请选择==</option>
							<option <%=room.getRoomStatus().equals("入住")?"selected=‘selected‘":"" %> value="入住">入住</option>
							<option <%=room.getRoomStatus().equals("空闲")?"selected=‘selected‘":"" %> value="空闲">空闲</option>
						</select>
					</td>
				</tr>
				<tr>
					<td>房间描述</td>
					<td><textarea name="roomDesc" cols="50" rows="5"><%=room.getRoomDescrip() %></textarea></td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="submit" style="width: 100px;" value="修改" />
					</td>
				</tr>
			</table>
		</form>
	</fieldset>
</body>
<% } %>
</html>


酒店管理系统

原文:http://lhmjava.blog.51cto.com/9668287/1623308

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!