java.sql.SQLException:索引处缺少IN或OUT参数,即使我提供了正确数量的参数

时间:2014-05-05 10:51:00

标签: java oracle jdbc prepared-statement

运行以下代码时出现错误java.sql.SQLException: Missing IN or OUT parameter at index:: 3。我已经检查了参数,它只有2个而我在PreparedStament中仅使用了2个。

for (int i = 0; i < count; i++) {
    JsonObject projectObject = projectQueryResponse.getResults().get(i).getAsJsonObject();
    JsonObject obj = projectObject.getAsJsonObject();
    //System.out.println(obj);
    projectValue = getJsonValue(obj, "_refObjectName");
    System.out.println(projectValue);
    objectValue = getJsonValue(obj, "ObjectID");
    System.out.println(objectValue);

    //st.("INSERT INTO CUST_RALLY_PROJECT_OBJID Values('" + projectValue + "','" + objectValue + "')");

    updateString += "update odf_ca_other ";
    updateString += "set rallyid = ? ";
    updateString += "where id = (select inv.id from inv_investments inv, odf_ca_other oco where inv.id = oco.id and inv.odf_object_code = 'other' and inv.name = ? ";
    updateString += "AND ( oco.team_type = 'delivery_team' or oco.team_type = 'reg_team' or oco.team_type = 'ux_team' or oco.team_type = 'business_team')) ";

    PreparedStatement rs = conn.prepareStatement(updateString);
    rs.setString(1, objectValue);
    rs.setString(2, projectValue);
    rs.execute();
    conn.commit();
}

3 个答案:

答案 0 :(得分:3)

updateString = ""; 

你必须在下一次迭代之前清空它。

否则,在循环外定义一次,然后在循环内重用它。正如@mark在评论中提到的那样!

答案 1 :(得分:0)

 updateString += "update odf_ca_other ";
 updateString += "set rallyid = ? ";
 updateString += "where id IN (select inv.id from inv_investments inv, odf_ca_other oco where inv.id = oco.id and inv.odf_object_code = 'other' and inv.name = ? ";
 updateString += "AND ( oco.team_type = 'delivery_team' or oco.team_type = 'reg_team' or oco.team_type = 'ux_team' or oco.team_type = 'business_team')) ";

尝试更改id =的{​​{1}}。它应该可以解决你的问题。

答案 2 :(得分:0)

你已经在循环之外定义了updateString,但是在你连接的每个循环中,所以在第一次迭代中它很好(有两个参数),但在第二次它有4,然后6等等。

你需要:

  1. 在循环外定义查询字符串(updateString
  2. 准备一次查询(也在循环之外)
  3. 准备一次查询也有一个性能优势,因为该语句没有准备好,并且在每次迭代时都会重现。