Skip to content
Advertisement

SQLException: Column not found

My sql query should take all the fields from the table and transfer them to the FTL page using the UserMapper template. The template indicates which object to create and to which fields of the object which table columns to match.

In summary: all the fields work correctly, but the seanceNumber field gives the error “Column ‘seanceNumber’ not found”.

This is strange, because column is correct, everywhere data type is int.

Table:

(
  id           int auto_increment,
  name         varchar(255) null,
  email        varchar(255) null,
  seance       varchar(255) null,
  seanceNumber int          not null,
  seat         int          null,

  constraint client_id_uindex
  unique (id)
);

FTL:

<#list clientsList as client>

<tr>
    <td><a href="/client/${client.id}">${client.id}</a></td>
    <td>${client.name}</td>
    <td>${client.email}</td>
    <td>${client.seance}</td>
    <td>${client.seanceNumber}</td>
    <td>${client.seatNumber}</td>
</tr>

SQL:

    public List<Client> getAll() {
    String sql = "SELECT * FROM client";
    return jdbcTemplate.query(sql, new UserMapper());
}

UserMapper:

    public Client mapRow(ResultSet rs, int rowNum) throws SQLException {
    Client client = new Client();
    client.setId(rs.getInt("id"));
    client.setName(rs.getString("name"));
    client.setEmail(rs.getString("email"));
    client.setSeance(rs.getString("seance"));
    client.setSeanceNumber(rs.getInt("seanceNumber"));
    client.setSeatNumber(rs.getInt("seat"));


    return client;
}

Result:

Message Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException for SQL [SELECT * FROM client]; SQL state [S0022]; error code [0]; Column ‘seanceNumber’ not found.; nested exception is java.sql.SQLException: Column ‘seanceNumber’ not found.

Checked for typos too. What am I doing wrong?

Advertisement

Answer

The problem was solved by renaming the column ‘seanceNumber’ to ‘seancenumber’ in the table.

“Column names in SQL are usually case-insensitive – so it doesn’t matter whether you ask for seance or SEANCE or SeAnCe. However, if you put the column names in double quotes, they become case sensitive – I guess your UserMapper did exactly that and asked for a column named “seanceNumber”, and the database couldn’t find it (since it’s called seancenumber or SEANCENUMBER in the database).”

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement