Postgresql 使用JDBC编写查询
我正在使用JDBC和PostgreSQL,我必须做以下工作:Postgresql 使用JDBC编写查询,postgresql,jdbc,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
/*
* 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;
}
dv_电影关系为:
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;
}
公共列表GetFilmTitleBasedOnLengtrange(连接,
int最小长度,int最大长度){
列表结果=新建LinkedList();
PreparedStatement st=conn.prepareStatement(“从DVU胶片A、DVU胶片B中选择标题,其中(?>=(计数(长度)ASC限制1))和(?>=(计数(长度)描述限制1));”;
返回结果;
}
您的声明:
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值
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));");