Spring JdbcTemplate查询参数类型错误:列类型无效

时间:2012-11-10 17:35:30

标签: oracle spring jdbc spring-jdbc

我使用Spring Jdbc Template:

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是列nate(例如“user_id”)sord是asc / desc

传递没有参数时(仅执行

        sql.append("SELECT user_id, username ");
        sql.append("FROM users ");

)一切都好。

更新:适用于:

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   

2 个答案:

答案 0 :(得分:2)

我认为问题在于你的order by条款, 您正试图动态更改order by子句。 试试吧

    StringBuilder sql = new StringBuilder();
    sql.append("SELECT user_id, username ");
    sql.append("FROM users ");
    sql.append("WHERE :fromRecord <= rownum AND rownum <= :toRecord ");
    sql.append("ORDER BY user_id asc ");

    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("fromRecord", fromRecord);
    params.addValue("toRecord", toRecord);

如果上述方法有效,那么请使用MapSqlParameterSource来更改order by子句,而不是使用

    StringBuilder sql = new StringBuilder();
    sql.append("SELECT user_id, username ");
    sql.append("FROM users ");
    sql.append("WHERE :fromRecord <= rownum AND rownum <= :toRecord ");
    sql.append("ORDER BY %s %s ");

    //Format the sql string accordingly


MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("fromRecord", fromRecord, Types.INTEGER);
params.addValue("toRecord", toRecord, Types.INTEGER);

希望它有所帮助。

答案 1 :(得分:0)

试试这个

List<Map> rows = getJdbcTemplate().queryForList(sql);
    for (Map row : rows) {
           BigDecimal id = (BigDecimal) row.get("user_id");
            String username = (String) row.get("username");
            User user = new User(id.intValue(), username);
            users.add(user);
}

好的尝试

MapSqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("fromRecord", fromRecord);
namedParameters.addValue("toRecord", toRecord);
namedParameters.addValue("sidx", sidx);
namedParameters.addValue("sord", sord);
return this.getNamedParameterJdbcTemplate().query(query,
            namedParameters, new UserElementMapper());


public class UserMapper implements RowMapper<User> {

public EmailElement mapRow(ResultSet rs, int rowNum) throws SQLException {
    User user = new User();
    emailElement.setID(rs.getInt("user_id"));
    emailElement.setUsernameo(rs.getString("username"));
    return user;
}

}