【问题标题】:proceed to next mySQL row JAVA继续下一个 mySQL 行 JAVA
【发布时间】:2015-01-05 02:34:16
【问题描述】:

当我的查询返回结果时我遇到了问题,我无法检查下一行。

//here i assume that only "regular" is correct room type
 public boolean checkAvalibility(String roomType, String checkInDate, String checkOutDate ) throws SQLException{
        database db = new database();
        db.connect();
        this.roomType = roomType;
        if(roomType!="regular"){
            System.out.println("please select correct room type");
            return false;
        }

             myStmt = db.myConn.prepareStatement("select * from Rooms where "
                        + "RoomType = ?");
             myStmt.setString(1, roomType);
             myRs = myStmt.executeQuery();
             boolean val = myRs.next();

             while(val){

                 if(roomType.equals(myRs.getString("RoomType"))){
                     System.out.println("correct room type");
                     isType =  true;
                 }

                 if(isType == true){
                     int roomNumber = myRs.getInt("idRooms");
                     if(checkDateAvailability(checkInDate, checkOutDate, roomNumber)==true){
                        return true;
                    }
                    else{
                        return false;
                    }
                 }

             }
                System.out.println();
                 return false;
         }

这里的代码

 private boolean checkDateAvailability(String checkInDate,String checkOutDate, int roomNumber) throws SQLException{
        database db = new database();
        db.connect();
         myStmt = db.myConn.prepareStatement("select idRooms, CheckIn, CheckOut from Rooms where "
                    + "CheckIn=? AND RoomType=?");
         myStmt.setString(1, checkInDate);
         myStmt.setString(2, roomType);
         myRs = myStmt.executeQuery();
         boolean val = myRs.next();

         while(val){
 //calcDateBeetween simply takes check-in date which is in database and current check-out date converts them to integer value and compares the difference     
      if(calcDateBetween(checkOutDate, myRs.getString("CheckIn")) <=0 ){
                System.out.println("You can book the room");
                return true;
            }

            else{
            System.out.println("Dates occupied");
            return false;
            }
         }
     if(val==false){
                System.out.println("room is available for booking date is empty");
                return true;
            }
         else{
             System.out.println("i have no idea of what im doing");
         }

    return false;
    }

作为先决条件,假设我只想有两行并且不需要新记录。如果我发送与数据库中的日期匹配的签入(!)日期(在签入列中),那么一切正常,并且我已经打印出该日期已被占用。但是,如果我发送签入值 ex。 23-01-2015 和退房 03-02-2015 它没有转到 calcDateBetween() 方法,可能假设如果查询为空,则可以更新表格,并且我有一个打印输出日期可供预订。在这种情况下有什么解决方案,为什么它在第二种情况下不比较日期? 谢谢!

【问题讨论】:

    标签: java mysql compare


    【解决方案1】:

    这里有几个问题。而不是

    boolean val = myRs.next();
    
    while(val){
    

    while(myRs.next()) {
    

    否则,您不会每次都检查是否有更多行;您只是每次都使用相同的 true 或 false 值。

    此外,在您的while 循环中,您有return true;return false; - 每次都会运行其中一个。这将使您的方法结束,并且您的循环不会再次运行。你可能不希望这些return 声明在那里。

    【讨论】:

    • 那么我应该删除 'if(val==false)' 吗?
    • 两件事:1)当我删除 return 语句时程序崩溃,因为方法应该返回一些东西;2)对我的问题的第二部分有任何帮助吗?为什么不使用“27-01-2015”值检查日期?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2011-11-16
    相关资源
    最近更新 更多