1. 程式人生 > >Java 執行資料庫儲存過程,並帶返回值

Java 執行資料庫儲存過程,並帶返回值

前提是載入資源DataSource

private JdbcTemplate jdbcTemplate;

Java 呼叫儲存過程:

 @Override
    public String oneUniscInfoHisToDm(final String uniscInfoHisId) {  
        jdbcTemplate = new JdbcTemplate(getDataSource());
        String result = (String) jdbcTemplate.execute(   
             new CallableStatementCreator() {   
                public
CallableStatement createCallableStatement(Connection con) throws SQLException { String storedProc = "{call unisc_his_to_unisc_info_new (?,?)}";// 呼叫的sql CallableStatement cs = con.prepareCall(storedProc); cs.setString(1, uniscInfoHisId);// 設定輸入引數的值
cs.registerOutParameter(2,OracleTypes.INTEGER);// 註冊輸出引數的型別 return cs; } }, new CallableStatementCallback<Object>() { public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { cs.execute(); return
cs.getString(2);// 獲取輸出引數的值 } }); return result; }