作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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/
我是一名优秀的程序员,十分优秀!