In cases like this it can be very helpful to isolate a particular piece of the application and use a little test program to experiment with it. For example, I created a [Booking] table in Access...
BookingID RmID BookArrivalDate BookDepartureDate
--------- ---- --------------- -----------------
1 101 2013-10-21 2013-10-23
2 102 2013-11-01 2013-11-03
...and then used the following little test program to run queries against it:
import java.sql.*;
import org.joda.time.*;
public class JDBCQuery {
public static void main(String args[]) {
Connection conn = null;
PreparedStatement s = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
"DBQ=C:\\Users\\Public\\Database1.accdb;");
LocalDate requestedArrivalDate = LocalDate.parse("2013-10-12");
LocalDate requestedDepartureDate = LocalDate.parse("2013-10-21");
s = conn.prepareStatement(
"SELECT BookingID, RmID FROM Booking WHERE BookDepartureDate > ? AND BookArrivalDate < ?");
s.setDate(1, new java.sql.Date(requestedArrivalDate.toDate().getTime()));
s.setDate(2, new java.sql.Date(requestedDepartureDate.toDate().getTime()));
ResultSet rs = s.executeQuery();
System.out.println(String.format("Requested arrival: %s", requestedArrivalDate.toString()));
System.out.println(String.format("Requested departure: %s", requestedDepartureDate.toString()));
System.out.println("The following bookings conflict with that request...");
if (rs!=null) {
while (rs.next()) {
System.out.println(String.format("BookingID: %d, RmId: %d", rs.getInt("BookingID"), rs.getInt("RmID")) );
}
}
System.out.println("(end of list)");
} catch( Exception e ) {
e.printStackTrace();
} finally {
try {
if (s != null) {
s.close();
}
if (conn != null) {
conn.close();
}
} catch( Exception e ) {
e.printStackTrace();
}
}
}
}
Once I was convinced that I got the SQL logic right and was able to isolate the conflicting Bookings (and hence the RmID values that I could not use) then it became clear that I wanted my final query to look something like this:
SQL =
"SELECT RmID FROM Room " +
"WHERE " +
"RmID NOT IN " +
"(" +
"SELECT RmID FROM Booking " +
"WHERE BookDepartureDate > ? AND BookArrivalDate < ? " +
")" +
"AND RmType = ?";
s = conn.prepareStatement(SQL);
s.setDate(1, new java.sql.Date(requestedArrivalDate.toDate().getTime()));
s.setDate(2, new java.sql.Date(requestedDepartureDate.toDate().getTime()));
s.setString(3, "Single");
ResultSet rs = s.executeQuery();