简体   繁体   English

SQLException:未找到列

[英]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.我的 sql 查询应该从表中获取所有字段,并使用 UserMapper 模板将它们传输到 FTL 页面。 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".总之:所有字段都正常工作,但 seanceNumber 字段给出了错误“未找到列 'seanceNumber'”。

This is strange, because column is correct, everywhere data type is int.这很奇怪,因为列是正确的,所有数据类型都是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;嵌套异常是 org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException for SQL [SELECT * FROM client]; SQL [SELECT * FROM client] 的未分类 SQLException; SQL state [S0022]; SQL 状态 [S0022]; error code [0];错误代码 [0]; Column 'seanceNumber' not found.;未找到列 'seanceNumber'。 nested exception is java.sql.SQLException: Column 'seanceNumber' not found.嵌套异常是 java.sql.SQLException:未找到列“seanceNumber”。

Checked for typos too.也检查错别字。 What am I doing wrong?我究竟做错了什么?

The problem was solved by renaming the column 'seanceNumber' to 'seancenumber' in the table.通过将表中的“seanceNumber”列重命名为“seancenumber”解决了该问题。

"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)." “SQL 中的列名通常不区分大小写 - 因此无论您要求降神还是 SEANCE 或 SeAnCe。但是,如果您将列名放在双引号中,它们就会区分大小写 - 我猜您的 UserMapper 确实做到了并要求一个名为“seanceNumber”的列,但数据库找不到它(因为它在数据库中称为 seancenumber 或 SEANCENUMBER)。”

Even if SQL is a standard, no database implemented it fully.即使 SQL 是一个标准,也没有数据库完全实现它。 To avoid such errors it's better to use sheak_case in tables and columns naming.为避免此类错误,最好在表和列命名中使用 sheak_case。

In your case seance_number should also work.在你的情况下seance_number也应该工作。

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.在您的示例中,它就像 rs.getInt("seanceNumber ") 导致相同的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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