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

5w9g7ksd  于 2023-05-12  发布在  Java
关注(0)|答案(4)|浏览(92)

我使用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;
                }
       }
       );
gjmwrych

gjmwrych1#

它可以用一个普通的JdbcTemplate来完成,如下面的示例所示,但是请注意,必须在匿名plsql块中用“?:= 'something';
下面使用的java参数:字符串id=“12345”和字符串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;

请注意上面的两个问号-第一个实际上是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);
            }
        });
sdnqo3pr

sdnqo3pr2#

我不相信您可以将NamedParameterJdbcTemplate(或JdbcTemplate的任何其他子类)与上述匿名PL/SQL块一起使用。您必须将匿名PL/SQL块 Package 到存储过程或函数中。
Spring旨在跨数据库进行移植。据我所知,MySQL和SQL Server都没有类似于Oracle的匿名PL/SQL块的概念(尽管我很高兴在这一点上被证明是错误的)。由于这个特性不能跨数据库移植,Spring不能真正只支持Oracle。

lmvvr0a8

lmvvr0a83#

List<SqlParameter> params = new LinkedList<SqlParameter>();
params.add(new SqlParameter(Types.NUMERIC));
params.add(new SqlParameter(Types.NUMERIC));
params.add(new SqlOutParameter("out_param_1", Types.NUMERIC));
params.add(new SqlOutParameter("out_param_2", Types.NUMERIC));
Map<String, Object> ret = simpleJdbcTemplate.getJdbcOperations().call(
new CallableStatementCreator() {

    @Override
    public CallableStatement createCallableStatement(Connection con) throws SQLException {
        CallableStatement cs = con.prepareCall("DECLARE ..." + 
                                               "BEGIN ... " + 
                                               "delete from table where table_field_1 = ? and table_field_2 = ? " + 
                                               "returning out_param_1, out_param_2 into ?, ?; " + 
                                               "END;"
        cs.setInt(1, first_in_param_value);
        cs.setInt(2, second_in_param_value);
        cs.registerOutParameter(3, Types.NUMERIC);
        cs.registerOutParameter(4, Types.NUMERIC);
        return cs;
    }
}, params);

ret.get("out_param_1");
ret.get("out_param_1");
7vux5j2d

7vux5j2d4#

这在Spring Data中运行良好:

NamedParameterJdbcTemplate jdbcTemplate;
jdbcTemplate.update(
    "DECLARE ... BEGIN ... (PL/SQL Block with named binding parameters) END;",
    new MapSqlParameterSource
        .addValue("parameter1", value1)
        .addValue("parameter2", value2));

相关问题