String productName = request.getParameter("productName");
int productPrice = Integer.parseInt(request.getParameter("productPrice"));
String query = " INSERT INTO PRODUCTS values(?,?,?)";
PreparedStatement pst = (PreparedStatement) con.prepareStatement("query");
int i = 1;
pst.setInt(1, i);
pst.setString(2, productName);
pst.setInt(3, productPrice);
i++;
pst.executeUpdate(query);
con.close();
答案 0 :(得分:3)
变化
PreparedStatement pst = (PreparedStatement) con.prepareStatement("query");
到
PreparedStatement pst = (PreparedStatement) con.prepareStatement(query);
请注意,您需要传递实际的query
变量而不是“查询”字符串
正如Mark所说:
此外,pst.executeUpdate(query)
需要更改为pst.executeUpdate()
。
答案 1 :(得分:0)
尝试指定要插入的列名称
String query = "INSERT INTO tablename (col1, col2, col3) VALUES (?, ?, ?)";
preparedStatement = connection.prepareStatement(query);