試圖找出這個Java代碼中的錯誤。MySQL Java語法錯誤
The SQLException reads: " You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to
use near 'order (item_quantity, customer_id, product_id) VALUES (5, 191, 31)'
順序表看起來像
order_id int pk ai <br>
item_quantity <br>
customer_id int <br>
product_id int <br>
這接入功能是:
public void createOrder(int productQuantity, int customerId, int productId) throws SQLException {
sql = "INSERT INTO order (item_quantity, customer_id, product_id) VALUES (" + productQuantity + ", " + customerId + ", " + productId + ")";
try {
int a = stmt.executeUpdate(sql);
if (a == 1) {
System.out.println("Order Added");
} else {
System.out.println("Order Failed");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
任何幫助將不勝感激,似乎無法想出解決辦法。
請問你可以反請一下'訂單'嗎? – 1000111
[ORDER是一個保留字](http://dev.mysql.com/doc/refman/5.0/en/keywords.html)。寫'Order'而不是'order'。 – Satya
與此特定問題無關,但作爲_soon_,當您開始習慣使用這種SQL方式時,您應該開始查看PreparedStatements。它們爲您提供更高的安全性(如果您的'int'參數是字符串,我會立即想知道SQL注入攻擊)。 – yshavit