使用JDBC编写查询

时间:2015-02-23 19:21:53

标签: postgresql jdbc

我正在使用JDBC和PostgreSQL,我必须执行以下操作:

/*
 * 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;
        }

1 个答案:

答案 0 :(得分: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值