資源描述:
《用Java調(diào)用Oracle存儲過程的示例代碼解析.doc》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫。
1、一:無返回值的存儲過程存儲過程為:1.CREATE?OR?REPLACE?PROCEDURE?TESTA(PARA1?IN?VARCHAR2,PARA2?IN?VARCHAR2)?AS?2.?3.BEGIN?4.?5.INSERT?INTO?HYQ.B_ID?(I_ID,I_NAME)?VALUES?(PARA1,?PARA2);?6.?7.END?TESTA;?然后呢,在java里調(diào)用時就用下面的代碼:1.package?com.hyq.src;?2.?3.import?java.sql.*;?4.?5.import?java.sql.ResultSet;?6.
2、?7.public?class?TestProcedureOne?{?8.?9.public?TestProcedureOne()?{?10.?11.}?12.?13.public?static?void?main(String[]?args?){?14.?15.String?driver?=?"oracle.jdbc.driver.OracleDriver";?16.?17.String?strUrl?=?"jdbc:oracle:thin:@127.0.0.1:1521:?hyq?";?18.?19.Statement?stmt?=?null;?20.?21.
3、ResultSet?rs?=?null;?22.?23.Connection?conn?=?null;?24.?25.CallableStatement?cstmt?=?null;?26.?27.try?{?28.?29.Class.forName(driver);?30.?31.conn?=?DriverManager.getConnection(strUrl,?"?hyq?",?"?hyq?");?1.?2.CallableStatement?proc?=?null;?3.?4.proc?=?conn.prepareCall("{?call?HYQ.TESTA
4、(?,?)?}");?5.?6.proc.setString(1,?"100");?7.?8.proc.setString(2,?"TestOne");?9.?10.proc.execute();?11.?12.}?13.?14.catch?(SQLException?ex2)?{?15.?16.ex2.printStackTrace();?17.?18.}?19.?20.catch?(Exception?ex2)?{?21.?22.ex2.printStackTrace();?23.?24.}?25.?26.finally{?27.?28.try?{?29.?3
5、0.if(rs?!=?null){?31.?32.rs.close();?33.?34.if(stmt!=null){?35.?36.stmt.close();?37.?38.}?39.?40.if(conn!=null){?41.?42.conn.close();?43.?44.}?1.?2.}?3.?4.}?5.?6.catch?(SQLException?ex1)?{?7.?8.}?9.?10.}?11.?12.}?當(dāng)然了,這就先要求要建張表TESTTB,里面兩個字段(I_ID,I_NAME)。二:有返回值的存儲過程(非列表)存儲過程為:1.CREATE?O
6、R?REPLACE?PROCEDURE?TESTB(PARA1?IN?VARCHAR2,PARA2?OUT?VARCHAR2)?AS?2.?3.BEGIN?4.?5.SELECT?INTO?PARA2?FROM?TESTTB?WHERE?I_ID=?PARA1;?6.?7.END?TESTB;?在java里調(diào)用時就用下面的代碼:1.package?com.hyq.src;?2.?3.public?class?TestProcedureTWO?{?4.?5.public?TestProcedureTWO()?{?6.?7.}?8.?9.public?static?v
7、oid?main(String[]?args?){?10.?11.String?driver?=?"oracle.jdbc.driver.OracleDriver";?12.?13.String?strUrl?=?"jdbc:oracle:thin:@127.0.0.1:1521:hyq";?14.?15.Statement?stmt?=?null;?16.?17.ResultSet?rs?=?null;?1.?2.Connection?conn?=?null;?3.?4.try?{?5.?6.Class.forName(driver);?7.?8.conn?=?
8、Drive