您好,登錄后才能下訂單哦!
這篇“spring中JdbcTemplate操作oracle的存儲過程是什么”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“spring中JdbcTemplate操作oracle的存儲過程是什么”文章吧。
使用java代碼調用oracle的存儲過程,本例使用JdbcTemplate模板類操作.
方便后續查閱.
CREATE OR REPLACE PROCEDURE PRO_QUERY_INFO_ARGS4(TASK_ID IN NUMBER) IS BEGIN INSERT INTO F_LOG_INFO (TASK_ID, BEGIN_TIME, END_TIME, FLAG, FAIL_INFO, DATA_COUNT, TABLE_NAME) VALUES (TASK_ID, SYSDATE - 1, SYSDATE, '999', '999', 999, 'TABLE_NAME2019'); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; END PRO_QUERY_INFO_ARGS4;
public static void doProcedures() { String procedures = "{call PRO_QUERY_INFO_ARGS4 ('888')}"; jdbcTemplate.execute(procedures); }
CREATE OR REPLACE PROCEDURE PRO_QUERY_INFO_ARGS3(ARGS IN VARCHAR2, RTNINFO OUT VARCHAR2, ERRORMSG OUT VARCHAR2, FAILINFO OUT VARCHAR2) IS BEGIN ERRORMSG := ''; RTNINFO := '你輸入的ARGS=' || ARGS; SELECT FAIL_INFO INTO FAILINFO FROM F_LOG_INFO where TASK_ID = 1; COMMIT; EXCEPTION WHEN OTHERS THEN ERRORMSG := 'PRO_QUERY_INFO_ARG拋出異常: ' || SQLERRM; END PRO_QUERY_INFO_ARGS3;
public static void getProceduresResult() { String tt2 = (String) jdbcTemplate.execute( new CallableStatementCreator() { public CallableStatement createCallableStatement( Connection con) throws SQLException { String procedures = "{call PRO_QUERY_INFO_ARGS3 (?,?,?,?)}"; CallableStatement cs = con.prepareCall(procedures); /** 設置輸入參數的值 */ cs.setString(1, "代碼調用"); /** 注冊輸出參數的類型-此處集合為oracle的VARCHAR2 */ cs.registerOutParameter(2, OracleTypes.VARCHAR); cs.registerOutParameter(3, OracleTypes.VARCHAR); cs.registerOutParameter(4, OracleTypes.VARCHAR); return cs; } }, new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement st) throws SQLException, DataAccessException { st.execute(); /** 依次獲取存儲過程參數值,按照順序存儲過程定義參數的順序獲取 */ Object tt2 = st.getObject(2); Object tt3 = st.getObject(3); Object tt4 = st.getObject(4); return tt2; } }); }
CREATE OR REPLACE PROCEDURE PRO_QUERY_INFO_ARGS2(ERRORMSG OUT VARCHAR2, CURINFO OUT SYS_REFCURSOR) IS BEGIN ERRORMSG := ''; OPEN CURINFO FOR SELECT FAIL_INFO, TABLE_NAME FROM F_LOG_INFO; COMMIT; EXCEPTION WHEN OTHERS THEN ERRORMSG := 'PRO_QUERY_INFO_ARG2拋出異常: ' || SQLERRM; END PRO_QUERY_INFO_ARGS2;
public static List getProceduresResultList() { List resultList = (List) jdbcTemplate.execute( new CallableStatementCreator() { public CallableStatement createCallableStatement( Connection conn) throws SQLException { /** 調用指定存儲過程 */ String procedures = "{ CALL PRO_QUERY_INFO_ARGS2(?,?) }"; CallableStatement statement = conn .prepareCall(procedures); /** 注冊輸出參數的類型-此處集合為oracle的VARCHAR2 */ statement.registerOutParameter(1, OracleTypes.VARCHAR); /** 注冊輸出參數的類型-此處集合為oracle的游標類型 */ statement.registerOutParameter(2, OracleTypes.CURSOR); return statement; } }, new CallableStatementCallback() { public Object doInCallableStatement( CallableStatement statement) throws SQLException, DataAccessException { List resultsMap = new ArrayList(); statement.execute(); /** 獲取游標結果集-此處2是存儲過程參數順序 */ ResultSet resultSet = (ResultSet) statement .getObject(2); /** 轉換每行的返回值到Map中 */ while (resultSet.next()) { Map rowMap = new HashMap(); rowMap.put("FAIL_INFO", resultSet.getObject("FAIL_INFO")); rowMap.put("TABLE_NAME", resultSet.getObject("TABLE_NAME")); resultsMap.add(rowMap); } resultSet.close(); return resultsMap; } }); return resultList; }
create table F_LOG_INFO ( task_id NUMBER(16) not null, begin_time DATE, end_time DATE, flag VARCHAR2(8), fail_info VARCHAR2(512), data_count NUMBER(16), table_name VARCHAR2(256) ); alter table F_LOG_INFO add constraint PK_F_LOG_INFO primary key (TASK_ID);
public class TestProcedures { public static JdbcTemplate jdbcTemplate = getJdbcTemplate(); public static void main(String[] args) { System.out.println("測試開始......"); // getProceduresResult(); doProcedures(); List result = getProceduresResultList(); for (int i = 0; i < result.size(); i++) { Map rowMap = (Map) result.get(i); String id = rowMap.get("FAIL_INFO").toString(); String name = rowMap.get("TABLE_NAME").toString(); System.out.println("FAIL_INFO=" + id + ";TABLE_NAME=" + name); } System.out.println("測試結束......"); } /** * 執行存儲過程無返回值 * */ public static void doProcedures() { String procedures = "{call PRO_QUERY_INFO_ARGS4 ('888')}"; jdbcTemplate.execute(procedures); } /** * 調用存儲過程-返回值是非集合 * */ public static void getProceduresResult() { String tt2 = (String) jdbcTemplate.execute( new CallableStatementCreator() { public CallableStatement createCallableStatement( Connection con) throws SQLException { String procedures = "{call PRO_QUERY_INFO_ARGS3 (?,?,?,?)}"; CallableStatement cs = con.prepareCall(procedures); /** 設置輸入參數的值 */ cs.setString(1, "代碼調用"); /** 注冊輸出參數的類型-此處集合為oracle的VARCHAR2 */ cs.registerOutParameter(2, OracleTypes.VARCHAR); cs.registerOutParameter(3, OracleTypes.VARCHAR); cs.registerOutParameter(4, OracleTypes.VARCHAR); return cs; } }, new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement st) throws SQLException, DataAccessException { st.execute(); /** 依次獲取存儲過程參數值,按照順序存儲過程定義參數的順序獲取 */ Object tt2 = st.getObject(2); Object tt3 = st.getObject(3); Object tt4 = st.getObject(4); return tt2; } }); } /** * 調用存儲過程-返回值是List集合 * */ public static List getProceduresResultList() { List resultList = (List) jdbcTemplate.execute( new CallableStatementCreator() { public CallableStatement createCallableStatement( Connection conn) throws SQLException { /** 調用指定存儲過程 */ String procedures = "{ CALL PRO_QUERY_INFO_ARGS2(?,?) }"; CallableStatement statement = conn .prepareCall(procedures); /** 注冊輸出參數的類型-此處集合為oracle的VARCHAR2 */ statement.registerOutParameter(1, OracleTypes.VARCHAR); /** 注冊輸出參數的類型-此處集合為oracle的游標類型 */ statement.registerOutParameter(2, OracleTypes.CURSOR); return statement; } }, new CallableStatementCallback() { public Object doInCallableStatement( CallableStatement statement) throws SQLException, DataAccessException { List resultsMap = new ArrayList(); statement.execute(); /** 獲取游標結果集-此處2是存儲過程參數順序 */ ResultSet resultSet = (ResultSet) statement .getObject(2); /** 轉換每行的返回值到Map中 */ while (resultSet.next()) { Map rowMap = new HashMap(); rowMap.put("FAIL_INFO", resultSet.getObject("FAIL_INFO")); rowMap.put("TABLE_NAME", resultSet.getObject("TABLE_NAME")); resultsMap.add(rowMap); } resultSet.close(); return resultsMap; } }); return resultList; } /** 獲取JdbcTemplate數據源 */ public static JdbcTemplate getJdbcTemplate() { DruidDataSource dataSource = new DruidDataSource(); /**數據庫連接信息*/ String username = "demodb"; String password = "123456"; String jdbcUrl = "jdbc:oracle:thin:@127.0.0.1:1521/orcl"; String driverName = "oracle.jdbc.OracleDriver"; /** 設置數據源屬性參數 */ dataSource.setPassword(password); dataSource.setUrl(jdbcUrl); dataSource.setUsername(username); dataSource.setDriverClassName(driverName); /** 獲取spring的JdbcTemplate*/ JdbcTemplate jdbcTemplate = new JdbcTemplate(); /** 設置數據源 */ jdbcTemplate.setDataSource(dataSource); return jdbcTemplate; } }
以上就是關于“spring中JdbcTemplate操作oracle的存儲過程是什么”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。