I tried to add a record do MariaDB database use Sql2o with IntelliJ
All fields have type String
and in Database have type varchar
.
But I got the error below (java.sql.SQLSyntaxErrorException)
Error in executeUpdate, You have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use near 'desc, manufacturer, category,
condition) values ('4082304923748','iPhone X','...' at line 1
public static void add(Product product, HttpServletRequest request) {
final String sql = "insert into product (item_code, name, unit_price, units_in_stock, desc, manufacturer, category, condition) " +
"values (:item_code,:name,:unit_price,:units_in_stock,:desc,:manufacturer,:category,:condition)\n";
try (Connection con = sql2o.open()) {
//con.createQuery(sql).bind(product).executeUpdate();
con.createQuery(sql).addParameter("item_code", product.getItem_code())
.addParameter("name", product.getName())
.addParameter("unit_price", product.getUnit_price())
.addParameter("units_in_stock", product.getUnits_in_stock())
.addParameter("desc", product.getDesc())
.addParameter("manufacturer", product.getManufacturer())
.addParameter("category", product.getCategory())
.addParameter("condition", product.getCondition())
.executeUpdate();
}
}
DESC
andCONDITION
is a reserved keyword, can't use them as a column namefor more