Reputation: 663
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. 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. What can be the solution in this situation and why it does not compare dates in second case?
Thanks !
Upvotes: 0
Views: 406
Reputation: 79848
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.
Also, within your while
loop, you have return true;
and 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.
Upvotes: 3