Java PreparedStatement.setDate(),查询返回无结果

时间:2013-10-15 09:59:17

标签: java sql oracle date prepared-statement

我尝试使用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上测试时,它会返回正确的结果。

谢谢, 胡志明

0 个答案:

没有答案