欢迎来到天天文库
浏览记录
ID:38341095
大小:44.50 KB
页数:4页
时间:2019-06-10
《如何在Java程序中调用存储过程》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、如何在Java程序中调用存储过程(一)?(1) 使用scott/tiger用户在Oracle中创建2个表并插入几条数据。Createtablecarl_test(Avarchar2(200));createtablecarl_test1(Bvarchar2(200));--insertsomedataintocarl_test1tableinsertintocarl_test1values('carl1');insertintocarl_test1values('carl2');insertintocarl_test1values('carl3');co
2、mmit;(2) 使用scott/tiger用户在Oracle中创建一个测试存储过程,该存储过程有三个参数,第一个参数为输入参数,最后两个参数为输出参数。为简单起见,没有在该存储过程中加入异常捕获机制。CREATEORREPLACEPROCEDUREcarl_sp_test(v_monthlyINvarchar2,last_inserted_rows_numOUTnumber,all_effected_rows_numOUTnumber)ISBEGIN/*删除carl_test表中所有记录*/deletecarl_test;/*将删除行数赋值给总影响行数
3、*/all_effected_rows_num:=SQL%Rowcount;commit;/*将用户输入参数插入carl_test表中*/insertintocarl_test(a)values(v_monthly);all_effected_rows_num:=all_effected_rows_num+SQL%Rowcount;/*将表carl_test1中的所有记录插入到carl_test1中*/insertintocarl_testselect*fromcarl_test1;/*获得影响记录数*/last_inserted_rows_num:=SQ
4、L%Rowcount;all_effected_rows_num:=all_effected_rows_num+SQL%Rowcount;commit;ENDcarl_sp_test;(3) 使用scott/tiger用户在SQL/Plus中测试上述存储过程SQL>variableall_effected_rows_numnumber;SQL>variablelast_inserted_rows_numnumber;SQL>execcarl_sp_test('firstvar',:last_inserted_rows_num,:all_effected_
5、rows_num);PL/SQLproceduresuccessfullycompletedlast_inserted_rows_num---------3all_effected_rows_num---------4SQL>printlast_inserted_rows_num;last_inserted_rows_num---------3SQL>printall_effected_rows_num;all_effected_rows_num---------4SQL>上述结果表示测试成功(4) 使用下面的Java类TestStoreProcedur
6、e.java测试存储过程。packagetest;importjava.sql.CallableStatement;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importoracle.jdbc.OracleTypes;/***ThisclassisusedtotestOraclestoreprocedure*@authorCarlWu**/publicclassTestStoreProcedure{/***测试主方法*@paramar
7、gs*/publicstaticvoidmain(String[]args){Connectioncon=null;try{//获得数据库连接java.lang.Class.forName("oracle.jdbc.driver.OracleDriver");con=DriverManager.getConnection("jdbc:oracle:thin:scott/tiger@localhost:1521:LEGDB");//测试存储过程TestStoreProceduresp=newTestStoreProcedure();sp.execute(co
8、n,"carl_sp_test","carl");}catch(C
此文档下载收益归作者所有