我使用了Spring JDBC模板道:JdbcTemplate類的查詢參數類型錯誤:無效列類型
public List<User> getUsersForGrid(int rows, int page, String sidx,
String sord) {
int fromRecord = 0;
int toRecord = 0;
toRecord = page * rows;
fromRecord = (page - 1) * rows;
StringBuilder sqlB = new StringBuilder();
sqlB.append("SELECT user_id, username ");
sqlB.append("FROM users ");
sqlB.append("WHERE :fromRecord <= rownum AND rownum <= :toRecord ");
sqlB.append("ORDER BY %s %s ");
String sql = String.format(sqlB.toString(), sidx, sord);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("fromRecord", fromRecord);
params.addValue("toRecord", toRecord);
List<Map<String, Object>> rsRows = this.jdbcTemplate.queryForList(sql
.toString(),params);
List<User> users = new ArrayList<User>();
for (Map<String, Object> row : rsRows) {
BigDecimal id = (BigDecimal) row.get("user_id");
String username = (String) row.get("username");
User user = new User(id.intValue(), username);
users.add(user);
}
return users;
}
,並得到java.sql.SQLException: Invalid column type
SIDX是列內特( 「USER_ID」 爲例)SORD是ASC /降序
當通過沒有PARAMS(僅執行
sql.append("SELECT user_id, username ");
sql.append("FROM users ");
)EV一切都好。
更新:兼容於:
sqlB.append("WHERE ? <= rownum AND rownum <= ? ");
和
this.jdbcTemplate.queryForList(sql.toString(),new Object[]{fromRecord, toRecord});
好像與Spring MapSqlParameterSource和命名參數的問題。我使用Spring 3.1.3
DB是Oracle 11.2
describe users;
Name Null Type
------------------------------ -------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
USER_ID NOT NULL NUMBER
USERNAME NOT NULL VARCHAR2(40)
PASSWORD NOT NULL VARCHAR2(20)
ENABLED NOT NULL NUMBER
這將工作。當我傳遞任何參數並只執行SELECT user_id時,用戶名FROM用戶沒關係。但我想傳遞參數。當queryForList被執行時引發異常,所以循環部分不重要(我假設) –