diff --git a/jweb/src/com/HotelDAO/HotelDAO.java b/jweb/src/com/HotelDAO/HotelDAO.java new file mode 100644 index 00000000..ce85301c --- /dev/null +++ b/jweb/src/com/HotelDAO/HotelDAO.java @@ -0,0 +1,8 @@ +package com.HotelDAO; +import javaBean.Customer; +public interface HotelDAO { + public abstract int create(Customer cus)throws Exception; + public abstract void remove(Customer cus)throws Exception; + public abstract Customer find(Customer cus)throws Exception; + +} diff --git a/jweb/src/com/HotelDAO/HotelsDAO.java b/jweb/src/com/HotelDAO/HotelsDAO.java new file mode 100644 index 00000000..c57ac912 --- /dev/null +++ b/jweb/src/com/HotelDAO/HotelsDAO.java @@ -0,0 +1,74 @@ +package com.HotelDAO; +import java.sql.*; +import javaBean.db.*; +import javaBean.Customer; +public class HotelsDAO implements HotelDAO{ + protected static final String FIELDS_INSERT="id,name,room,bill"; + protected static String INSERT_SQL="insert into cus_info(" + +FIELDS_INSERT+")"+"values(?,?,?,?)"; + protected static String DELETE_SQL="delete from cus_info where id=?"; + protected static String SELECT_SQL="select" + +FIELDS_INSERT+"frome cus_info where id=?"; +//实现向数据库中添加记录 + public int create(Customer cus)throws Exception{ + Connection con=null; + PreparedStatement prepStmt=null; + ResultSet rs=null; + int n=0; + try{ + con=DbConnect.getDBconnection(); + prepStmt=con.prepareStatement(INSERT_SQL); + prepStmt.setDouble(1,cus.getId()); + prepStmt.setString(2, cus.getName()); + prepStmt.setInt(3,cus.getRoom()); + prepStmt.setFloat(4, 0); + n=prepStmt.executeUpdate(); + System.out.print(cus.getName()); + }catch(Exception e){ + DbConnect.closeDB(con,prepStmt,rs); + } + return n; + } + +//实现查询数据库中对指定的记录是否存在 + public Customer find(Customer cus)throws Exception{ + Connection con=null; + PreparedStatement prepStmt=null; + ResultSet rs=null; + Customer cus2=null; + try{ + con=DbConnect.getDBconnection(); + prepStmt=con.prepareStatement(INSERT_SQL); + prepStmt.setDouble(1, cus.getId()); + rs=prepStmt.executeQuery(); + if(rs.next()){ + cus2=new Customer(); + cus2.setId(rs.getInt(1)); + cus2.setName(rs.getString(2)); + cus2.setRoom(rs.getInt(3)); + cus2.setBill(rs.getFloat(4)); + } + }catch(Exception e){ + + }finally{ + DbConnect.closeDB(con, prepStmt, rs); + } + return cus2; + } + + public void remove(Customer cus)throws Exception{ + Connection con=null; + PreparedStatement prepStmt=null; + ResultSet rs=null; + try{ + con=DbConnect.getDBconnection(); + prepStmt=con.prepareStatement(DELETE_SQL); + prepStmt.setDouble(1,cus.getId()); + prepStmt.executeUpdate(); + }catch(Exception e){ + + }finally{ + DbConnect.closeDB(con,prepStmt,rs); + } + } +} \ No newline at end of file diff --git a/jweb/src/javaBean/Customer.java b/jweb/src/javaBean/Customer.java new file mode 100644 index 00000000..591cb872 --- /dev/null +++ b/jweb/src/javaBean/Customer.java @@ -0,0 +1,33 @@ +package javaBean; + +public class Customer { + private double id; + private String name; + private int room; + private float bill; + public double getId() { + return id; + } + public void setId(double id) { + this.id = id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public int getRoom() { + return room; + } + public void setRoom(int room) { + this.room = room; + } + public float getBill() { + return bill; + } + public void setBill(float bill) { + this.bill = bill; + } + +} diff --git a/jweb/src/javaBean/Room.java b/jweb/src/javaBean/Room.java new file mode 100644 index 00000000..1992a20b --- /dev/null +++ b/jweb/src/javaBean/Room.java @@ -0,0 +1,26 @@ +package javaBean; + +public class Room { + private int number; + private String type; + private String zhuangtai; + public int getNumber() { + return number; + } + public void setNumber(int number) { + this.number = number; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public String getZhuangtai() { + return zhuangtai; + } + public void setZhuangtai(String zhuangtai) { + this.zhuangtai = zhuangtai; + } + +} diff --git a/jweb/src/javaBean/db/DbConnect.java b/jweb/src/javaBean/db/DbConnect.java new file mode 100644 index 00000000..81db41aa --- /dev/null +++ b/jweb/src/javaBean/db/DbConnect.java @@ -0,0 +1,32 @@ +package javaBean.db; +import java.sql.*; +public class DbConnect { + private static String driverName="com.mysql.jdbc.Driver"; + private static String userName="root"; + private static String userPwd="root"; + private static String dbName="customer"; + public static Connection getDBconnection(){ + String url1="jdbc:mysql://localhost/"+dbName; + String url2="?user="+userName+"&password="+userPwd; + String url3="&useUnicode=true&characterEncoding=GB2312&useSSL=false"; + String url=url1+url2+url3; + try{ + Class.forName(driverName); + Connection con=DriverManager.getConnection(url); + return con; + }catch(Exception e){ + e.printStackTrace(); + } + return null; + + } + public static void closeDB(Connection con,PreparedStatement pstm,ResultSet rs){ + try{ + if(rs!=null)rs.close(); + if(pstm!=null)pstm.close(); + if(con!=null)con.close(); + }catch(SQLException e){ + e.printStackTrace(); + } + } +} diff --git a/jweb/src/servlet/Insert.java b/jweb/src/servlet/Insert.java new file mode 100644 index 00000000..f83ff5c9 --- /dev/null +++ b/jweb/src/servlet/Insert.java @@ -0,0 +1,92 @@ +package servlet; + +import java.io.IOException; +import java.io.PrintWriter; + +import javaBean.Customer; +import com.HotelDAO.*; +import javaBean.db.*; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class Insert extends HttpServlet { + + /** + * Constructor of the object. + */ + public Insert() { + super(); + } + + /** + * Destruction of the servlet.
+ */ + public void destroy() { + super.destroy(); // Just puts "destroy" string in log + // Put your code here + } + + /** + * The doGet method of the servlet.
+ * + * This method is called when a form has its tag value method equals to get. + * + * @param request the request send by the client to the server + * @param response the response send by the server to the client + * @throws ServletException if an error occurred + * @throws IOException if an error occurred + */ + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + request.setCharacterEncoding("utf-8"); + double id=Double.parseDouble(request.getParameter("id")); + String name=request.getParameter("name"); + int room=Integer.parseInt(request.getParameter("room")); + Customer cus=new Customer(); + int n=0; + cus.setId(id); + cus.setName(name); + cus.setRoom(room); + HotelsDAO run=new HotelsDAO(); + try { + n=run.create(cus); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + if(n>=1) + request.getRequestDispatcher("success.jsp").forward(request, response); + else + request.getRequestDispatcher("error.jsp").forward(request, response); + } + + /** + * The doPost method of the servlet.
+ * + * This method is called when a form has its tag value method equals to post. + * + * @param request the request send by the client to the server + * @param response the response send by the server to the client + * @throws ServletException if an error occurred + * @throws IOException if an error occurred + */ + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + doGet(request,response); + } + + /** + * Initialization of the servlet.
+ * + * @throws ServletException if an error occurs + */ + public void init() throws ServletException { + // Put your code here + } + +} diff --git a/jweb/src/servlet/Se1414080902137Servlet.java b/jweb/src/servlet/Se1414080902137Servlet.java new file mode 100644 index 00000000..1865ba21 --- /dev/null +++ b/jweb/src/servlet/Se1414080902137Servlet.java @@ -0,0 +1,85 @@ +package servlet; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class Se1414080902137Servlet extends HttpServlet { + + + /** + * Constructor of the object. + */ + public Se1414080902137Servlet() { + super(); + } + + /** + * Destruction of the servlet.
+ */ + public void destroy() { + super.destroy(); // Just puts "destroy" string in log + // Put your code here + } + + /** + * The doGet method of the servlet.
+ * + * This method is called when a form has its tag value method equals to get. + * + * @param request the request send by the client to the server + * @param response the response send by the server to the client + * @throws ServletException if an error occurred + * @throws IOException if an error occurred + */ + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + doPost(request,response); + } + + /** + * The doPost method of the servlet.
+ * + * This method is called when a form has its tag value method equals to post. + * + * @param request the request send by the client to the server + * @param response the response send by the server to the client + * @throws ServletException if an error occurred + * @throws IOException if an error occurred + */ + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + response.setContentType("text/html"); + String userName=request.getParameter("username"); + String userPwd=request.getParameter("password"); + String info=""; + if(("abc").equals(userName)&&("123").equals(userPwd)){ + info="欢迎你,"+userName+"!"; + request.setAttribute("a", userName); + request.setAttribute("outputMessage", info); + request.getRequestDispatcher("index.jsp").forward(request,response); + }else{ + response.sendRedirect("index_title.jsp?error=yes"); + + } + + + } + + /** + * Initialization of the servlet.
+ * + * @throws ServletException if an error occurs + */ + public void init() throws ServletException { + // Put your code here + } + +} diff --git a/jweb/web/1414080902137/occupancy.jsp b/jweb/web/1414080902137/occupancy.jsp index 264353d1..4d1b7761 100644 --- a/jweb/web/1414080902137/occupancy.jsp +++ b/jweb/web/1414080902137/occupancy.jsp @@ -23,10 +23,10 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. -
- 韬唤璇侊細
+ + 韬唤璇侊細
濮撳悕锛
- 鎴块棿鍙凤細
+ 鎴块棿鍙凤細
diff --git a/jweb/web/1414080902137/tijiao.jsp b/jweb/web/1414080902137/tijiao.jsp index 6e124483..ae80d6de 100644 --- a/jweb/web/1414080902137/tijiao.jsp +++ b/jweb/web/1414080902137/tijiao.jsp @@ -24,8 +24,9 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. - - - +
纭淇℃伅
+ 瀹㈡埛韬唤璇侊細<%=request.getParameter("id") %> + 瀹㈡埛濮撳悕锛<%=request.getParameter("name") %> + 鎴块棿鍙凤細<%=request.getParameter("room") %>