java 调用oracle包_Java代码调用Oracle的存储过程,存储函数和包

1 importjava.sql.CallableStatement;2 importjava.sql.Connection;3 importjava.sql.ResultSet;4 importjava.sql.SQLException;5

6 importoracle.jdbc.driver.OracleCallableStatement;7 importoracle.jdbc.driver.OracleTypes;8

9 importorg.junit.Test;10

11 public classTestOracle {12

13 /*

14 * CallableStatement 接口15 * 调用存储函数,等号左边有一个返回值16 * {?= call [(,, ...)]}17 * 调用存储过程. 没有返回值18 {call [(,, ...)]}19

20 *21 */

22

23 /*存储过程 查询某个员工的姓名  月薪 职位24 * create or replace procedure queryEmpinfo(eno in number,25 pename out varchar2,26 psal out number,27 pjob out varchar2)28 */

29

30 @Test31 public voidtestProcedure(){32 //{call [(,,...)]}

33 String sql = "{call queryEmpinfo(?,?,?,?)}";//4个问号中,第一个是输入参数,其余是输出参数

34 Connection conn = null;35 //要用CallableStatement这个接口,用于执行 SQL 存储过程的接口

36 CallableStatement call = null;37

38 try{39 conn =JDBCUtils.getConnection();40 call =conn.prepareCall(sql);41 //对于in参数,需要赋值

42 call.setInt(1,7839);43 //对于out参数,需要声明

44 call.registerOutParameter(2, OracleTypes.VARCHAR);//第二个是字符串

45 call.registerOutParameter(3, OracleTypes.NUMBER);//第三个是数字

46 call.registerOutParameter(4, OracleTypes.VARCHAR);//第四个是字符串

47

48 call.execute();49 //取出结果

50 String name = call.getString(2);51 double sal = call.getDouble(3);52 String job = call.getString(4);53 System.out.println(name+"\t"+sal+"\t"+job+"\t");54 } catch(SQLException e) {55 e.printStackTrace();56 }finally{57 JDBCUtils.release(conn, call, null);//没有最后一个参数就传入null

58 }59 }60

61 /*存储函数 查询某个员工的姓名,月薪和职位62 * create or replace function queryEmpIncome(eno in number)63 return number64 */

65 @Test66 public voidtestFunction(){67 //{?= call [(,, ...)]}68 //第一个问号是函数的返回值,第二个问号是输入参数. 返回值的作用和输出参数是一样的.

69 String sql = "{?=call QUERYEMPINCOME(?)}";//这个call后面的存储过程名或者是存储函数名大写或者是小写是没有要求的.

70 Connection conn = null;71 //要用CallableStatement这个接口,用于执行 SQL 存储过程的接口

72 CallableStatement call = null;73

74 try{75 conn =JDBCUtils.getConnection();76 call =conn.prepareCall(sql);77

78 //对于in参数,赋值

79 call.setInt(2,7839);80

81 //对于out参数,申明

82 call.registerOutParameter(1, OracleTypes.NUMBER);83 call.execute();84 //取出结果85 //取出结果

86 double income = call.getDouble(1);87 System.out.println(income);88 } catch(SQLException e) {89 e.printStackTrace();90 }finally{91 JDBCUtils.release(conn, call, null);//没有最后一个参数就传入null

92 }93

94

95 }96

97 /*

98 查询某个部门中所有员工的所有信息99 包头100 CREATE OR REPLACE PACKAGE MYPACKAGE AS101

102 type empcursor is ref cursor;103 procedure queryEmpList(dno in number,empList out empcursor);104

105 END MYPACKAGE;106

107

108 包体109 CREATE OR REPLACE110 PACKAGE BODY MYPACKAGE AS111

112 procedure queryEmpList(dno in number,empList out empcursor) AS113 BEGIN114 open empList for select * from emp where deptno=dno;115 END queryEmpList;116

117 END MYPACKAGE;118 */

119 @Test120 public voidtestCursor(){121 //{call [(,, ...)]}

122 String sql = "{call MYPACKAGE.queryEmpList(?,?)}";123

124 Connection conn = null;125 CallableStatement call = null;126 //有游标,就有结果集

127 ResultSet rest = null;128 try{129 conn =JDBCUtils.getConnection();130 call =conn.prepareCall(sql);131

132 //对于in参数,赋值

133 call.setInt(1, 20);134

135 //对于out参数,申明

136 call.registerOutParameter(2, OracleTypes.CURSOR);137 call.execute();138 //取出集合139 //这个地方要强转!!!OracleCallableStatement是抽象类,继承了CallableStatement140 //不强转没有getCursor()方法...

141 rest = ((OracleCallableStatement)call).getCursor(2);142 while(rest.next()){143 String name = rest.getString("ename");144 double sal = rest.getDouble("sal");145 System.out.println(name+"\t"+sal);146 }147 }catch(Exception e) {148 e.printStackTrace();149 }finally{150 JDBCUtils.release(conn, call, rest);//上面打开了光标,再这个地方关闭结果集rest,也就关闭了光标

151 }152 }153 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值