[英]proceed to next mySQL row JAVA
I ran into problem when my query returns result and I cannot check the next row. 我的查询返回结果时遇到问题,无法检查下一行。
//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;
}
this code here 此代码在这里
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;
}
As a pre-requirement, let's say I want to have only two rows and I don't need new records. 作为前提条件,假设我只希望有两行,并且不需要新记录。 If I send check-IN(!) date which matches the one in database (in Check-in column) then everything works fine, and I have print out saying that date is occupied.
如果我发送与数据库中的日期(在“入住”列中)相匹配的入住日期(!),则一切正常,并且我已打印出该日期已被占用。 But if I send check-in value ex.
但是,如果我发送入住值ex。 23-01-2015 and check-out 03-02-2015 it does not go to
calcDateBetween()
method, probably assuming that if query was empty then the table can be updated and I have a printout that dates are available for booking. 2015年1月23日和退房2015年3月2日,它不会转到
calcDateBetween()
方法,可能假设如果查询为空,则该表可以更新,并且我有一个打印输出,该日期可供预订。 What can be the solution in this situation and why it does not compare dates in second case? 在这种情况下可以采取什么解决方案,为什么在第二种情况下不比较日期呢? Thanks !
谢谢 !
There are a couple of issues here. 这里有几个问题。 Instead of
代替
boolean val = myRs.next();
while(val){
write 写
while(myRs.next()) {
Otherwise, you're not checking each time whether there are more rows; 否则,您不必每次都检查是否还有更多行; you're just using the same true or false value each time.
您每次都使用相同的true或false值。
Also, within your while
loop, you have return true;
同样,在
while
循环中,您还return true;
and return false;
并
return false;
- and one of these is going to run each time. -其中之一将每次运行。 That will make your method end, and your loop won't run again.
这将使您的方法结束,并且循环不会再次运行。 You probably don't want these
return
statements in there. 您可能不希望这些
return
语句存在其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.