【问题标题】:Using JDBC to Write a Query使用 JDBC 编写查询
【发布时间】:2015-04-25 05:49:36
【问题描述】:

我正在使用 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;
        }

【问题讨论】:

    标签: postgresql jdbc


    【解决方案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 值

    【讨论】:

      猜你喜欢
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 2011-05-20
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多