日期:2014-05-16 浏览次数:20531 次
1: CREATE OR REPLACE PROCEDURE TESTPRO(PARAM1 IN VARCHAR2,PARAM2 IN VARCHAR2) AS 2: BEGIN 3: INSERT INTO TESTTABLE (ID,NAME) VALUES (PARAM1, PARAM2); 4: END TESTPRO; Java代码: 1: package com.dragon.test; 2: import org.springframework.jdbc.core.JdbcTemplate; 3: public class JdbcTemplateTest { 4: private JdbcTemplate jdbcTemplate; 5: public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 6: this.jdbcTemplate = jdbcTemplate; 7: } 8: public void test(){ 9: this.jdbcTemplate.execute("call testpro('p1','p2')"); 10: } 11: }
public void test() { String param2Value = (String) jdbcTemplate.execute( new CallableStatementCreator() { public CallableStatement createCallableStatement(Connection con) throws SQLException { String storedProc = "{call testpro(?,?)}";// 调用的sql CallableStatement cs = con.prepareCall(storedProc); cs.setString(1, "p1");// 设置输入参数的值 cs.registerOutParameter(2, OracleTypes.VARCHAR);// 注册输出参数的类型 return cs; } }, new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { cs.execute(); return cs.getString(2);// 获取输出参数的值 } }); }
CREATE OR REPLACE PROCEDURE TESTPRO(PARAM1 IN VARCHAR2,test_cursor out TESTPACKAGE.TEST_CURSOR) IS BEGIN OPEN test_cursor FOR SELECT * FROM TESTTABLE; END TESTPRO;
public void test() { List resultList = (List) jdbcTemplate.execute( new CallableStatementCreator() { public CallableStatement createCallableStatement(Connection con) throws SQLException { String storedProc = "{call testpro(?,?)}";// 调用的sql CallableStatement cs = con.prepareCall(storedProc); cs.setString(1, "p1");// 设置输入参数的值 cs.registerOutParameter(2, OracleTypes.CURSOR);// 注册输出参数的类型 return cs; } }, new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement cs) throws SQLException,DataAccessException { List resultsMap = new ArrayList(); cs.execute(); ResultSet rs = (ResultSet) cs.getObject(2);// 获取游标一行的值 while (rs.next()) {// 转换每行的返回值到Map中 Map rowMap = new HashMap(); rowMap.put("id", rs.getString("id")); rowMap.put("name", rs.getString("name")); resultsMap.add(rowMap); } rs.close(); return resultsMap; } }); for (int i = 0; i < resultList.size(); i++) { Map rowMap = (Map) resultList.get(i); String id = rowMap.get("id").toString(); String name = rowMap.get("nam