JDBC 访问 Oracle9i 存储过程

       p2 IN number,
       p3 OUT number);
 
   FUNCTION myf_mult
     ( p1 IN number,
       p2 IN number)
     RETURN  number;
  
   FUNCTION myf_rtnrcd
     ( p_deptno in number )
     RETURN t_rec;--此函数返回类型为Cursor

END; -- Package spec

PACKAGE BODY MYTESTPKG1
IS
   PROCEDURE my_add
    ( p1 in number ,
      p2 in number,
      p3 out number)
    IS
   BEGIN
      p3:=p1+p2;
   END;

   function myf_mult
   ( p1 in number,
     p2 in number)
     return number
     is
     p3 number(8);
     begin
        p3:=p1*p2;
        return p3;
     end;
    
   function myf_rtnrcd
   ( p_deptno in number )
   return t_rec
   is
   myrec t_rec;--定义Cursor类型的变量
   begin
       open myrec for select * from dept where deptno=p_deptno;
       return myrec;--返回一个Cursor变量
   end;
    
END;

二、JDBC程序实例:

1. OracleConfig.java :

package com.JerryZhang.dbms;

public class oracleConfig
{
  private String drivername="oracle.jdbc.driver.OracleDriver";
  private String dburl="jdbc:oracle:thin:@localhost:1521:ora901";
  private String user="scott";
  private String password="tiger";
 
 
  public oracleConfig()
  {
  }

  public String getDriverName(){
    return drivername;
  }
  public String getDbURL(){
    return dburl;
  }
  public String getUser(){
    return user;
  }
  public String getPassword(){
    return password;
  }
}

 

2. Class1.java :

package com.JerryZhang.dbms;
import java.sql.*;
import oracle.jdbc.OracleTypes; //需要在class path 中加入 oracle lib: class12.jar, nls_charset12.jar
public class Class1
{
  public Class1()
  {
  }
  public static void main(String args[]) throws Exception {
    oracleConfig oracleconfig=new oracleConfig();
    Connection conn;
    Statement stmt;
    ResultSet rs;
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn=DriverManager.getConnection(oracleconfig.getDbURL(),oracleconfig.getUser(),oracleconfig.getPassword());


    // Statement 
    stmt=conn.createStatement();
    rs=stmt.executeQuery("select * from dept");
    while(rs.next()){
      System.out.println(rs.getString(2));
    }

    // CallableStatement 1 - 调用一个含有out参数的procedure
    CallableStatement cs=conn.prepareCall("{call mytestprc10(?,?,?)}");
    cs.setString(1,"10");
    cs.setString(2,"20");
    cs.registerOutParameter(3,Types.INTEGER);//注意此处对返回参数的设置方法
    
    cs.executeUpdate();
    int t=cs.getInt(3);
    System.out.println("CallableStatement 1 :"+t);
    cs.close();

    //CallableStatement 2 - 调用一个返回数值型参数的函数
    CallableStatement cs2=conn.prepareCall("{?=call mytestpkg1.myf_mult(?,?)}");
    cs2.registerOutParameter(1,Types.INTEGER);//注意此处对返回参数的设置方法
    cs2.setInt(2,2);
    cs2.setInt(3,3);
    cs2.executeUpdate();
    int t2=cs2.getInt(1);
    System.out.println("CallableStatement 2 : " + t2);
    cs2.close();
   
    //CallableStatement 3 - 调用一个返回Cursor类型参数的函数
    CallableStatement cs3=conn.prepareCall("{?=call mytestpkg1.myf_rtnrcd(?)}");
    cs3.registerOutParameter(1, OracleTypes.CURSOR);//注意此处对返回参数的设置方法,和上面的有所不同
    cs3.setInt(2,20);
    cs3.executeUpdate();
    ResultSet rs3=(ResultSet)cs3.getObject(1);
    rs3.next();
    System.out.println("CallableStatement 3 : " + rs3.getString(2));
    rs3.close();
    cs3.close();


      rs.close();
      stmt.close();
      conn.close();
    }
  }

你可能感兴趣的:(oracle)