2015-02-23 65 views
1

我與JDBC和PostgreSQL的工作,我必須做到以下幾點:使用JDBC來編寫一個查詢

/* 
* Return list of film titles whose length is equal to or greater 
* than the minimum length, and less than or equal to the maximum 
* length. 
*/ 
public List<String> getFilmTitlesBasedOnLengthRange(Connection connection, 
       int minLength, int maxLength) { 
     List<String> result = new LinkedList<String>(); 

     return result; 
} 

我試圖通過編寫PostgreSQL的(不知道的查詢首先要解決此問題,如果它甚至正確):

SELECT title 
FROM dv_film A, dv_film B 
WHERE (A.length >= (COUNT(length) ASC LIMIT 1)) AND (B.length >= (COUNT(length) DESC LIMIT 1)); 

的dv_film關係是:

CREATE TABLE dv_film (
         film_id  integer, 
         title  varchar(50), 
         description text, 
         length  smallint, 
         rating  mpaa_rating 
); 

我在試圖解決這個問題的嘗試是:

public List<String> getFilmTitlesBasedOnLengthRange(Connection connection, 
         int minLength, int maxLength) { 
       List<String> result = new LinkedList<String>(); 

       PreparedStatement st = conn.prepareStatement("SELECT title FROM dv_film A, dv_film B WHERE (? >= (COUNT(length) ASC LIMIT 1)) AND (? >= (COUNT(length) DESC LIMIT 1));"); 

       return result; 
     } 
+0

這裏有多個問題。 SQL似乎很困惑,我會使用BETWEEN來做這件事,在這裏似乎多餘的別名是不必要的。首先簡化這個工作,然後查看一個jdbc教程。 – 2015-02-24 17:02:21

回答

0

您的語句:

PreparedStatement st = conn.prepareStatement("SELECT title FROM dv_film A, dv_film B WHERE (? >= (COUNT(length) ASC LIMIT 1)) AND (? >= (COUNT(length) DESC LIMIT 1));"); 

在這裏,您試圖列名傳遞給PreparedStatement和不能設置列名的PreparedStatement值。您只能將 列值設置爲PreparedStatement值