2011-03-31 14 views
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種模式的數據庫,我用錯了一個(舊版本)

+1

只是要清楚。當「開始」是2011年3月23日,「完成」是2011年3月30日,您的​​Java代碼打印出「對不起。沒有結果!」但是如果您將SELECT查詢複製並粘貼到pgAdmin並在相同的數據庫上運行,則會獲得數據。正確? – NPE 2011-03-31 14:41:06

+0

是的,這是正確的! – user195257 2011-03-31 14:42:45

+1

您確定您正在爲java以及pdAdmin執行相同數據庫上的sql嗎? – Naved 2011-03-31 14:45:55

回答

1

嘗試使用PreparedStatement而不是將您的值連接在一起。我也建議不要打印\r個字符,除非你真的想從行首開始覆蓋。我已經重寫了代碼的一部分,以顯示我通常如何執行此操作;希望它能爲你工作:

PreparedStatement ps = Connection.prepareStatement("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 ? AND ?" 
      + " GROUP BY SalesRep.SalesRepID, SalesRep.Name " 
      + " ORDER BY Total_Value DESC"; 
try { 
    ps.setDate(1, start); 
    ps.setDate(2, finish); 

    ResultSet rs = ps.executeQuery(); 
    try { 
     String name; 
     int total; 
     double totalPrice; 
     int count = 0; 
     String output; 

     while (rs.next()) { 
      if (count == 0) { 
       System.out.printf("%-20s %-18s %-15s\n", "Sales Rep", "Total units sold", "Total Value"); 
      } 

      count++; 
      System.out.println("Sales representative performace review from " + start + " to " + finish); 
      name = rs.getString(1); 
      total = rs.getInt(2); 
      totalPrice = rs.getDouble(3); 
      System.out.printf("%-20s %-18d £%-15.2f\n", name, total, totalPrice); 
     } 

     if (count > 0) { 
      System.out.println("\nQuery complete with " + count + " results!\n"); 
     } else { 
      System.out.println("\nSorry.. no results!\n"); 
     } 
    } 
    finally { 
     rs.close(); 
    } 
} 
finally { 
    ps.close(); 
} 

如果這不起作用,您可能有時區的問題。通過ps.setDate(...)中的Calendar對象更改您傳遞的日期的時區。

相關問題