gpt4 book ai didi

java - MySQL Java语法错误

转载 作者:行者123 更新时间:2023-11-29 05:13:01 24 4
gpt4 key购买 nike

试图找出此 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());
}
}

任何帮助将不胜感激,似乎无法解决这个问题。

最佳答案

用反引号括起顺序(表名)如下所示:

INSERT INTO `order` (item_quantity, customer_id, product_id) VALUES...

注意:

例如,反引号可以帮助您避免意外使用在 SQL 中作为保留字的名称。拿一个名为“where”的表,我同意这是一个愚蠢的表名,但如果你用反引号将它包起来它会工作正常

关于java - MySQL Java语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36880333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com