java.sql.SQLException:参数索引超出范围(1>参数个数,即0-

时间:2018-03-26 09:48:20

标签: java mysql jdbc

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();

2 个答案:

答案 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);