原生态JAVAEE酒店管理系统系列二

有点晚了,放一部分代码上来


首先是domain层,先来两个class

Hotel .java

/**
 * 
 */
package edu.fjnu.hotelsys.domain;

/**
 * 
 * �ֵ���Ϣ
 * 
 * @author Harry
 */
public class Hotel extends ValueObject {
	
	private Integer hotelId;
	private String  hotelName;
	private String  hotelAddr;
	private String  hotelPhone;
	private Integer hotelRoomCount = 0;
	private byte[]  hotelPic;
		
	public byte[] getHotelPic() {
		return hotelPic;
	}
	public void setHotelPic(byte[] hotelPic) {
		this.hotelPic = hotelPic;
	}
	public Integer getHotelId() {
		return hotelId;
	}
	public void setHotelId(Integer hotelId) {
		this.hotelId = hotelId;
	}
	public String getHotelName() {
		return hotelName;
	}
	public void setHotelName(String hotelName) {
		this.hotelName = hotelName;
	}
	public String getHotelAddr() {
		return hotelAddr;
	}
	public void setHotelAddr(String hotelAddr) {
		this.hotelAddr = hotelAddr;
	}
	public String getHotelPhone() {
		return hotelPhone;
	}
	public void setHotelPhone(String hotelPhone) {
		this.hotelPhone = hotelPhone;
	}
	public Integer getHotelRoomCount() {
		return hotelRoomCount;
	}
	public void setHotelRoomCount(Integer hotelRoomCount) {
		this.hotelRoomCount = hotelRoomCount;
	}
	
	

}


User.java

/**
 * 
 */
package edu.fjnu.hotelsys.domain;

/**
 * @author Harry
 */
public class User extends ValueObject {
	
	private Integer userId;
	private String userName;
	private String userPwd;
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	
	
	
	
}



  这两个domain层有了,开始放一些dao层的代码上来

dao层我分成了接口和实现

放一个hotel的接口上来


/**
 * 
 */
package edu.fjnu.hotelsys.dao;

import java.util.List;

import edu.fjnu.hotelsys.domain.Hotel;

/**
 * @author Harry
 *
 */
public interface HotelDao {
	/**
	 * 添加酒店
	 * @param hotel
	 * @author Harry
	 */
	public void addHotel(Hotel hotel);
	
	/**
	 * 删除hotel对象
	 * @param hotelId
	 * @author Harry
	 */
	public void deleteHotel(Integer hotelId);
	
	/**
	 * 更新hotel信息
	 * @param hotel
	 * @author Harry
	 */
	public void updateHotel(Hotel hotel);
	
	/**
	 * 通过hotelId查找到Hotel对象
	 * @param hotelId
	 * @return hotel对象
	 * @author Harry
	 */
	public Hotel findHotelById(Integer hotelId);
	
	/**
	 * 显示出所有的Hotel
	 * @return 返回hotel的List集合或是返回空
	 * @author Harry
	 */
	public List<Hotel> loadAllHotel();
}


代码规范写注释已经成了习惯。。


在这边插入一个DBUtils,数据库连接小工具


