[英]SQLException: Column not found
我的 sql 查詢應該從表中獲取所有字段,並使用 UserMapper 模板將它們傳輸到 FTL 頁面。 模板指示要創建的對象以及要匹配對象的哪些字段的哪些表列。
總之:所有字段都正常工作,但 seanceNumber 字段給出了錯誤“未找到列 'seanceNumber'”。
這很奇怪,因為列是正確的,所有數據類型都是int。
桌子:
(
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)
);
超車:
<#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>
查詢語句:
public List<Client> getAll() {
String sql = "SELECT * FROM client";
return jdbcTemplate.query(sql, new 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;
}
結果:
消息請求處理失敗; 嵌套異常是 org.springframework.jdbc.UncategorizedSQLException: StatementCallback; SQL [SELECT * FROM client] 的未分類 SQLException; SQL 狀態 [S0022]; 錯誤代碼 [0]; 未找到列 'seanceNumber'。 嵌套異常是 java.sql.SQLException:未找到列“seanceNumber”。
也檢查錯別字。 我究竟做錯了什么?
通過將表中的“seanceNumber”列重命名為“seancenumber”解決了該問題。
“SQL 中的列名通常不區分大小寫 - 因此無論您要求降神還是 SEANCE 或 SeAnCe。但是,如果您將列名放在雙引號中,它們就會區分大小寫 - 我猜您的 UserMapper 確實做到了並要求一個名為“seanceNumber”的列,但數據庫找不到它(因為它在數據庫中稱為 seancenumber 或 SEANCENUMBER)。”
即使 SQL 是一個標准,也沒有數據庫完全實現它。 為避免此類錯誤,最好在表和列命名中使用 sheak_case。
在你的情況下seance_number
也應該工作。
我遇到了同樣的問題,但在我的情況下,在我的映射器中獲取列時,我有一個空間。 在您的示例中,它就像 rs.getInt("seanceNumber ") 導致相同的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.