1.创建package function
create or replace package pkg_test is
function f_get_name(
param in varchar2
) return varchar2;
end pkg_test;
/
create or replace package body pkg_test is
function f_get_name(
param in varchar2
) return varchar2 is
begin
if param = '1' then
return 'scott1';
elsif param = '2' then
return 'scott2';
else
return 'scott3';
end if;
end f_get_name;
end pkg_test;
/
2.Java调用函数
DBean db = null;
CallableStatement cstmt = null;
ResultSet rs = null;
String sql = null;
db = new DBean();
db.connect();
Connection con = db.getConnection();
//con.setAutoCommit(false);
String callSQL = "{ ? = call pkg_test.f_get_name(?) }";
cstmt = con.prepareCall(callSQL);
cstmt.registerOutParameter(1,Types.VARCHAR);
cstmt.setString(2,"2");
cstmt.execute();
String name = cstmt.getString(1);
//con.commit();
System.out.println(name); //scott1