How to call Oracle function or stored procedure using spring persistence framework?
2012-11-22 16:52
991 查看
Assuming you are referring to JdbcTemplate:
Calling a function is almost identical:
转载地址:http://stackoverflow.com/questions/862694/how-to-call-oracle-function-or-stored-procedure-using-spring-persistence-framewo
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 } } );
There are a number of ways to call stored procedures in Spring. If you use CallableStatementCreator to declare parameters, you will be using Java's standard interface of CallableStatement, i.e register out parameters and set them separately. Using SqlParameter abstraction will make your code cleaner. I recommend you looking at SimpleJdbcCall. It may be used like this: SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate) .withSchemaName(schema) .withCatalogName(package) .withProcedureName(procedure)(); ... jdbcCall.addDeclaredParameter(new SqlParameter(paramName, OracleTypes.NUMBER)); ... jdbcCall.execute(callParams); For simple procedures you may use jdbcTemplate's update method: jdbcTemplate.update("call SOME_PROC (?, ?)", param1, param2);
转载地址:http://stackoverflow.com/questions/862694/how-to-call-oracle-function-or-stored-procedure-using-spring-persistence-framewo
相关文章推荐
- How to call an Oracle Stored Procedure that returns one or more REF CURSORS, using ADO from C++ How
- How To Call Stored Procedure In Hibernate
- How to create Function/Procedure in Oracle?
- How to call an external C function from within Oracle
- Spring jdbc call oralce procedure or function
- How to Use a Function or a Procedure as a Parameter in another Function
- How-To : Using EJB 3.0 and Java Persistence API with Spring in OC4J
- How to Simulate the Price Order or Price Line Function using API QP_PREQ_PUB.PRICE_REQUEST Includes
- How to Simulate the Price Order or Price Line Function using API QP_PREQ_PUB.PRICE_REQUEST Includes
- How to Simulate the Price Order or Price Line Function using API QP_PREQ_PUB.PRICE_REQUEST Includes
- How to call stored procedure in Hibernate
- How to Simulate the Price Order or Price Line Function using API QP_PREQ_PUB.PRICE_REQUEST Includes
- How to call stored procedure in Hibernate
- illustrates how to call a SQL Server stored procedure
- How to troubleshoot 'Procedure or function has too many arguments specified' in asp.net 2.0
- How To Use a DataReader Against an Oracle Stored Procedure in Visual C# .NET
- [ASP.NET2.0][数据库更新]How to troubleshoot 'Procedure or function has too many arguments specified' in asp.net 2.0
- Creating a Stored Procedure or Function in an Oracle Database
- How to use Oracle 11g ODP.NET UDT in an Oracle Stored Procedure's Where clause
- How to execute a Stored Procedure with Entity Framework Code First