简体   繁体   English

在数据库中的条目之间循环问题

[英]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. 我正在尝试使用Java浏览Access数据库,以检查日期范围是否落在另一个日期范围之间,以避免重复的保留。 I am running into an SQL error that says "ResultSet is Closed". 我遇到一个SQL错误,提示“ ResultSet已关闭”。 I believe it is because I am using two resultSet in order to check two different dates withing one while loop. 我相信这是因为我正在使用两个resultSet以便使用一个while循环检查两个不同的日期。 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: 编辑:根据要求这里是dateCheck代码:

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? 为什么仅在SQL可以给您结果时就摆弄?

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: 在您的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. 如果有任何预订,您将收到预订ID。 If not null. 如果不为null。 You can also put above SQL within another select command and return true or false value. 您还可以在另一个选择命令中的SQL上方放置并返回true或false值。 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. 附:从移动电话接听,请仔细检查SQL中是否有错字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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