房间预订检查

时间:2018-08-22 11:20:44

标签: java eclipse jsp

在我的项目中,我创建了一个预订房间的系统。 我的问题与在同一日期预订房间有关。 这是有关预订的数据库。

  

id_book,登录名,电子邮件,打字室,数字会议室,到达日期,离开目的地。

这是检查一段时间内是否有空房间的代码:

try {

            Class.forName("com.mysql.cj.jdbc.Driver");
      //  out.println("driver loaded");
            Connection  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Hotel?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root" ,"123456789");
            out.println("Connect");
            Statement  st =  con.createStatement();
            Statement stmt = con.createStatement();
            out.println("connection successfull");

            String check = ("SELECT res1.id_prenotazione, res1.typeroom, res1.arrivaldate, res1.departuredate\n" + 
                    "FROM reservation res1, reservation res2\n" + 
                    "WHERE ( res1.typeroom = res2.typeroom ) \n" + 
                    "AND (res1.arrivaldate <= res2.departuredate)\n" + 
                    "AND (res2.arrivaldate <= res1.departuredate)");
            String check1 = ("SELECT count(*) FROM reservation WHERE arrivaldate");

            ResultSet rs2  = stmt.executeQuery(check);
            ResultSet rs3 = stmt.executeQuery(check1);
            if( rs2 != rs3) {
            int rs =  st.executeUpdate("insert into reservation (login,email,typeroom,numroom,arrivaldate,departuredate)values ('"+login+"','"+email+"','"+typeroom+"','"+numroom+"','"+arrivaldate+"','"+departuredate+"')");
            }
            String getResultSet = ("SELECT count(*) FROM reservation WHERE arrivaldate ='"+arrivaldate+"'");
            String rs1 = ("SELECT count(*) FROM reservation WHERE arrivaldate");


            if (getResultSet != rs1) {
                int i=st.executeUpdate("DELETE FROM reservation WHERE id_prenotazione ='"+id_prenotazione+"'");

            }

问题在于,以这种方式我会不断记录相同日期的相同房间,我该如何解决?

1 个答案:

答案 0 :(得分:0)

我没有完全回答您的问题。我了解的部分是您显然不想在给定日期超额预订房间。
如果是这种情况,在显示可用房间时,您可以触发该日期的count(*)查询。那时不需要任何更新操作。
在实际预订时,您需要采取适当的锁定或以没有的方式写查询来处理超量预订。的房间数量不低于0。