I have tested my sql in pgAdmin, it returns results fine. However when in Java it does not return any values when the dates are 25/03/2011 and 30/03/2011 but it does when the start is 23/03/2011.
i have System.out.println'ed the sql that i created and run it in pgAdmin and it returns the results fine, but in java it doesnt, any ideas why? The database is being opend and closed as well as the statment and result set fine, i dont see the problem? Here is a snippet of my code
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();
Solved:
I had 2 schemas for the database and i was using the wrong one (an old version)
startis 2011-03-23 andfinishis 2011-03-30, your Java code prints out "Sorry.. no results!" yet if you copy-and-paste theSELECTquery into pgAdmin and run it against the same database, you get data. Correct?Statementobject for this query?