package edu.fjnu.hotelsys.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtils {
	
	private static DBUtils me=new DBUtils();
	
	private DBUtils() {}
	
	public static DBUtils getInstance()
	{
		return me;
	}
	
	/**
	 * 获取数据库连接
	 * @author Harry
	 * @return
	 */
	public Connection getConn()
	{
		
		Connection conn=null;
		
		try {
			//查询类路径中是否存在这个驱动入口类
			Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
			conn=DriverManager.getConnection("proxool.hotelsys-ds");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		
		return conn;
		
	}
	
	/**
	 * 释放数据库资源
	 * @param conn
	 * @param pstmt
	 * @param rset
	 */
	public void ReleaseRes(Connection conn,PreparedStatement pstmt,ResultSet rset)
	{
		try{
		  if(rset!=null) rset.close();
		  if(pstmt!=null) pstmt.close();
		  if(conn!=null)  conn.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
	}
	
	
	

}

用这个工具做会方便点


放一个hotel的接口实现


/**
 * 
 */
package edu.fjnu.hotelsys.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.fjnu.hotelsys.dao.HotelDao;
import edu.fjnu.hotelsys.domain.Hotel;
import edu.fjnu.hotelsys.utils.DBUtils;

/**
 * @author Harry
 *
 */
public class HotelDaoImpl implements HotelDao {

	private static final String SQL_ADD="insert into hotel(hotel_name,hotel_addr,hotel_phone,hotel_room_count) values(?,?,?,?)";
	private static final String SQL_LOADALL="select * from hotel order by hotel_no desc";
	private static final String SQL_FINDONE="select * from hotel where hotel_no=?";
	private static final String SQL_UPDATE="update hotel set hotel_name=?,hotel_addr=?,hotel_phone=?,hotel_room_count=? where hotel_no=?";
	private static final String SQL_DEL = "delete from hotel where hotel_no=?";
	
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rset;
	/**
	 * 添加酒店
	 * @param hotel
	 * @author Harry
	 */
	public void addHotel(Hotel hotel) {
		
		conn=DBUtils.getInstance().getConn(); // 找连接池拿一个连接对象
		try{
		   pstmt=conn.prepareStatement(SQL_ADD);//动态sql语句,创建一个statement对象,用来发送sql参数给数据库
		   //Creates a PreparedStatement object for sending parameterized SQL statements to the database
		   pstmt.setString(1, hotel.getHotelName());//设置上述提到的问号,这是jdbc防sql注入的一个手段
		   pstmt.setString(2, hotel.getHotelAddr());
		   pstmt.setString(3, hotel.getHotelPhone());
		   pstmt.setInt(4, hotel.getHotelRoomCount());
		   pstmt.executeUpdate();//执行
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, null);//finally,一定执行,释放连接
		}
		

	}
	
	/**
	 * 删除hotel对象
	 * @param hotelId
	 * @author Harry
	 */
	public void deleteHotel(Integer hotelId) {
		
		conn=DBUtils.getInstance().getConn(); // 找连接池拿一个连接对象
		try{
		   pstmt=conn.prepareStatement(SQL_DEL);//动态sql语句,创建一个statement对象,用来发送sql参数给数据库
		   //Creates a PreparedStatement object for sending parameterized SQL statements to the database
		   pstmt.setInt(1,hotelId);
		   pstmt.executeUpdate();//执行
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, null);//finally,一定执行,释放连接
		}
		
	}
	
	/**
	 * 更新hotel信息
	 * @param hotel
	 * @author Harry
	 */
	public void updateHotel(Hotel hotel) {
		conn=DBUtils.getInstance().getConn(); // 找连接池拿一个连接对象
		try{
		   pstmt=conn.prepareStatement(SQL_UPDATE);//动态sql语句,创建一个statement对象,用来发送sql参数给数据库
		   //Creates a PreparedStatement object for sending parameterized SQL statements to the database
		   pstmt.setString(1, hotel.getHotelName());
		   pstmt.setString(2, hotel.getHotelAddr());
		   pstmt.setString(3, hotel.getHotelPhone());
		   pstmt.setInt(4, hotel.getHotelRoomCount());
		   pstmt.setInt(5, hotel.getHotelId());
		   pstmt.executeUpdate();//执行
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, null);//finally,一定执行,释放连接
		}
	}
	
	/**
	 * 通过hotelId查找到Hotel对象
	 * @param hotelId
	 * @return hotel对象
	 * @author Harry
	 */
	public Hotel findHotelById(Integer hotelId) {
		conn=DBUtils.getInstance().getConn(); // 找连接池拿一个连接对象
		Hotel hotel = null ; //创建一个hotel对象,用来返回
		try{
		   pstmt=conn.prepareStatement(SQL_FINDONE);//动态sql语句,创建一个statement对象,用来发送sql参数给数据库
		   //Creates a PreparedStatement object for sending parameterized SQL statements to the database
		   pstmt.setInt(1, hotelId);
		   rset = pstmt.executeQuery();//执行
			
		   if(rset.next()){
			   hotel = new Hotel();
			   hotel.setHotelId(rset.getInt("hotel_no"));
			   hotel.setHotelName(rset.getString("hotel_name"));
			   hotel.setHotelAddr(rset.getString("hotel_addr"));
			   hotel.setHotelPhone(rset.getString("hotel_phone"));
			   hotel.setHotelRoomCount(rset.getInt("hotel_room_count"));
			   
		   }
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);//finally,一定执行,释放连接
		}
		return hotel;
	}
	
	/**
	 * 显示出所有的Hotel
	 * @return 返回hotel的List集合或是返回空
	 * @author Harry
	 */
	public List<Hotel> loadAllHotel() {
		conn=DBUtils.getInstance().getConn(); // 找连接池拿一个连接对象
		List<Hotel> hotelList = new ArrayList<Hotel>();
		try{
		   pstmt=conn.prepareStatement(SQL_LOADALL);//动态sql语句,创建一个statement对象,用来发送sql参数给数据库
		   //Creates a PreparedStatement object for sending parameterized SQL statements to the database
		   rset = pstmt.executeQuery();
		   while(rset.next()){
			   Hotel hotel = new Hotel();
			   hotel.setHotelId(rset.getInt("hotel_no"));
			   hotel.setHotelName(rset.getString("hotel_name"));
			   hotel.setHotelAddr(rset.getString("hotel_addr"));
			   hotel.setHotelPhone(rset.getString("hotel_phone"));
			   hotel.setHotelRoomCount(rset.getInt("hotel_room_count"));
			   hotelList.add(hotel);
		   }
		  
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);//finally,一定执行,释放连接
		}
		return hotelList;
	}

}


放一个开设分店的jsp上来

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title></title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link rel="stylesheet" type="text/css" href="<c:url value="/css/application.css"/>">
    <link rel="stylesheet" type="text/css" href="<c:url value="/css/form.css"></c:url>">
  </head>
  
  <body>
    <div id="wrapper">
	    <div id="f_title">开设分店</div>
	    <form action="<c:url value="/HotelMgrServlet?act=create"/>" method="post">
	        <div class="f_row">
	          <span>分店名称:</span>
	          <input type="text" name="hotelname" size="20"/>
	        </div>
	        <div class="f_row">
	          <span>分店地址:</span>
	          <input type="text" name="hoteladdr" size="50"/>        
	        </div>
	        <div class="f_row">
	          <span>联络电话:</span>
	          <input type="text" name="hotelphone" size="20"/>          
	        </div> 
	        <div class="f_row">
	          <input type="submit" value="保存信息"/>      
	        </div>                        
	    </form>
    </div>
  </body>
</html>


这边用到了一个c标签,主要是为了方便解决路径问题


简单放一点

有兴趣的可以去看看,目前做了一点点


http://139.129.12.4:8080/HarryHotelSys/ 

账号aaa

密码aaa123

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值