pgAdmin에서 내 sql을 테스트했지만 결과가 정상적으로 반환됩니다. 그러나 Java에서 날짜가 25/03/2011 및 30/03/2011 일 때 값을 반환하지 않지만 시작 날짜가 23/03/2011 인 경우에는 값을 반환하지 않습니다.java가 실행될 때 SQL이 실행되지 않습니다.
나는 System.out.println'ed를 만들어서 pgAdmin에서 실행하고 결과를 잘 반환하지만 java에서는 왜 그런가? 데이터베이스가 opend되고 닫혀있을뿐만 아니라 statment와 결과가 잘 설정되어 있습니다. 문제가 보이지 않습니까? 여기
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 스키마를했고 내가 잘못된 (이전 버전)를 사용했다
그냥 알기 쉽습니다. 'start'가 2011-03-23이고'finish'가 2011-03-30 일 때 자바 코드는 "Sorry .. no results!"라는 문구를 출력합니다. 만약 당신이 pgAdmin에'SELECT' 쿼리를 복사하여 붙여 넣고 같은 데이터베이스에 대해 실행하면, 데이터를 얻습니다. 옳은? – NPE
네 맞습니다! – user195257
java와 pdAdmin에 대해 동일한 데이터베이스에서 sql을 실행하고 있습니까? – Naved