用java控制数据库中的数据

6gpjuf90  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(345)

早上好,我在创建一个web应用程序时遇到了问题。我对这种编程很不熟悉。所以我的网络应用程序应该负责预订酒店房间。我的问题是当有人想订房间时要核对一下,这样你就不能在同一时间订两个相等的房间了。只有在写了代码之后,才不会执行检查,房间仍然被预订。我怎么能照你说的做呢?非常感谢你的帮助。

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.id_prenotazione != res2.id_prenotazione\n" +
        "AND (res1.arrivaldate <= res2.departuredate)\n" +
        "AND (res2.arrivaldate <= res1.departuredate)");

    // ResultSet rs2  = stmt.executeQuery(check);
    out.println("<h1> Stringa chek eseguita </h1>");

    if (check) { // THIS DOESN't WORK OF COURSE
        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 + "'");

    }

reservation ( id_prenotazione int(11)非空自动增量, login varchar(45)不为空, email varchar(45)不为空, typeroom varchar(45)不为空, numroom int(11)不为空, arrivaldate 日期时间不为空, departuredate 日期时间不为空,
主键( id_prenotazione )
users ( login varchar(45)不为空, firstname varchar(45)不为空, lastname varchar(45)不为空, password varchar(45)不为空,
主键( login )

vc9ivgsu

vc9ivgsu1#

在您的check select中使用了一个与同一个表的连接,但是还没有插入任何内容,因此查询是没有意义的。您需要对照要存储的预订的值进行检查。

SELECT COUNT(*) 
FROM reservation 
WHERE numroom = $roomnumber -- I am guessing numroom is room number
  AND ($startdate >= arrivaldate AND $startdate <= departuredate)
   OR ($enddate >= arrivaldate AND $enddate <= departuredate)

替换变量( $... )你的价值观。你可能想研究一下如何相互验证日期,也许应该是这样 <, > 而不是 <=, >= 在比较中。
在java中,这将是

String query = "SELECT COUNT(*) FROM reservation WHERE numroom = '" + numroom + "' AND ('" + arrivaldate + "' >= arrivaldate AND '" + arrivaldate + "' <= departuredate) OR ('" + departuredate + "' >= arrivaldate AND '" + departuredate + "' <= departuredate)

相关问题