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

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

Invalid SQL statement with CallableStatement during Stored proc call

问题

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

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

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

What am i doing wrong?

答案1

得分: 2

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

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

Can you just write call statement when calling your procedure

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

答案2

得分: 1

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

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

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:

确定