【问题标题】:java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0-java.sql.SQLException:参数索引超出范围(1>参数个数,即0-
【发布时间】:2018-03-26 09:48:20
【问题描述】:
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();

【问题讨论】:

  • pst.executeUpdate(query);更改为pst.executeUpdate();
  • 你能分享正确的代码和你面临的问题吗?
  • 伙计们,我在堆栈溢出中没有得到任何正确的答案来解决这个错误。我需要摆脱这个错误。需要帮助
  • 没有帮助的Eran
  • @Eran con.prepareStatement("query"); 看起来也不对,不应该是con.prepareStatement(query);吗?

标签: java mysql jdbc


【解决方案1】:

改变

PreparedStatement pst = (PreparedStatement) con.prepareStatement("query");

PreparedStatement pst = (PreparedStatement) con.prepareStatement(query);

请注意,您需要传递实际的 query 变量而不是“查询”字符串

正如马克在下面指出的那样: 另外pst.executeUpdate(query)需要改成pst.executeUpdate()

【讨论】:

  • 另外pst.executeUpdate(query)需要改成pst.executeUpdate()
【解决方案2】:

尝试指定要插入的列名

    String query = "INSERT INTO tablename (col1, col2, col3) VALUES (?, ?, ?)";
     preparedStatement = connection.prepareStatement(query);

【讨论】:

    猜你喜欢
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多