compare元素

n7taea2i  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(241)

我创建了一个简单的web应用程序。我需要检查一下,当有人预订房间时,到达日期是否不同。例如:人员a:在数据中预订房间:2018年7月12日人员b:在数据中预订房间:2018年7月12日(他不能,因为还有人员a)。
我有这个数据库:
酒店预订
cid,cname,email,到达日期,离开日期,人,房间,键入房间,评论,状态,uid。
这是我的密码
酒店预订.java

package hotel;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author OOPs
 */
public class Hotelbooking extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String s1 = request.getParameter("id");
        String s2 = request.getParameter("name");
           String s3 = request.getParameter("email");
           String s4 = request.getParameter("arrivaldate");
           String s5 = request.getParameter("departuredate");
           String s6 = request.getParameter("person");
           String s7 = request.getParameter("rooms");
           String s8 = request.getParameter("type_room");
           String s9 = request.getParameter("comment");
           String s10 = request.getParameter("status");

           // sesson..........
           HttpSession session=request.getSession(true);
          String s11 = (String)session.getAttribute("sessname");

       out.println(s11);

       try {

            Class.forName("com.mysql.jdbc.Driver");
      //  out.println("driver loaded");
     Connection  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/HotelReservation","root" ,"123456789");
      //  out.println("Connect");
      Statement  st =  con.createStatement();
        //  out.println("conncetion successfull");

       int rs =  st.executeUpdate("insert into hotel_booking (cname,email,arrival_date,departure_date,person,room,type_room,comment,status,uid)values ('"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+s7+"','"+s8+"','"+s9+"','"+s10+"','"+s11+"')");
         // out.println(s1);
         // out.println(s2);
          out.println("<h1> Register sucsefulltttt </h1>");

          /* if(rs == 1)
           {
           out.println("<script type=\"text/javascript\">");
       out.println("alert('send successfully');");
       out.println("</script>");
       //response.sendRedirect("home.jsp");
           }else{

       }*/

          response.sendRedirect("thankyoureg.jsp"); 

        }catch(Exception e){
        out.println("nahiiiiiiiiiiiii" +e);
        }
        finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

你认为我该怎么办??谢谢你的帮助。

aiazj4mn

aiazj4mn1#

检查如下查询是否返回 0 .

SELECT count(*)
       FROM hotel_booking
       WHERE arrival_date <= $1
             AND departure_date > $1,
             AND room = $2;

如果它回来了 0 客人要入住的当天房间没有人。否则就是。
替代品 $1 客人希望到达的当天 $2 房间(号码、名字或你用的任何东西)。
我假设签入/ checkout 时间的处理方式如下:
在某一天的晚上办理入住手续。
在某一天的早晨退房。也就是说,一位客人可以在另一位客人退房的当天办理入住手续。
如果这个假设是错误的,那么就必须相应地调整比较运算符。
还可以考虑使用参数化查询,而不是使用未初始化的用户输入值进行字符串连接。后者是危险的,您的应用程序容易受到sql注入攻击。另外,参数化查询有助于为给定的数据类型找到正确的表示形式,从而防止由于错误表示值而导致的错误。

相关问题