当我的查询返回结果时我遇到了问题,我无法检查下一行。
//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;
}
作为先决条件,假设我只想拥有两行并且不需要新记录。如果我发送与数据库中的日期(在“签到”列中)匹配的签到(!)日期,那么一切正常,并且我打印出该日期已被占用。但是,如果我发送 checkin 值 ex。 2015 年 1 月 23 日和 2015 年 2 月 3 日 checkout 它不会转到 calcDateBetween()
方法,可能假设如果查询为空则可以更新表并且我有一个打印输出日期可供预订。在这种情况下有什么解决方案,为什么在第二种情况下不比较日期?
谢谢 !
最佳答案
这里有几个问题。而不是
boolean val = myRs.next();
while(val){
写
while(myRs.next()) {
否则,您不会每次都检查是否有更多行;您只是每次都使用相同的 true 或 false 值。
此外,在您的 while
循环中,您有 return true;
和 return false;
- 其中一个将运行每个时间。这将使您的方法结束,并且您的循环将不会再次运行。您可能不希望这些 return
语句在那里。
关于java - 继续下一个 mySQL 行 JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27772701/