如何通过 SQL 语句从 DB table 获取两个特定日期(由用户指定)之间的数据?

How to get data from DB table between two specific dates(specified by user) by SQL statement?

这是我的代码..我收到错误

"UCanAccess error - net.ucanaccess.jdbc.UcanaccessSQLException: incompatible data type in operation: at following line..

PreparedStatement pStmt = conn.prepareStatement(sql)

public void showCeilingMaterials(Site_Details site_detail) 
{
    String sql="SELECT SiteName, SUM(PlanTileQuantity), SUM(PlanTilePrice), SUM(PellingQuantity),SUM(PellingPrice), SUM(PowderQuantity),SUM(PowderPrice),SUM(LpattiQuantity),SUM(LpattiPrice),LpattiSize,SUM(CeilingTotalPrice) FROM CeilingMaterials Where SiteName='?' AND Date<='?' AND Date>=?";
        try (PreparedStatement pStmt = conn.prepareStatement(sql)) {
            SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
            java.util.Date parsed = format.parse(site_detail.getStartDate());
            java.sql.Date sql_date1 = new java.sql.Date(parsed.getTime());
            format.format(sql_date1);
            java.util.Date parsed1 = format.parse(site_detail.getEndDate());
            java.sql.Date sql_date2 = new java.sql.Date(parsed1.getTime());
            format.format(sql_date2);
            pStmt.setString(1, site_detail.getSiteName());
            pStmt.setDate(2, sql_date1);
            pStmt.setDate(2,sql_date2);
            ResultSet rs= pStmt.executeQuery();
            while(rs.next()){
                showCeil.setSiteName(rs.getString("SiteName"));
                showCeil.setTileQuantity(rs.getString("PlanTileQuantity"));
                showCeil.setTilePrice(rs.getString("PlanTilePrice"));
                showCeil.setPellingQuantity(rs.getString("PellingQuantity"));
                showCeil.setPellingPrice(rs.getString("PellingPrice"));
                showCeil.setLpattiQuantity(rs.getString("LpattiQuantity"));
                showCeil.setLpattiPrice(rs.getString("LpattiPrice"));
                showCeil.setLpattiSize(rs.getString("LpattiSize"));
                showCeil.setPowderQuantity(rs.getString("PowderQuantity"));
                showCeil.setPowderPrice(rs.getString("PowderPrice"));
                showCeil.setTotalCeilingPrice(rs.getString("CeilingTotalPrice"));
                show_ceil_w=new Site_Details_Show_Interface();
                show_ceil_w.showGui(showCeil);
            }
        }
    }

参数化查询的 SQL 命令文本绝不能在参数占位符周围包含引号(或其他分隔符)。您有以下内容,这是不正确的:

... Where SiteName='?' AND Date<='?' AND Date>=?

具体来说,是 Date<='?' 导致了问题中引用的错误,尽管 UCanAccess 的较新版本(在本例中为 v3.0.2)中的错误消息略有不同:

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.2 incompatible data types in combination

相反,您需要

... Where SiteName=? AND [Date]<=? AND [Date]>=?

[请注意,Date 是 Access 中的保留字(内置函数名称),因此如果要引用名为 Date.]

一旦该错误得到纠正,您 SQL 中的其他错误就会自行显现。您必须解决的下一个问题是:

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.2 expression not in aggregate or GROUP BY columns: PUBLIC.CEILINGMATERIALS.SITENAME

因为您已将 SiteName 包含在要返回的列列表中,但它不是聚合函数(例如 MIN()MAX())或 GROUP 的一部分BY 子句。 LpattiSize 也有同样的问题。

你还有

pStmt.setDate(2, sql_date1);
pStmt.setDate(2,sql_date2);

您已经为参数 #2 赋值两次(因此参数 #3 没有值)。

最后,请注意当您对一列进行 SUM() 而不提供别名时,如

SELECT ... SUM(PlanTileQuantity), ...

结果列将命名为"PlanTileQuantity"。 UCanAccess 会为其分配一个任意的列名,如 "C1"、"C2" 等。最好明确分配一个别名,例如

SELECT ... SUM(PlanTileQuantity) AS SumOfPlanTileQuantity, ...