簡體   English   中英

java.sql.SQLException:即使提供正確數量的參數,索引處的IN或OUT參數也丟失

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

我在運行以下代碼時遇到錯誤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();
}
updateString = ""; 

您必須在下一次迭代之前將其清空。

否則,在循環外定義一次,然后在循環內重復使用。 如評論中提到的@mark!

 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 =id in 它應該可以解決您的問題。

您已經在循環外部定義了updateString ,但是在每個循環中都將其串聯起來,因此在第一個迭代中它很好(有兩個參數),但是在第二個迭代中它有4個,然后是6個,依此類推。

你需要:

  1. 在循環外定義查詢字符串( updateString
  2. 一次准備查詢(也在循環之外)

一次准備查詢還具有性能優勢,因為該語句並非未准備好,而是在每次迭代時都准備好了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM