Java和Access - 使用Prepared Statement并获取日期

问题描述 投票:-1回答:1

我正在使用MS Access 2007数据库执行JavaAFX应用程序。我需要通过在日期之后过滤来从数据库中提取数据。

这项工作很好:

ResultSet rs = stat.executeQuery("SELECT * FROM Notification WHERE 
postDate>=#2018-01-18# AND getDate<=#2019-02-18#");

但这不是:

private final static String GET_ALL = 
    "SELECT * FROM Notification WHERE postDate>=? AND getDate<=?";

public ArrayList<Notification> getAllNotification(LocalDate postDate, LocalDate getDate) {

    PreparedStatement prepStmt = conn.prepareStatement(GET_ALL);       
    prepStmt.setDate(1, java.sql.Date.valueOf(postDate));
    prepStmt.setDate(2, java.sql.Date.valueOf(getDate));

    ResultSet rs = prepStmt.executeQuery();
}

我做错了什么或者我没做什么。我当然使用版本4.0.3中的ucanaccess库。有什么建议或提示吗?

java date ms-access ucanaccess
1个回答
0
投票

在MS Access中,日期'''用作标识符。您必须在每个日期的开头和结尾明确指定#。你可以尝试这个,它应该工作。

"SELECT * FROM Notification WHERE postDate>= #?# AND getDate<= #?#";

为此,我将使用以下语法。

private String GET_ALL = "";

public ArrayList<Notification> getAllNotification(LocalDate postDate, LocalDate getDate) {

    GET_ALL = "Select * FROM Notification WHERE postDate >= #" + postDate + "# AND getDate <= #" + getDate + "#";

    PreparedStatement prepStmt = conn.prepareStatement(GET_ALL);
    ResultSet rs = prepStmt.executeQuery();
}
© www.soinside.com 2019 - 2024. All rights reserved.