String weeklyQuery = "Select ItemName, SUM(Unit) As SoldUnit, SUM(Total) As
TotalAmount, SUM(Profit) As TotalProfit
from InvoiceTable
Where DateSO BETWEEN?AND?GROUPBY ItemName"
通过使用此查询,没有异常或错误阻塞bt结果未生成意味着没有提取单个记录。
答案 0 :(得分:1)
嗯,您正试图以非常奇怪的方式对数据库进行查询。 实际上,它是在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 dialect允许您编写BETWEEN min AND max
?
您的数据库可能只允许您编写where DateSO >= min AND DateSO <= max
我建议您阅读本教程: http://docs.oracle.com/javase/tutorial/jdbc/index.html
答案 1 :(得分:0)
由于缺乏信息,我会提出一个建议。
将between子句更改为DateSo&gt; smallestDateYouWant和DateSo&lt; largestDateYouWant
另外,附上样本集,以便我们可以在不盲目的情况下进行测试。
答案 2 :(得分:0)
好吧,你写的查询语句没问题。但你可以尝试这样:
String weeklyQuery = "Select ItemName, SUM(Unit) As SoldUnit, SUM(Total) As
TotalAmount, SUM(Profit) As TotalProfit
from InvoiceTable
WHERE DateSO >= ? AND DateSO <= ? GROUPBY ItemName"