大侠们 我想问一下,我有一个存储过程有一个输入参数和2个输出参数,一个输出参数是返回一个vachar一个是返回一个集类型,我想问怎么写存储过程,最主要是的在程序中怎么调用,详细贴一下代码,并给我讲解一下,谢谢了。要全代码哦  我有点菜    

解决方案 »

  1.   


    首先,建立存储过程( 建立student表略 ):
    create or replace procedure p_student_info(i_sex in varchar2,o_cur out sys_refcursor)
     is
    begin
      open o_cur for select * from student where student_sex=i_sex;
    end;
    存储过程既有in类型也有out类型且out类型是cursor。
    java代码:
    建立数据库链接省略,注意不要以system账号链接:
    调用方法:
    public static void cursorTest(){
      Connection conn = JdbcUtil.getConnection();
      CallableStatement cstmt = null;
      try {
       cstmt = conn.prepareCall("{call p_student_info(?,?)}");
       cstmt.setString(1, "m");
       cstmt.registerOutParameter(2, OracleTypes.CURSOR);//out类型需要注册
       cstmt.execute();
       ResultSet rs = (ResultSet)cstmt.getObject(2);//此处的2要与存储过程中cursor的问题对应
       while(rs.next()){
        System.out.println(rs.getString(1));//获取具体的值
       }
      
      } catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
     }
    调用以上方法测试。
     
      

  2.   

    create or replace procedure p_student_info(i_sex in varchar2,o_cur out sys_refcursor)
     is
    begin
      open o_cur for select * from student where student_sex=i_sex;
    end;
      

  3.   

    create or replace procedure p_student_info(i_sex in varchar2,o_cur out sys_refcursor)
     is
    begin
      open o_cur for select * from student where student_sex=i_sex;
    end;