11

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.

What could be a reason for this error?

Here is the table create statement for 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)
);

Here are some lines from my dao class regarding the track table:

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());
}
3
  • It's likely the data type. Try resultSet.getLong instead of getInt Commented Sep 28, 2013 at 3:09
  • @Thomas it still doesn't work :/. Commented Sep 28, 2013 at 3:50
  • In my case, I had been specifying my params in single quotes, i.e. do NOT do this: "WHERE value = ':myvalue'" Commented Apr 13, 2020 at 15:43

1 Answer 1

34

Check your SQL statement -- you need to include the category_id in the column list:

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.

Sign up to request clarification or add additional context in comments.

1 Comment

when you miss selecting few columns in the SQL statement you can get this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.