简体   繁体   中英

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?

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)."

Even if SQL is a standard, no database implemented it fully. To avoid such errors it's better to use sheak_case in tables and columns naming.

In your case seance_number should also work.

I had the same issue, but in my case i had a space left, when getting the column in my mapper. In your example it would be like rs.getInt("seanceNumber ") which caused the same error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM