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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23470688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:14:27  来源:igfitidea点击:

java.sql.SQLException: Missing IN or OUT parameter at index, even when I provide correct number of parameter

javaoraclejdbcprepared-statement

提问by user2335123

I'm getting the error java.sql.SQLException: Missing IN or OUT parameter at index:: 3when I run the following code. I've checked the parameters, its only 2 and I used only 2 in the PreparedStament.

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

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

采纳答案by Mark Rotteveel

You have defined updateStringoutside of the loop, but in every loop you are concatenating in it, so in the first iteration it is fine (has two parameters), but in the second it has 4, then 6 and so on.

您已updateString在循环外定义,但在每个循环中您都在其中连接,因此在第一次迭代中它很好(有两个参数),但在第二次迭代中它有 4,然后是 6,依此类推。

You need to:

你需要:

  1. Define the query string (updateString) outside of the loop
  2. Prepare the query once (also outside of the loop)
  1. updateString在循环外定义查询字符串 ( )
  2. 准备一次查询(也在循环之外)

Preparing the query once also has a performance benefit, because the statement is not unprepared and reprepared on each iteration.

准备查询一次也有性能优势,因为语句不是未准备好的,而是在每次迭代时重新准备的。

回答by Jordi Reina

 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')) ";

Try to change id =for id in. It should solve your problem.

尝试更改id =id in. 它应该可以解决您的问题。

回答by Maheswaran Ravisankar

updateString = ""; 

You have to empty it before the next iteration.

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

Else, define it once, outside the loop and just reuse it inside loop. as @mark mentioned in comment!

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