1

我有两种方法,一种有效,另一种不适用于插入查询

// this one fails
public void product(String product, String quantity, String price,
        String date) throws SQLException {
    try {       
        statement.execute("INSERT INTO product (productn,quantity,price,date) VALUES ('" +       product + "','" + quantity + "','" + price + "','" + date + "')");
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

// this one works
public void customer(String name, String q, String p, String pro)
        throws SQLException {
    try {
        statement.execute("INSERT INTO Customer (name,price,product,quantity) VALUES ('" + name  + "','" + q + "','" + p + "','" + pro + "')");
    } catch (Exception e) {
        System.out.println("problem in Customer insert !");
    }
}

一种工作正常的方法意味着它将数据插入表中,但另一种方法给出以下错误:

[Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement
4

2 回答 2

2

您将有更多机会以这种方式进行操作,这样您就可以更好地检查值的完整性。

private static final String insert = "INSERT INTO product (productn,quantity,price,date) VALUES ('"+?+"','"+?+"','"+?+"','"+?+"')";



statement.clearParameters();

statement.set"Type_of_the_value"(1, productn) ;

statement.set"Type_of_the_value"(2, quantity) ;

statement.set"Type_of_the_value"(3, price) ;

statement.set"Type_of_the_value"(4, date) ;

statement.executeUpdate() ;
于 2013-01-03T13:24:22.333 回答
2

试试这个:

INSERT INTO product ([productn],[quantity],[price],[date]) VALUES ('" + product + "','" + quantity + "','" + price + "','" + date + "')

让我知道它是否有效

于 2013-01-03T13:33:03.873 回答