1

I have a query and a few parameters as follows,

String query = "SELECT * FROM table_name ORDER BY ? LIMIT ? ";

//I am creating this 'sortString' on runtime based on some user inputs
String sortString = " column1 ASC, column 2 ASC "; 

int count =5;

I am calling the jdbcTemplate method as follows,

List<Map<String, Object>> rows = getJdbcTemplate().queryForList(query, sortString, count);

The query that is actually used by the jdbcTemplate is as follows,

SELECT * FROM table_name ORDER BY ' column1 ASC, column 2 ASC ' LIMIT 5

Now, the ORDER BY clause does not works since the criteria is put up inside ' ' by jdbcTemplate. How can I add the string to the query without the jdbcTemplate adding the " ' " by default.

I want the query to be,

SELECT * FROM table_name ORDER BY column1 ASC, column 2 ASC LIMIT 5
4

1 回答 1

0

当您动态生成查询的整个部分时,您不能使用准备好的语句。在?准备好的语句中总是代表一个值。//我正在创建这个'sortString'

String sortString = " column1 ASC, column 2 ASC "; 

String query = "SELECT * FROM table_name ORDER By " + sortString + " LIMIT ? ";

于 2015-05-25T15:57:35.617 回答