函数——>oracle详细

来源:互联网 发布:英伦 知乎 编辑:程序博客网 时间:2024/03/28 20:12
create or replace function fun_InsertPerson
(
   p_id   out number,
   p_user_name varchar2,
   p_password   varchar2,
   p_real_name  varchar2,
   p_birthday   date
)
return varchar2
is
returnVal varchar2(50);
begin
   select seq_personid.nextval into p_id from dual;
   insert into person(id,user_name,password,real_name,birthday)
   values(p_id,p_user_name,p_password,p_real_name,p_birthday);
   returnVal := p_user_name;
   return (returnVal);
end;

在pl sql中这样调用

方法1:

declare
   p_id  number;
   p_user_name varchar2(50);
begin
   p_user_name := fun_InsertPerson(p_id,'test','111111','test',sysdate);  
   dbms_output.put_line(p_id);
   dbms_output.put_line(p_user_name);

end;

方法2:command window中:

select my_fun(‘my’)from dual;



在java中这样调:

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "scott");
CallableStatement cs = con.prepareCall("{? = call test_function(?)}");
cs.registerOutParameter(1, oracle.jdbc.OracleTypes.VARCHAR);
cs.setInt(2, 1);
cs.execute();
String retValue = cs.getString(1);
System.out.println(retValue);


在ibatis中怎么调用?