Java調用Oracle存儲過程詳解。本站提示廣大學習愛好者:(Java調用Oracle存儲過程詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Java調用Oracle存儲過程詳解正文
Java調用Oracle存儲過程詳解
步驟:
1、編寫Oracle存儲過程
2、編寫數據庫獲取連接工具類
3、編寫簡單應用調用存儲過程
實現:
1、Oracle存儲過程:
/*測試表*/ create table test( id varchar2(32), name varchar2(32) ); /*存儲過程 插入數據*/ CREATE OR REPLACE PROCEDURE insert_procedure( PARA1 IN VARCHAR2, PARA2 IN VARCHAR2 ) AS BEGIN INSERT INTO test (id, name) VALUES (PARA1, PARA2); END insert_procedure; /*存儲過程 返回結果集*/ CREATE OR REPLACE PROCEDURE select_procedure( para_id IN VARCHAR2, name OUT sys_refcursor /* 這個sys_refcursor類型在SYS.STANDARD包中 */ ) AS BEGIN OPEN name FOR SELECT * FROM test WHERE id = para_id; END;
2、JDBC工具類
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String URL = "jdbc:oracle:thin:@localhost:1521/orcl"; public static final String USERNAME = "pfm"; public static final String PASSWORD = "pfm"; /** * 通過靜態代碼塊 注冊數據庫驅動 */ static { try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 獲得Connection * * @return */ public static Connection getConnection() { Connection conn = null; try { conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { e.printStackTrace(); } return conn; } /** * 獲得Statement * * @return */ public static Statement getStatement() { Statement st = null; try { st = getConnection().createStatement(); } catch (SQLException e) { e.printStackTrace(); } return st; } /** * 關閉ResultSet * * @param rs */ public static void closeResultSet(ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 關閉Statement * * @param st */ public static void closeStatement(Statement st) { if (st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 關閉Connection * * @param conn */ public static void closeConnection(Connection conn) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 關閉全部 * * @param rs * @param sta * @param conn */ public static void closeAll(ResultSet rs, Statement sta, Connection conn) { closeResultSet(rs); closeStatement(sta); closeConnection(conn); } }
3、調用存儲過程:
import java.sql.CallableStatement; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import oracle.jdbc.driver.OracleTypes; /** * 測試調用存儲過程 * */ public class StoredTest { public static void main(String[] args) { insert_call(); //select_call(); } /** * 執行存儲過程 插入數據 */ public static void insert_call() { Connection conn = DBUtil.getConnection(); PreparedStatement pst = null; CallableStatement proc = null; // 創建執行存儲過程的對象 try { proc = conn.prepareCall("{ call insert_procedure(?,?) }"); proc.setString(1, "1"); // 設置第一個輸入參數 proc.setString(2, "hello call"); // 設置第一個輸入參數 proc.execute();// 執行 } catch (SQLException e) { e.printStackTrace(); } finally { try { // 關閉IO流 proc.close(); DBUtil.closeAll(null, pst, conn); } catch (Exception e) { e.printStackTrace(); } } } /** * 執行存儲過程 查詢數據 */ public static void select_call() { Connection conn = DBUtil.getConnection(); CallableStatement stmt; try { stmt = conn.prepareCall("{ call select_procedure(?, ?) }"); // 用此調用方法不能實現多行語法 stmt.setString(1, "1"); stmt.registerOutParameter(2, OracleTypes.CURSOR); stmt.execute(); ResultSet rs = (ResultSet) stmt.getObject(2); while (rs.next()) { System.out.println(rs.getString("name")); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.closeConnection(conn); } } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!