0
我已經在pgAdmin中測試了我的sql,它返回的結果很好。但是,如果使用Java,在日期爲2011年3月25日和2011年3月30日時不會返回任何值,但在開始日期爲2011年3月23日時會返回任何值。SQL沒有在java中運行時它應該
我有System.out.println'ed我創建的SQL,並在pgAdmin中運行它,它返回的結果很好,但在Java它沒有,任何想法爲什麼?數據庫正在打開和關閉,以及統計和結果集罰款,我沒有看到問題?這裏是我的代碼片段
String sql = "SELECT Salesrep.Name, SUM(OrderLine.Quantity) AS Total_Sold, SUM(OrderLine.UnitSellingPrice * Orderline.Quantity) AS Total_Value"
+ " FROM SalesRep, OrderLine, ShopOrder WHERE ShopOrder.SalesRepID = SalesRep.SalesRepID"
+ " AND OrderLine.ShopOrderID = ShopOrder.ShopOrderID"
+ " AND ShopOrder.OrderDate BETWEEN '"+start+"' AND '"+finish+"'"
+ " GROUP BY SalesRep.SalesRepID, SalesRep.Name "
+ " ORDER BY Total_Value DESC";
System.out.println(sql);
try {
rs = Database.stmt.executeQuery(sql);
String name;
int total;
double totalPrice;
int count = 0;
String output;
if (rs.next()) {
System.out.println("Sales representative performace review from " + start + " to " + finish);
name = rs.getString(1);
total = rs.getInt(2);
totalPrice = rs.getDouble(3);
output = String.format("%-20s %-18s %-15s", "Sales Rep", "Total units sold", "Total Value");
System.out.println(output);
output = String.format("%-20s %-18d £%-15.2f", name, total, totalPrice);
System.out.println(output);
count++;
while (rs.next()) {
name = rs.getString(1);
total = rs.getInt(2);
totalPrice = rs.getDouble(3);
output = String.format("%-20s %-18d £%-15.2f", name, total, totalPrice);
System.out.println(output);
count++;
}
}
if (count > 0) {
System.out.println("\r\nQuery complete with " + count + " results! \r\n");
} else {
System.out.println("\r\nSorry.. no results! \r\n");
}
rs.close();
解決:
我有2種模式的數據庫,我用錯了一個(舊版本)
只是要清楚。當「開始」是2011年3月23日,「完成」是2011年3月30日,您的Java代碼打印出「對不起。沒有結果!」但是如果您將SELECT查詢複製並粘貼到pgAdmin並在相同的數據庫上運行,則會獲得數據。正確? – NPE 2011-03-31 14:41:06
是的,這是正確的! – user195257 2011-03-31 14:42:45
您確定您正在爲java以及pdAdmin執行相同數據庫上的sql嗎? – Naved 2011-03-31 14:45:55