1

我刚刚从使用普通旧连接切换到 JdbcTemplate,当我运行我的应用程序时,我收到了这个:

org.springframework.jdbc.InvalidResultSetAccessException: Invalid column name; nested exception is java.sql.SQLException: Invalid column name

我发现当您使用它的 getXXX 方法时,SqlRowSet 无法识别列名别名。有没有办法使用 SqlRowSet 解决这个问题?还是我只需要使用这些列的完整列名或索引?

提前致谢,

ktm

4

2 回答 2

1

我不确定 SqlRowSet,但如果您不需要断开连接的 ResultSet (SqlRowSet),另一种选择是使用以 RowMapper 作为参数的查询方法之一。RowMapper 将获得一个 ResultSet,它应该支持别名。

于 2013-01-28T02:40:54.537 回答
0

在类 ResultSetWrappingSqlRowSet 源中,

public class ResultSetWrappingSqlRowSet implements SqlRowSet

像 getXXX 这样的包装方法使用字段“标签”而不是字段名称。

public int getInt(String columnLabel) throws InvalidResultSetAccessException {
    return getInt(findColumn(columnLabel));
}


public int findColumn(String columnLabel) throws InvalidResultSetAccessException {
    Integer columnIndex = this.columnLabelMap.get(columnLabel);
    if (columnIndex != null) {
        return columnIndex;
    }
    else {
        try {
            return this.resultSet.findColumn(columnLabel);
        }
        catch (SQLException se) {
            throw new InvalidResultSetAccessException(se);
        }
    }
}

ResultSetMetaData#g​​etColumnLabel(int)文档链接。

public ResultSetWrappingSqlRowSet(ResultSet resultSet) throws InvalidResultSetAccessException {
    this.resultSet = resultSet;
    try {
        this.rowSetMetaData = new ResultSetWrappingSqlRowSetMetaData(resultSet.getMetaData());
    }
    catch (SQLException se) {
        throw new InvalidResultSetAccessException(se);
    }
    try {
        ResultSetMetaData rsmd = resultSet.getMetaData();
        if (rsmd != null) {
            int columnCount = rsmd.getColumnCount();
            this.columnLabelMap = new HashMap<String, Integer>(columnCount);
            for (int i = 1; i <= columnCount; i++) {
                this.columnLabelMap.put(rsmd.getColumnLabel(i), i);
            }
        }
        else {
            this.columnLabelMap = Collections.emptyMap();
        }
    }
    catch (SQLException se) {
        throw new InvalidResultSetAccessException(se);
    }

}

所以你需要检查你的 sql 和数据库配置(支持从 sql 获取数据库元数据)。

于 2013-01-28T02:47:52.627 回答