简体   繁体   English

使用JDBC编写查询

[英]Using JDBC to Write a Query

I am working with JDBC and PostgreSQL and I have to do the following: 我正在使用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;
}

I tried to first solve this problem by writing a query in PostgreSQL (not sure if it's even correct): 我试图通过在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));

The dv_film relation is: dv_film关系为:

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

My attempt at trying to solve the problem is: 我尝试解决该问题的尝试是:

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

your statement: 您的声明:

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));");

Here you are trying to pass column names to PreparedStatement and you cannot set column names as PreparedStatement values. 在这里,您尝试将列名传递给PreparedStatement,并且不能将列名设置为PreparedStatement值。 You can only set column values as PreparedStatement values 您只能将列值设置为PreparedStatement值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM