oracle触发器调用java程序

1、创建java程序:接收存储过程传递的参数,发送socket通信。

Java代码  

  1. create or replace and compile java source named jym as   
  2. import java.io.BufferedReader;   
  3. import java.io.InputStream;   
  4. import java.io.InputStreamReader;   
  5. import java.io.OutputStream;   
  6. import java.io.StringWriter;   
  7. import java.net.InetSocketAddress;   
  8. import java.net.Socket;   
  9. import java.sql.Connection;   
  10. import java.sql.DriverManager;   
  11. import java.sql.PreparedStatement;   
  12. import java.sql.Statement;   
  13. public class JYM {   
  14.     public static  String sendSynMsg(String ipAddr, byte[] datas) throws Exception{   
  15.         InetSocketAddress endpoint = new InetSocketAddress(ipAddr ,18002);   
  16.         Socket socket = null;   
  17.         OutputStream out = null;   
  18.         InputStream in = null;   
  19.         try {   
  20.             socket = new Socket();   
  21.             socket.connect(endpoint);   
  22.             out = socket.getOutputStream();   
  23.             in =  socket.getInputStream();   
  24.             out.write(datas);   
  25.             out.flush();   
  26.             return null;   
  27.         } finally {   
  28.             if (out != null) {   
  29.                 try {   
  30.                     out.close();   
  31.                 } catch(Exception ex) {   
  32.                     ex.printStackTrace();   
  33.                 }   
  34.             }   
  35.             if (in != null) {   
  36.                 try {   
  37.                     in.close();   
  38.                 } catch(Exception ex) {   
  39.                     ex.printStackTrace();   
  40.                 }   
  41.             }   
  42.             if (socket != null) {   
  43.                 try {   
  44.                     socket.close();   
  45.                 } catch(Exception ex) {   
  46.                     ex.printStackTrace();   
  47.                 }   
  48.             }   
  49.         }   
  50.     }   
  51.     public static void say(String ip,byte[] context) throws Exception {   
  52.         String str=JYM.sendSynMsg(ip,context);      
  53.         Class.forName("oracle.jdbc.driver.OracleDriver");   
  54.         Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.9.109:1521:orcl","zhym","jhhz621");     
  55.         PreparedStatement pr=conn.prepareStatement("insert into backdata(context) values(?)");   
  56.         pr.setBytes(1, context);   
  57.         pr.executeUpdate();   
  58.         pr.close();    
  59.         conn.close();   
  60.     }   
  61. }  

create or replace and compilejava source named jym as

import java.io.BufferedReader;

import java.io.InputStream;

importjava.io.InputStreamReader;

import java.io.OutputStream;

import java.io.StringWriter;

importjava.net.InetSocketAddress;

import java.net.Socket;

import java.sql.Connection;

import java.sql.DriverManager;

importjava.sql.PreparedStatement;

import java.sql.Statement;

public class JYM {

public static  StringsendSynMsg(String ipAddr, byte[] datas) throws Exception{

  InetSocketAddress endpoint = new InetSocketAddress(ipAddr ,18002);

  Socket socket = null;

  OutputStream out = null;

  InputStream in = null;

  try {

   socket = new Socket();

   socket.connect(endpoint);

   out = socket.getOutputStream();

   in = socket.getInputStream();

   out.write(datas);

   out.flush();

   return null;

  } finally {

   if (out != null) {

    try {

     out.close();

    } catch(Exception ex) {

     ex.printStackTrace();

    }

   }

   if (in != null) {

    try {

     in.close();

    } catch(Exception ex) {

     ex.printStackTrace();

    }

   }

   if (socket != null) {

    try {

     socket.close();

    } catch(Exception ex) {

     ex.printStackTrace();

    }

   }

  }

}

public static void say(String ip,byte[] context) throws Exception {

  String str=JYM.sendSynMsg(ip,context);  

  Class.forName("oracle.jdbc.driver.OracleDriver");

  Connection conn =DriverManager.getConnection("jdbc:oracle:thin:@192.168.9.109:1521:orcl","zhym","jhhz621"); 

  PreparedStatement pr=conn.prepareStatement("insert intobackdata(context) values(?)");

  pr.setBytes(1, context);

  pr.executeUpdate();

  pr.close();

  conn.close();

}

}

 2、存储过程调用java source

Sql代码  

  1. create or replace procedure socket(   
  2. ip varchar2,   
  3. context raw   
  4. )   
  5. as  
  6. language java name  
  7. 'JYM.say(java.lang.String,byte[])';  

create or replace proceduresocket(

ip varchar2,

context raw

)

as

language java name

'JYM.say(java.lang.String,byte[])';

 3、触发器调用存储过程

Java代码  

  1. create or replace trigger todata_after   
  2.   after insert on todata   
  3.   for each row   
  4. begin   
  5.    socket('192.168.9.12',:new.context);   
  6. end todata_after;  

create or replace triggertodata_after

  after insert on todata

  for each row

begin

   socket('192.168.9.12',:new.context);

end todata_after;

 

 

代码写好后可以先用存储过程测试:

Sql代码  

  1. call  socket('192.168.9.12','023132');  

call  socket('192.168.9.12','023132');

 为了能在控制台中看见错误信息,要先运行下面3句话

Sql代码  

  1. set serveroutput on;   
  2. set serveroutput on size 5000;   
  3. call dbms_java.set_output(5000);  

set serveroutput on;

set serveroutput on size 5000;

calldbms_java.set_output(5000);

 

 

假如你运行存储过程时爆出了  请给用户复制权限的提示则按照提示在sysdba 下赋权限,如:

Sql代码  

  1. exec dbms_java.grant_permission('zhym','SYS:java.net.SocketPermission','192.168.9.12:18002','connect,resolve')   
  2.   
  3. exec dbms_java.grant_permission('ZHYM','SYS:java.net.SocketPermission','127.0.0.1:1521','connect,resolve')  

execdbms_java.grant_permission('zhym','SYS:java.net.SocketPermission','192.168.9.12:18002','connect,resolve')

 

execdbms_java.grant_permission('ZHYM','SYS:java.net.SocketPermission','127.0.0.1:1521','connect,resolve')

 

exec 后面的语句会在你的报错提示中打印出来,只要前面加exec就可以了。

你可能感兴趣的:(Java)