JAVA调用ORACLE存储过程

 

一:简单的调用存储过程返回几个字段

1:建立测试表及存储过程

 

----创建表 userInfo
create table userInfo
(
userid varchar2(5),
username varchar2(10),
usersex varchar2(2),
useage number
);
----插入测试数据
insert into userInfo values('U001','zhang','女',20);
insert into userInfo values('U002','Jack','男',21);
insert into userInfo values('U003','Jone','女',21);
insert into userInfo values('U004','Bill','男',23);
commit;
----创建过程
create or replace procedure user_pro
  ----参数
 (uid in varchar2,uname out varchar2,usex out varchar2,uage out number)
as
 ----变量
 U_name varchar2(10);
 U_sex varchar2(2);
 U_age number;
begin
select username,usersex,useage into U_name,U_sex,U_age from userInfo where userid=uid;
uname:=U_name;
usex:=U_sex;
uage:=U_age;
end;

 里面的存储过程还可以简化为:

 

 

create or replace procedure user_pro
 (uid in varchar2,uname out varchar2,usex out varchar2,uage out number)
as
 
begin
select username,usersex,useage into uname,usex,uage from userInfo where userid=uid;
end;

  

 

2:调用函数

 

import java.sql.*;
 
public class Main { 
 
    public static void main(String[] args) {
        Connection conn=null;
        String url = "jdbc:oracle:thin:@localhost:1521:eos";
        String username="eos";
        String pwd = "eos";
        try{
            //连接到数据库
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn=DriverManager.getConnection(url,username ,pwd);
            //用这个方法调用存储过程
            //第一个参数为in,后三个为out,注意方式的不同
            CallableStatement cs=conn.prepareCall("{call user_pro(?,?,?,?)}");
            cs.setString(1,"U001");//设置in参数
            cs.registerOutParameter(2,Types.VARCHAR);//注册返回值类型
            cs.registerOutParameter(3,Types.VARCHAR); //注册返回值类型
            cs.registerOutParameter(4,Types.INTEGER); //注册返回值类型
            cs.execute();//执行过程
            //下面是获得out的值,注意了,不是ResultSet,而是CallableStatement
            String name=cs.getString(2);
            String sex=cs.getString(3);
            int age=cs.getInt(4);
            System.out.println("  "+"姓名:"+name+"  性别:"+sex+"  年龄:"+age);
        }catch(Exception ex){
            System.out.print("异常:"+ex.toString());
        }
         
    }
}

  

 

二:返回游标,

 

java.sql.Connection
java.sql.Driver
java.sql.CallableStatement
Type.Others
可以换成
oracle.jdbc.OracleConnection
oracle.jdbc.OracleDriver
oracle.jdbc.OracleCallableStatement
OracleType.CURSOR
cstmt = conn.prepareCall("{call list_early_deaths(?)}");
            cstmt.registerOutParameter(1, OracleTypes.CURSOR);
            cstmt.execute();
            ResultSet rs = (ResultSet)cstmt.getObject(1);
 
            while(rs.next())
            {
              System.out.print("id = "+rs.getInt(1));
              System.out.print("name = "+rs.getString(2));
              System.out.print("pwd = "+rs.getString(3));
              System.out.println();
            }
create or replace procedure list_early_deaths
(r_cursor out sys_refcursor)
as
 
begin
 open r_cursor for select *  from my_user u;
end;

 

 

你可能感兴趣的:(oracle存储过程)