下面为您介绍的Java调用Oracle函数方法,共两种情况,一种调用无入参的Oracle函数,一种则是调用有一个入参,一个输出参数以及一个字符串返回值的Oracle函数。
调用无入参的函数
函数定义
- CREATE OR REPLACE Function f_getstring Return Varchar2 Is
- Begin
- Return ''String value'';
- End f_getstring;
调用函数的Java片断
- CallableStatement cstmt = con.prepareCall("{?=call f_getstring}");
- cstmt.registerOutParameter(1, Types.VARCHAR);
- cstmt.execute();
- String strValue = cstmt.getString(1);
- System.out.println("The return value is:" + strValue);
- cstmt.close();
调用有一个入参,一个输出参数以及一个字符串返回值的函数
函数定义
- CREATE OR REPLACE Function f_Getinfo(Id Integer, Age Out Integer) Return Varchar2 Is
- Begin
- Age := 10;
- Return ''The age is:'' || Id;
- End f_Getinfo;
调用函数的Java代码片断
- CallableStatement cstmt = con
- .prepareCall("{?=call f_getinfo(?,?)}");
- cstmt.registerOutParameter(1, Types.VARCHAR);
- cstmt.setInt(2, 11);
- cstmt.registerOutParameter(3, Types.INTEGER);
- cstmt.execute();
- String strValue = cstmt.getString(1);
- int age = cstmt.getInt(3);
- System.out.println("The return value is:" + strValue
- + " and age is:" + age);
- cstmt.close();
转自: http://database.51cto.com/art/201010/231091.htm