欢迎来到天天文库
浏览记录
ID:12351445
大小:37.50 KB
页数:6页
时间:2018-07-16
《callablestatement调用oracle存储过程返回结果集(resultset)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)一:无返回值的存储过程调用存储过程: CREATEORREPLACEPROCEDUREPRO_1(PARA1INVARCHAR2,PARA2INVARCHAR2) AS BEGIN INSERTINTODBO.EMP(ID,NAME)VALUES(PARA1,PARA2); ENDPRO_1; Java代码: packagecom.icesoft.service;i
2、mportjava.sql.*;importjava.sql.ResultSet;publicclassCallProcedureTest1{publicCallProcedureTest1(){ super();}publicstaticvoidmain(String[]args){ Stringdriver="oracle.jdbc.driver.OracleDriver"; Stringurl="jdbc:oracle:thin:@127.0.0.1:1521:orcl"; Stringus
3、er="admin"; Stringpwd="password"; Connectionconn=null; CallableStatementcs=null; ResultSetrs=null; try{ Class.forName(driver); conn=DriverManager.getConnection(url,user,pwd); cs=conn.prepareCall("{callDBO.PRO_1(?,?)}"); cs.setString(1,"10");
4、 cs.setString(2,"Peter"); cs.execute(); }catch(SQLExceptione){ e.printStackTrace(); }catch(Exceptione){ e.printStackTrace(); }finally{ try{ if(rs!=null){ rs.close(); } if(cs!=null){ cs.close(); } if(conn!=null){ conn
5、.close(); } }catch(SQLExceptione){ } } }}备注,存储过程PRO_1中用到了表EMP(ID,NAME),需事先建好二:有返回值的存储过程(非结果集) 存储过程: CREATEORREPLACEPROCEDUREPRO_2(PARA1INVARCHAR2,PARA2OUTVARCHAR2) AS BEGIN SELECTINTOPARA2FROMEMPWHEREID=PARA1; ENDPRO_2; Java代码:
6、 packagecom.icesoft.service;importjava.sql.*;publicclassCallProcedureTest2{publicCallProcedureTest2(){ super();}publicstaticvoidmain(String[]args){ Stringdriver="oracle.jdbc.driver.OracleDriver"; Stringurl="jdbc:oracle:thin:@127.0.0.1:1521:orcl"; Stri
7、nguser="admin"; Stringpwd="password"; Connectionconn=null; CallableStatementcs=null; ResultSetrs=null; try{ Class.forName(driver); conn=DriverManager.getConnection(url,user,pwd); cs=conn.prepareCall("{callDBO.PRO_2(?,?)}"); cs.setString(1,"10
8、"); cs.registerOutParameter(2,Types.VARCHAR); cs.execute(); Stringname=cs.getString(2); System.out.println("name:"+name); }catch(SQLExceptione){ e.printStackTrace(); }catch(Exceptione){ e.printStackTra
此文档下载收益归作者所有