0

我尝试使用 PreparedStatement.setDate 方法将 Date 对象输入到在 Oracle Db 上运行的 SQL 查询中。问题是无论我为 Date 选择什么参数,结果总是空的。

SQL查询:

Select (TO_CHAR(transaction_date, 'YYYY,MM,DD')) transaction_date_graph, \
(TO_CHAR(transaction_date, 'DD MON YYYY')) transaction_date_view, \
sum(trans_amount) amount from ar_customer_balance_itf \
where transaction_date>=? and transaction_date<=? \
group by transaction_date ORDER BY transaction_date

的java代码:

public List<Map<String, String>> queryData(String query) {

List<Date> dates = analyseDate(query);
ResultSet rs = null;
ResultSetMetaData rsmd;
int count = 0;
List<Map<String, String>> propertyList = new LinkedList<Map<String, String>>();
try {
    if (dates.size()==2) // If there are two dates in the query
    {   
        stmt_dateRange.setDate(1, dates.get(0));
        stmt_dateRange.setDate(2, dates.get(1));
        rs = stmt_dateRange.executeQuery();
        System.out.println("Executed query.");
    } else if (dates.size()==1) // If there is only one date
    {   
        stmt_dateStart.setDate(1, dates.get(0));
        rs = stmt_dateStart.executeQuery();
        System.out.println("Executed query.");
    }

    rsmd = rs.getMetaData();
    while (rs.next()) {
        Map<String, String> fieldMap = new HashMap<String, String>();
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            if (rs.getString(i) == null)
                break;
            fieldMap.put(rsmd.getColumnLabel(i).toLowerCase(),
                    rs.getString(i));
        }
        propertyList.add(fieldMap);
        count++;
    }

} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println("Query result: " + count + " rows");
return propertyList;

}

输入的 Date 参数是正确的,当我在 DbVisualizer 上对其进行测试时,它确实返回了正确的结果。

谢谢,明

4

0 回答 0