插入INTO查询不适用于一种方法,但适用于另一种方法

时间:2013-01-03 13:12:14

标签: java database ms-access

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

// 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

2 个答案:

答案 0 :(得分: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() ;

答案 1 :(得分:2)

试试这个:

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

让我知道它是否有效