9

我收到以下错误:

HTTP Status 500 - Request processing failed; nested exception is
org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar 
[SELECT id, name FROM track WHERE category_id = 1 ORDER BY name]; nested exception is
java.sql.SQLException: Column 'category_id' not found.

但是,当我将错误中列出的非常 select 语句复制并粘贴到 mysql shell 中时,我得到了结果,这是预期的,因为表track中有 column category_id

此错误的原因可能是什么?

这是表的创建语句track

CREATE TABLE track (
 id SERIAL
,name VARCHAR(50)
,category_id BIGINT UNSIGNED -- This references a serial (bigint unsigned)
,CONSTRAINT track_id_pk PRIMARY KEY (id)
,CONSTRAINT track_category_id_fk FOREIGN KEY
  (category_id) REFERENCES category (id)
);

以下是我的 dao 课程中关于track表格的一些内容:

private static final class TrackMapper implements RowMapper<Track> {
    @Override
    public Track mapRow(ResultSet resultSet, int rowNum) throws SQLException {
        Track track = new Track();
        track.setId(resultSet.getInt("id"));
        track.setName(resultSet.getString("name"));
        track.setCategoryId(resultSet.getInt("category_id"));
        return track;
    }
}
public List<Track> getTracks(int categoryId) {
    String sql = "SELECT id, name FROM track WHERE category_id = " + categoryId + " ORDER BY name";
    return jdbcTemplate.query(sql, new TrackMapper());
}
4

1 回答 1

31

检查您的 SQL 语句——您需要category_id在列列表中包含:

String sql = "SELECT id, name, category_id FROM track WHERE category_id = " + categoryId + " ORDER BY name";

它失败了,因为您试图从中提取category_id并且ResultSet它不存在。

于 2013-09-28T04:23:45.987 回答