gpt4 book ai didi

java - 如何从java中的access中获取两个日期之间的数据?

转载 作者:行者123 更新时间:2023-12-01 13:47:47 25 4
gpt4 key购买 nike

String weeklyQuery = "Select ItemName, SUM(Unit) As SoldUnit, SUM(Total) As
TotalAmount, SUM(Profit) As TotalProfit
from InvoiceTable
Where DateSO BETWEEN?AND?GROUPBY ItemName"

通过使用此查询,不会出现异常或错误,但未生成结果意味着未获取单个记录。

最佳答案

好吧,您正在尝试以非常奇怪的方式对数据库进行查询。事实上,在 java 中从来没有这样做过

你需要做什么:

Connection con = DriverManager.getConnection(
"jdbc:myDriver:myDatabase",
username,
password);

PreparedStatement pstmt = con.prepareStatement(
"Select ItemName
, SUM(Unit) As SoldUnit
, SUM(Total) As TotalAmount
, SUM(Profit) As TotalProfit
from InvoiceTable
Where DateSO BETWEEN ? AND ? GROUPBY ItemName");

pstmt.setDate(1, dateSOMin);
pstmt.setDate(2, dateSOMax);
ResultSet rs = pstmt.executeQuery();

while (rs.next()) {
String itemName = rs.getString("ItemName");
int soldUnit= rs.getInt("SoldUnit");
int totalAmount= rs.getInt("TotalAmount");
float totalProfit = rs.getFloat("TotalProfit");

//do something with it
}

此外,请确保您的数据库 sql 方言允许您编写 BETWEEN min AND max

可能您的数据库只允许您写入 where DateSO >= min AND DateSO <= max

我建议您阅读本教程: http://docs.oracle.com/javase/tutorial/jdbc/index.html

关于java - 如何从java中的access中获取两个日期之间的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20218966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com