Java SpringJDBC模板。如何获取pl/sql脚本的结果变量

Java SpringJDBC模板。如何获取pl/sql脚本的结果变量,java,plsql,spring-jdbc,Java,Plsql,Spring Jdbc,我正在使用NamedParameterJdbcTemplate来运行pl/sql脚本。 但我不知道怎样才能得到这些值​​out变量的数量:id\u out。 提前谢谢 String script = "declare begin if myFunc(:id_in) is null then :id_out := 0; e

我正在使用NamedParameterJdbcTemplate来运行pl/sql脚本。 但我不知道怎样才能得到这些值​​out变量的数量:id\u 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;
                }
       }
       );

我不相信您可以将NamedParameterJdbcTemplate或JdbcTemplate的任何其他子类与上述匿名PL/SQL块一起使用。您必须将匿名PL/SQL块包装到存储过程或函数中


Spring旨在跨数据库进行移植。据我所知,MySQL和SQL Server都没有类似于Oracle的匿名PL/SQL块的概念,但我很高兴在这一点上被证明是错误的。由于此功能不能跨数据库移植,Spring无法仅为Oracle提供支持。

我不相信您可以将NamedParameterJdbcTemplate或JdbcTemplate的任何其他子类与上述匿名PL/SQL块一起使用。您必须将匿名PL/SQL块包装到存储过程或函数中


Spring旨在跨数据库进行移植。据我所知,MySQL和SQL Server都没有类似于Oracle的匿名PL/SQL块的概念,但我很高兴在这一点上被证明是错误的。由于此功能不能跨数据库移植,Spring无法仅为Oracle真正支持它。

可以使用普通的JdbcTemplate完成此功能,如以下示例所示,但请注意必须在匿名plsql块中使用?:=”指定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;
注意上面的两个问号-第一个实际上是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);
            }
        });

可以使用普通的JdbcTemplate完成,如以下示例所示,但请注意,必须在匿名plsql块中使用?:=”指定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;
注意上面的两个问号-第一个实际上是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);
            }
        });

你想要什么?您的SQL不是一个查询,即不返回任何内容,但您确实返回null,所以……?我需要访问这些值​​变量的类型,例如id_,您想要什么?您的SQL不是一个查询,即不返回任何内容,但您确实返回null,所以……?我需要访问这些值​​对于变量,例如id_out,您还可以在CallableStatement中使用命名参数-cstmt.registerOutParameterid_out,Types.NUMERIC;如何提取id类型的参数?列表是tablename1.id%类型的表?您还可以在CallableStatement-cstmt.registerOutParameterid\u out,Types.NUMERIC中使用命名参数;如何提取类型为id_list的参数是tablename1.id%类型的表?SQL Server可以通过JDBC执行块。有关模拟ALTER TABLE选项卡MODIFY col DEFAULT val语句时执行的示例块,请参见SQL Server可以通过JDBC执行块。有关模拟ALTER TABLE tab MODIFY col DEFAULT val语句时执行的示例块,请参见