触发器,存储过程,oracle调用java方法


1 编写触发器tri_oa_task_pm

create or replace trigger tri_oa_task_pm
  after insert on oa_activity_data
  for each row
begin
  if :new.hjzt = 'running' then
    HeerMsgUtil('task', :new.activity_data_id, 'add');
  end if;
end tri_oa_task_pm;

2 编写存储过程HeerMsgUtil

create or replace procedure HeerMsgUtil(tableName in VARCHAR2,id in VARCHAR2,opt in VARCHAR2)
as language java name
'HeerMessage.message (java.lang.String,java.lang.String,java.lang.String)';

这句话的意思是:用java语言调用名为HeerMessage.message(),来创建或者替代已有的存储过程HeerMsgUtil

3 定义java类

create or replace and compile java source named heermessage as
import java.net.*;

public class HeerMessage {
  public static void message(String table_name, String opt_name, String id) {
    try {
      URL url = new URL("http://192.168.0.1:8080/test/method1?t=" + table_name + "," + opt_name + "," + id);
      URLConnection rulConnection = url.openConnection();
      System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
      System.setProperty("sun.net.client.defaultReadTimeout", "30000");
      rulConnection.connect();
      rulConnection.getInputStream();
      System.out.print("HeerMessage.message("+table_name+","+opt_name+","+id+")");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
};

4 url?t=......就是访问你要访问的服务端,在服务端去处理你的逻辑

5 在用oracle存储过程调用java程序的时候,在java的code中有URL的代码,所以由于权限问题,导致无法正常运行。解决办法是赋权:

exec dbms_java.grant_permission( 'username','SYS:java.net.SocketPermission', 'localhost:8080', 'connect,resolve' );
exec dbms_java.grant_permission( 'username', 'SYS:java.util.PropertyPermission', 'sun.net.client.defaultConnectTimeout', 'write' );
exec dbms_java.grant_permission( 'username', 'SYS:java.util.PropertyPermission', 'sun.net.client.defaultReadTimeout', 'write' );



你可能感兴趣的:(3,数据库原理和应用)