如何在java中按Date获取访问数据

时间:2013-11-25 17:26:39

标签: java

SELECT ItemName, SUM(Unit) AS SoldUnit, SUM(Total) AS TotalAmount,
       SUM(Profit) AS TotalProfit
FROM InvoiceTable
WHERE DateSO='"+dateTxt.getText()+"'
GROUP BY ItemName;

这是我的查询,但我得到了

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

在访问数据库DateSo中,Date/Time类型为{{1}} 请帮帮我。

1 个答案:

答案 0 :(得分:0)

我建议你尝试这样的事情

private static PreparedStatement prepareStatement(
    Connection conn, java.util.Date date)
    throws SQLException {
  final String sql = "SELECT ItemName, SUM(Unit) AS SoldUnit, "
      + "SUM(Total) AS TotalAmount, "
      + "SUM(Profit) AS TotalProfit "
      + "FROM InvoiceTable WHERE DateSO=? GROUP BY ItemName";
  PreparedStatement ps = conn.prepareStatement(sql);
  ps.setDate(1, new java.sql.Date(date.getTime()));
  return ps;
}

然后使用ResultSet rs = ps.executeQuery();,请务必阅读ResultSet并始终clean up after yourself

最后,如果您需要一系列日期(在SQL中使用BETWEEN,并传入两个日期对象)...