我正在使用Spring JbdcTemplate和Postgres。
遇到Postrgres JDBC内部准备语句实现的问题原因
我的查询字符串是:
private static final String SELECT_ALL_PARTIALLY =
"SELECT login, added FROM admin ORDER BY ? ? OFFSET ? LIMIT ?";
我想用它来像:
List matches = getJdbcTemplate().query(SELECT_ALL_PARTIALLY,
(new Object[]{
column, // "login" or "added"
order, // "asc" or "desc"
offset,
limit
}),
new RowMapper() {
...
问题在于...ORDER BY ? ? OFFSET...
org.postgresql.util.PSQLException: ERROR: syntax error at or near "$2"
Position: 61
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:648) ...
如何将这两个占位符分开?
答案 0 :(得分:1)
Postgres不允许您将订购方向指定为参数。您需要为asc
和desc
订单创建不同的查询字符串,然后选择其中一个执行。