[英]java.sq.SQLException: Column not found
I am receiving the following error: 我收到以下错误:
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.
But when I copy and paste the very select statement listed in the error into a mysql shell, I get the result, which is expected as the table track
has the column category_id
. 但是当我将错误中列出的非常选择的语句复制并粘贴到mysql shell中时,我得到了结果,这是预期的,因为表
track
列为category_id
。
What could be a reason for this error? 这个错误可能是什么原因?
Here is the table create statement for track
: 以下是
track
的表create语句:
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)
);
Here are some lines from my dao class regarding the track
table: 以下是我的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());
}
Check your SQL statement -- you need to include the category_id
in the column list: 检查您的SQL语句 - 您需要在列列表中包含
category_id
:
String sql = "SELECT id, name, category_id FROM track WHERE category_id = " + categoryId + " ORDER BY name";
It is failing because you're trying to extract category_id
from the ResultSet
and it isn't there. 它失败了,因为你试图从
ResultSet
提取category_id
并且它不在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.