Reputation: 335
I have two method one is working and other's not working for insert query
// 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 !");
}
}
one method which is working fine mean it insert data into the table but the other is giving the following error:
[Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement
Upvotes: 1
Views: 154
Reputation: 18123
Try this:
INSERT INTO product ([productn],[quantity],[price],[date]) VALUES ('" + product + "','" + quantity + "','" + price + "','" + date + "')
And let me know if it works
Upvotes: 2
Reputation: 65
You'll have more chance to do it in this way, like that you could check better the integrity of the values.
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() ;
Upvotes: 2