資源描述:
《詳例實現(xiàn)java調(diào)用oracle存儲過程代碼》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在行業(yè)資料-天天文庫。
1、Oracle存儲過程調(diào)用8/8詳例實現(xiàn)java調(diào)用ORACLE的存儲過程代碼本篇文章主要是在java中調(diào)用ORACLE存儲過程,并把要用到的存儲過程和代碼列了出來,給大家做一個參考:一:無返回值的存儲過程存儲過程為:CREATEORREPLACEPROCEDURETESTA(PARA1INVARCHAR2,PARA2INVARCHAR2)ASBEGININSERTINTOHYQ.B_ID(I_ID,I_NAME)VALUES(PARA1,PARA2);ENDTESTA;然后呢,在java里調(diào)用時就用下面的代碼:packagecom.hyq.src;imp
2、ortjava.sql.*;importjava.sql.ResultSet;publicclassTestProcedureOne{publicTestProcedureOne(){}publicstaticvoidmain(String[]args){Stringdriver="oracle.jdbc.driver.OracleDriver";StringstrUrl="jdbc:oracle:thin:@127.0.0.1:1521:hyq";Statementstmt=null;ResultSetrs=null;Connectionconn=nu
3、ll;CallableStatementcstmt=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");proc.execute();}catch(SQLExceptionex2){ex2.pr
4、intStackTrace();}catch(Exceptionex2){ex2.printStackTrace();}finally{try{if(rs!=null){rs.close();if(stmt!=null){stmt.close();}if(conn!=null){conn.close();}}}catch(SQLExceptionex1){}}}}當然了,這就先要求要建張表TESTTB,里面兩個字段(I_ID,I_NAME)。二:有返回值的存儲過程(非列表)存儲過程為:CREATEORREPLACEPROCEDURETESTB(PARA1
5、INVARCHAR2,PARA2OUTVARCHAR2)AS8Oracle存儲過程調(diào)用8/8BEGINSELECTINTOPARA2FROMTESTTBWHEREI_ID=PARA1;ENDTESTB;在java里調(diào)用時就用下面的代碼:packagecom.hyq.src;publicclassTestProcedureTWO{publicTestProcedureTWO(){}publicstaticvoidmain(String[]args){Stringdriver="oracle.jdbc.driver.OracleDriver";Strings
6、trUrl="jdbc:oracle:thin:@127.0.0.1:1521:hyq";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,"100");pro
7、c.registerOutParameter(2,Types.VARCHAR);proc.execute();StringtestPrint=proc.getString(2);System.out.println("=testPrint=is="+testPrint);}catch(SQLExceptionex2){ex2.printStackTrace();}catch(Exceptionex2){ex2.printStackTrace();}finally{try{if(rs!=null){rs.close();if(stmt!=null){stm
8、t.close();}if(conn!=null){conn.close();}