无效的 SQL 语句,在调用存储过程时使用了 CallableStatement。

huangapple go评论109阅读模式
英文:

Invalid SQL statement with CallableStatement during Stored proc call

问题

  1. public void setContext(String userId, String someId, List<String> accountsList) {
  2. List<SqlParameter> parameters = Collections.singletonList(new SqlInOutParameter("output", Types.VARCHAR));
  3. log.info("executing stored procedure with user id: {}", userId);
  4. Map<String, Object> t = defaultTemplate.call(connection -> {
  5. CallableStatement callableStatement = connection.prepareCall("STORED_PROC(?, ?, ?, ?)");
  6. callableStatement.setString(1, userId);
  7. callableStatement.registerOutParameter(2, Types.VARCHAR);
  8. callableStatement.setString(3, someId);
  9. OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);
  10. Array array = oracleConnection.createOracleArray("SOME_TYPE", accountsList.toArray());
  11. callableStatement.setArray(4, array);
  12. callableStatement.execute();
  13. return callableStatement;
  14. }, parameters);
  15. log.info("Status of the stored procedure: {}", t.get("status_output"));
  16. }
英文:

I have below code to call a stored procedure with certain parameters.

However it fails on callableStatement.execute(); with the error:

java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement

  1. public void setContext(String userId, String someId, List&lt;String&gt; accountsList) {
  2. List&lt;SqlParameter&gt; parameters = Collections.singletonList(new SqlInOutParameter(&quot;output&quot;, Types.VARCHAR));
  3. log.info(&quot;executing stored procedure with user id: {}&quot;, userId);
  4. Map&lt;String, Object&gt; t = defaultTemplate.call(connection -&gt; {
  5. CallableStatement callableStatement = connection.prepareCall(&quot;STORED_PROC(?, ?, ?, ?)&quot;);
  6. callableStatement.setString(1, userId);
  7. callableStatement.registerOutParameter(2, Types.VARCHAR);
  8. callableStatement.setString(3, someId);
  9. OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);
  10. Array array = oracleConnection.createOracleArray(&quot;SOME_TYPE&quot;, accountsList.toArray());
  11. callableStatement.setArray(4, array);
  12. callableStatement.execute();
  13. return callableStatement;
  14. }, parameters);
  15. log.info(&quot;Status of the stored procedure: {}&quot;, t.get(&quot;status_output&quot;));
  16. }

What am i doing wrong?

答案1

得分: 2

当调用你的存储过程时,只需编写 call 语句:

  1. CallableStatement callableStatement = connection.prepareCall("{call STORED_PROC(?, ?, ?, ?)}");
英文:

Can you just write call statement when calling your procedure

  1. CallableStatement callableStatement = connection.prepareCall(&quot;{call STORED_PROC(?, ?, ?, ?)}&quot;);

答案2

得分: 1

  1. CallableStatement callableStatement = connection.prepareCall("{call STORED_PROC(?, ?, ?, ?)}");
英文:
  1. CallableStatement callableStatement = connection.prepareCall(&quot;STORED_PROC(?, ?, ?, ?)&quot;);

Put {} around the argument.And add call before the statement.Like below:

  1. CallableStatement callableStatement = connection.prepareCall(&quot;{call STORED_PROC(?, ?, ?, ?)}&quot;);

huangapple
  • 本文由 发表于 2020年4月7日 23:43:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61083932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定