简体   繁体   中英

Issue With Cycling Through Entries In Database

I am attempting to go through my Access database with Java in order to check if a date range falls in between another date range to avoid duplicate reservations. I am running into an SQL error that says "ResultSet is Closed". I believe it is because I am using two resultSet in order to check two different dates withing one while loop. Here is the code:

String date1Query = "SELECT checkIn FROM Reservation WHERE roomLocation LIKE '" + room + "'";
String date2Query = "SELECT checkOut FROM Reservation WHERE roomLocation LIKE '" + room + "'";

ResultSet date1RS = statement.executeQuery(date1Query);
ResultSet date2RS = statement.executeQuery(date2Query);
    while (date1RS.next()) {
        date1 = date1RS.getDate("checkIn");
        date2 = date2RS.getDate("checkOut");
        dateCheckBool = dateCheck.dateCheck(date1, date2, checkIn, checkOut);    

        if (dateCheckBool == true)
            break;
    }

How else can I format this to achieve the same functionality (being able to check the input dates by the user against the already existing dates in the database) but not get an error? If this is not clear enough what I am trying to do I will try to clarify further upon request. Thank you!

EDIT: As requested here is the dateCheck code:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class dateCheck {
    public boolean dateCheck(Date date1, Date date2, String date3, String 
date4){
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    Date dateCheck1;
    Date dateCheck2;
    boolean dateRange1 = false;
    boolean dateRange2 = false;
    try {
        dateCheck1 = dateFormat.parse(date3);
        dateCheck2 = dateFormat.parse(date4);

        if(dateCheck1.before(date1) || dateCheck1.after(date2))
            dateRange1 = false;
        else
            dateRange1 = true;
        if(dateCheck2.after(date2))
            dateRange2 = false;
        else
            dateRange2 = true;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    if(dateRange1 == false && dateRange2 == false)
        return false;
    else
        return true;
    }
}

Why fiddling around when SQL alone can give you the result?

If you wish to know whether a reservation is already in place, just ask the database.

 SELECT id as result From reservations
 Where (
        ([RoomLocation] like ?)
        And (
            (? between [checkin] and [checkout])
            OR (? between [checkin] and [checkout])
        ) 
);

in your java:

statement.setString(1,room);
statement.setDate(2,date1);
statement.setDate(3,date2);

..execution..

If there are any reservations you will receive the reservation id. If not null. You can also put above SQL within another select command and return true or false value. If you are having issues with incompatible date formats, you might have to format the date and pass it as string.

Ps: answering from mobile, double check the SQL for typos.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM