解決java.sql.SQLException:索引中丟失 IN或OUT 參數(shù)::x問題
java.sql.SQLException:索引中丟失 IN或OUT 參數(shù)::x
使用JDBC時,會有這么一個錯誤:java.sql.SQLException: 索引中丟失 IN或OUT 參數(shù)::x
如下:
示例中insertLog.execute();這行會拋出這個異常:
String logSQL = "insert into tbl_obj(id, obj, type, cont, proposer, operator, date, remark) " + "values(seq_tot.nextval, ?, ?, ?, ?, ?, SYSDATE, ?)"; insertLog = conn.prepareStatement(logSQL); insertLog.setString(2, trace.getObj()); insertLog.setString(3, trace.getType()); insertLog.setString(4, trace.getCont()); insertLog.setString(5, trace.getProposer()); insertLog.setString(6, trace.getOperator()); insertLog.setString(8, trace.getRemark()); insertLog.execute();
檢索了一些帖子,對于這種問題,指出的原因很多
- 全角半角引起;
- 參數(shù)過多;
- 配置文件和數(shù)據庫字段類型不一致;
- 或是數(shù)據庫的索引問題等”
根據錯誤提示,歸結為兩點
(1) 索引是否有問題?(“索引中丟失”)
(2) 字段賦值是否與數(shù)據庫字段類型匹配?
- 對于(1)的論證:
查看這張表的索引,這張表是以ID作為主鍵,沒有其他索引,因此只有一個主鍵索引,查看狀態(tài)也是VALID的,沒有錯誤:
SQL> select index_name, status from user_indexes where table_name='TBL_OBJ_TRACE'; INDEX_NAME STATUS ------------------------------ -------- SYS_C0031302 VALID
- 對于(2)的論證:
首先看了trace的set/get方法中對字段類型的定義,都是String,對應庫中的字段類型都是VARCHAR2,沒有差別。
其次再看setString,和VALUES中字段是對應的啊。
其實問題就出在這了,看下setString方法的解釋:
void java.sql.PreparedStatement.setString(int parameterIndex, String x) throws SQLException
Sets the designated parameter to the given Java String value. The driver converts this to an SQL VARCHAR or LONGVARCHAR value (depending on the argument's size relative to the driver's limits on VARCHAR values) when it sends it to the database.
Parameters:
parameterIndex the first parameter is 1, the second is 2, ...
x the parameter value
Throws:
SQLException - if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closed PreparedStatement
可以看到第一個參數(shù)parameterIndex,參數(shù)索引,parameterIndex does not correspond to a parameter marker in the SQL statement(如果沒有對應到SQL語句中的參數(shù)標識符),則會拋出SQLException異常。
SQL語句中values(seq_tot.nextval, ?, ?, ?, ?, ?, SYSDATE, ?)的參數(shù)標識符一共6個,setString同樣是6個,但順序不對,setString中第一個參數(shù)的索引序號是要和SQL語句中是一致的,并不是SQL語句中這里VALUES字段的位置,而應該是SQL語句VALUES中參數(shù)標識符的序號。
改為如下格式就對了:
String logSQL = "insert into tbl_obj(id, obj, type, cont, proposer, operator, date, remark) " + "values(seq_tot.nextval, ?, ?, ?, ?, ?, SYSDATE, ?)"; insertLog = conn.prepareStatement(logSQL); insertLog.setString(1, trace.getObj()); insertLog.setString(2, trace.getType()); insertLog.setString(3, trace.getCont()); insertLog.setString(4, trace.getProposer()); insertLog.setString(5, trace.getOperator()); insertLog.setString(6, trace.getRemark()); insertLog.execute();
總結
JDBC的這個報錯,提示信息很晦澀,但這個錯誤感覺是屬于那種碰過一次之后,基本下次就能知道錯誤的范圍,排查起來應該也比較順暢了。
例如:
索引是否有問題、代碼中的字段類型和表中字段類型是否一致、代碼中使用的參數(shù)索引和SQL語句中的參數(shù)標識符是否一致(個數(shù)、順序等)。
好了,以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- java.sql.SQLException:?connection?holder?is?null錯誤解決辦法
- Java中java.sql.SQLException異常的正確解決方法(親測有效!)
- java報錯Cause: java.sql.SQLException問題解決
- java.sql.SQLException:com.mysql.cj.jdbc.Driver報錯問題解決
- java.sql.SQLException問題解決以及注意事項
- 解決java.sql.SQLException:The?server?time?zone?value?'?D1ú±ê×?ê±??'?is?unrecognized問題