我收到以下错误:
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
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()); }
检查您的SQL语句-您需要category_id在列列表中包括:
String sql = "SELECT id, name, category_id FROM track WHERE category_id = " + categoryId + " ORDER BY name";
之所以失败,是因为您尝试从中提取内容category_id,ResultSet而该内容不存在。
ResultSet