[英]Java JDBC Error Insert into
這是代碼:
String sqlstatment = "INSERT INTO order (orderedBy, totalItems, totalPrice) "+
"VALUES (?, ?, ?);";
ResultSet keys = null;
try (
PreparedStatement stmt = conn.prepareStatement(sqlstatment, Statement.RETURN_GENERATED_KEYS);
){
stmt.setInt(1, 1);
stmt.setInt(2, 3);
stmt.setInt(3, 5);
int affected = stmt.executeUpdate();
if (affected == 1){
keys = stmt.getGeneratedKeys();
keys.next();
int newKey = keys.getInt(1);
orderBean.setOrderID(newKey);
}else{
System.out.println("An Error has ocurred while creating the Order.");
}
}catch (SQLException e){
System.err.println(e.getMessage());
}finally{
if (keys != null) keys.close();
}
當我運行代碼時,我收到此錯誤:
您的 SQL 語法有錯誤; 檢查與您的 MySQL 服務器版本相對應的手冊,了解在第 1 行的“order (orderedBy, totalItems, totalPrice) VALUES (1, 3, 5)”附近使用的正確語法
我不完全確定為什么我會收到錯誤,所以如果你知道那會很棒。
order是保留字,試試
String sqlstatment = "INSERT INTO \"order\" (orderedBy, totalItems, totalPrice) "+
"VALUES (?, ?, ?);";
您的查詢包含一個RESERVED KEYWORD order
作為您的表名。 MySQL 文檔明確建議應始終避免使用此類關鍵字,如果需要使用它們,則必須使用反引號,如下所示。
MySQL 中的某些對象,包括數據庫、表、索引、列、別名、視圖、存儲過程、分區、表空間和其他對象名稱,稱為標識符。
如果標識符包含特殊字符或者是保留字,則在引用它時必須引用它。
依次分配給String
查詢應更改為以下內容以解決此錯誤!
"INSERT INTO \"order\" (orderedBy, totalItems, totalPrice) VALUES (?, ?, ?);"
以下是 MySQL 保留關鍵字的文檔鏈接 ->文檔
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.