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

时间:2015-10-23 22:12:24

标签: java sql jdbc ucanaccess

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

  

" UCanAccess错误 - net.ucanaccess.jdbc.UcanaccessSQLException:操作中不兼容的数据类型:在以下行..

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);
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

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

my-canvas

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

  

net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc ::: 3.0.2组合不兼容的数据类型

相反,你需要

Date<='?'

[请注意,... Where SiteName=? AND [Date]<=? AND [Date]>=? 是Access中的保留字(内置函数名),因此如果要引用名为Date的列,则应在其周围使用方括号。 ]

纠正错误后,SQL中的其他错误会自行显示。您需要解决的下一个问题是:

  

net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc ::: 3.0.2表达式不在聚合或GROUP BY列中:PUBLIC.CEILINGMATERIALS.SITENAME

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

你也有

LpattiSize

您已为参数#2分配了两次值(因此参数#3没有值)。

最后,请注意,当您在不提供别名的情况下SUM()列时,如

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

生成的列将命名为“PlanTileQuantity”。 UCanAccess将为其分配一个名为“C1”,“C2”等的列名称。最好明确指定别名,例如,

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