如何使用Spring Persistence Framework调用Oracle函数或存储过程? [英] How to call Oracle function or stored procedure using spring persistence framework?

查看:93
本文介绍了如何使用Spring Persistence Framework调用Oracle函数或存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为项目使用Spring持久性框架. 我想从此框架调用oracle函数或存储过程.

I am using Spring persistence framework for my project. I want to call oracle function or stored procedure from this framework.

任何人都可以建议我如何实现这一目标.

Can anybody suggest how can I achieve this.

请同时提供* oracle函数和*存储过程的解决方案.

Please give solution for both * oracle function and *stored procedure.

谢谢.

推荐答案

假设您是指JdbcTemplate:

Assuming you are referring to JdbcTemplate:

jdbcTemplate.execute(
    new CallableStatementCreator() {
        public CallableStatement createCallableStatement(Connection con) throws SQLException{
            CallableStatement cs = con.prepareCall("{call MY_STORED_PROCEDURE(?, ?, ?)}");
            cs.setInt(1, ...); // first argument
            cs.setInt(2, ...); // second argument
            cs.setInt(3, ...); // third argument
            return cs;
        }
    },
    new CallableStatementCallback() {
        public Object doInCallableStatement(CallableStatement cs) throws SQLException{
            cs.execute();
            return null; // Whatever is returned here is returned from the jdbcTemplate.execute method
        }
    }
);

调用函数几乎是相同的:

Calling a function is almost identical:

jdbcTemplate.execute(
    new CallableStatementCreator() {
        public CallableStatement createCallableStatement(Connection con) {
            CallableStatement cs = con.prepareCall("{? = call MY_FUNCTION(?, ?, ?)}");
            cs.registerOutParameter(1, Types.INTEGER); // or whatever type your function returns.
            // Set your arguments
            cs.setInt(2, ...); // first argument
            cs.setInt(3, ...); // second argument
            cs.setInt(4, ...); // third argument
            return cs;
        }
    },
    new CallableStatementCallback {
        public Object doInCallableStatement(CallableStatement cs) {
            cs.execute();
            int result = cs.getInt(1);
            return result; // Whatever is returned here is returned from the jdbcTemplate.execute method
        }
    }
);

这篇关于如何使用Spring Persistence Framework调用Oracle函数或存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