Spring JDBC模板.如何获取pl/sql脚本的结果变量

use*_*818 1 java plsql spring-jdbc

我正在使用NamedParameterJdbcTemplate运行pl/sql脚本.但我不知道如何获得out变量的值(:id_out).提前致谢.

String script = "declare 
                   begin 
                     if myFunc(:id_in) is null then 
                        :id_out := 0; 
                     else 
                        :id_out := 1; 
                     end if; 
                   end;";
Map<String,Object> bindVars = new HashMap<String, Object>();
bindVars.put(id_in,1);
bindVars.put(id_out,2);


jdbcTmpl.execute(script, bindVars, new PreparedStatementCallback<Object>() {
    @Override public Object doInPreparedStatement(PreparedStatement cs)
        throws SQLException, DataAccessException {
        cs.execute();
        return null;
                }
       }
       );
Run Code Online (Sandbox Code Playgroud)

Noc*_*que 5

它可以使用普通的JdbcTemplate来完成,如下例所示,但请注意必须在匿名plsql块中使用"?:='something'指定OUT值的方式;

下面使用的java参数:String id ="12345"和String fixSql =

    declare
        p_id VARCHAR2(20) := null; 
        p_status_message VARCHAR2(32767) := null;
    begin
        p_id := ?;
        p_status_message := ' Everything is possible: ' || p_id;
        ? := 'Return text.' || p_status_message;
    end;
Run Code Online (Sandbox Code Playgroud)

注意上面的两个问号 - 第一个是有效的IN参数,第二个是OUT参数.此代码将调用它:

    public class Foo extends JdbcDaoSupport {
    ...
    public String doAnonymousPlSql(final String id, String fixSql) throws CustomerFixException {
        String resultValue = getJdbcTemplate().execute(new CallableStatementCreator() {
            @Override
            public CallableStatement createCallableStatement(Connection connection) throws SQLException {
                CallableStatement sql = connection.prepareCall(fixSql);
                sql.setString(1, id);
                sql.registerOutParameter(2, Types.VARCHAR);
                return sql;
            }
        }
        , new CallableStatementCallback<String>() {
            @Override
            public String doInCallableStatement(CallableStatement callablestatement) throws SQLException,
                    DataAccessException {
                callablestatement.executeUpdate();
                return (String) callablestatement.getObject(2);
            }
        });
Run Code Online (Sandbox Code Playgroud)