简体   繁体   English

MySQL Java语法错误

[英]MySQL Java syntax error

Trying to figure out what the error is in this java code. 试图弄清楚这个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)'

The order table looks like 订单表看起来像

order_id int pk ai <br>
item_quantity <br>
customer_id int <br>
product_id int <br>

And the function that inserts is: 而且插入的功能是:

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());
    }
}

Any help would be greatly appreciated, can't seem to figure this out. 任何帮助将不胜感激,似乎无法弄清楚这一点。

Enclose the order (table name) by backtick like below: 用反引号括起订单 (表名),如下所示:

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

Note: 注意:

The backticks help you from accidentally using a name that is a reserved word in SQL for example. 反引号可以帮助您意外地使用SQL中的保留字名称。 Take a table named "where", it's a stupid name for a table I agree, but if you wrap it in backticks it will work fine 拿一个名为“where”的表,这是我同意的表的愚蠢名称,但是如果你用反引号包装它会很好的工作

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

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