資源描述:
《java中調(diào)用oracle存儲(chǔ)過程》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、java中調(diào)用ORACLE存儲(chǔ)過程2007-06-2622:14一:無返回值的存儲(chǔ)過程存儲(chǔ)過程為:CREATEORREPLACEPROCEDURETESTA(PARA1INVARCHAR2,PARA2INVARCHAR2)??ASBEGIN???INSERTINTOHYQ.B_ID(I_ID,I_NAME)VALUES(PARA1,PARA2);ENDTESTA;然后呢,在java里調(diào)用時(shí)就用下面的代碼:packagecom.hyq.src;importjava.sql.*;importjava.sql.ResultSet;publicc
2、lassTestProcedureOne{??publicTestProcedureOne(){??}??publicstaticvoidmain(String[]args){????Stringdriver="oracle.jdbc.driver.OracleDriver";????StringstrUrl="jdbc:oracle:thin:@127.0.0.1:1521:hyq";????Statementstmt=null;????ResultSetrs=null;????Connectionconn=null;????Calla
3、bleStatementcstmt=null;????try{??????Class.forName(driver);??????conn=??DriverManager.getConnection(strUrl,"hyq","hyq");??????CallableStatementproc=null;??????proc=conn.prepareCall("{callHYQ.TESTA(?,?)}");??????proc.setString(1,"100");??????proc.setString(2,"TestOne");???
4、???proc.execute();????}????catch(SQLExceptionex2){??????ex2.printStackTrace();????}????catch(Exceptionex2){??????ex2.printStackTrace();????}????finally{??????try{????????if(rs!=null){??????????rs.close();??????????if(stmt!=null){????????????stmt.close();??????????}???????
5、???if(conn!=null){????????????conn.close();??????????}????????}??????}??????catch(SQLExceptionex1){??????}????}??}}當(dāng)然了,這就先要求要建張表TESTTB,里面兩個(gè)字段(I_ID,I_NAME)。二:有返回值的存儲(chǔ)過程(非列表)存儲(chǔ)過程為:CREATEORREPLACEPROCEDURETESTB(PARA1INVARCHAR2,PARA2OUTVARCHAR2)??ASBEGIN???SELECTINTOPARA2FROMT
6、ESTTBWHEREI_ID=PARA1;ENDTESTB;在java里調(diào)用時(shí)就用下面的代碼:packagecom.hyq.src;publicclassTestProcedureTWO{??publicTestProcedureTWO(){??}??publicstaticvoidmain(String[]args){????Stringdriver="oracle.jdbc.driver.OracleDriver";????StringstrUrl="jdbc:oracle:thin:@127.0.0.1:1521:hyq";????
7、Statementstmt=null;????ResultSetrs=null;????Connectionconn=null;????try{??????Class.forName(driver);??????conn=??DriverManager.getConnection(strUrl,"hyq","hyq");??????CallableStatementproc=null;??????proc=conn.prepareCall("{callHYQ.TESTB(?,?)}");??????proc.setString(1,"10
8、0");//設(shè)置輸入?yún)?shù)值??????proc.registerOutParameter(2,Types.VARCHAR);//第2個(gè)參數(shù)設(shè)為輸出??????proc.execute();??