oracle 建立package 以及对其中的存储过程的调用

1、建立package:java

create or replace
 

package test1
 
is 
  procedure  my;
  
  end;

 

2、建立package body:sql

create or replace 


package body test1

  is 
  procedure my
  is 
  begin 
  dbms_output.put_line('test');
  end my;
 
 end test1;

3、调用 package下的存储过程:oracle

begin

  test1.my;
  
end ;   

而后就在DBMS输出中看到 存储过程已经调用。

在pl/sql develper中 选中 存储过程,点test ,显示以下:code

beginget

 call test1.my;io

end;class

 

在java中调用存储过程:test

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import com.wa.sql.oracle.utils.DBUtils;
public class Procedure {
 
   public static void main(String[] args) throws SQLException {
  
    Connection connection =DBUtils.getConnect();
    
    CallableStatement statement =connection.prepareCall("{call test1.my}");
    statement.execute();
   
    DBUtils.closeConnection(connection);
 }
}

 

2、对有参数的存储过程的调用:import

--建立存储过程
create or replace procedure 
param_pro (age number , outage out number )
is
begin 
outage := age +1000;
end param_pro;



--sql 中调用 
declare
str number;
begin 
param_pro(10,str);
dbms_output.put_line(str);
end;

2、Java中调用:程序

public class Procedure {
 
   public static void main(String[] args) throws SQLException {
  
    Connection connection =DBUtils.getConnect();
    
    CallableStatement statement =connection.prepareCall("{call param_pro(?,?)}");
    // 第一个参数赋值
    statement.setInt(1,200);
    //输出参数的类型
    statement.registerOutParameter(2,java.sql.Types.INTEGER);
    //执行存储过程
    statement.execute();
    //获取输出结果
   System.out.println(statement.getInt(2));
    DBUtils.closeConnection(connection);
 }
}

运行程序后控制台输出 :1200 ,程序正常!

相关文章
相关标签/搜索