Oracle triggers call Java programs

Source: Internet
Author: User

1, create Java program: Receive parameters passed by stored procedure, send socket communication. Create or replace and Compilejava source named Jym asImportJava.io.BufferedReader;ImportJava.io.inputstream;importjava.io.inputstreamreader;ImportJava.io.OutputStream;Importjava.io.stringwriter;importjava.net.inetsocketaddress;ImportJava.net.Socket;Importjava.sql.Connection;Importjava.sql.drivermanager;importjava.sql.preparedstatement;Importjava.sql.Statement; Public classJym { Public StaticStringsendsynmsg (String ipaddr,byte[] datas)throwsexception{inetsocketaddress Endpoint=NewInetsocketaddress (IPADDR, 18002); Socket Socket=NULL; OutputStream out=NULL; InputStream in=NULL; Try{Socket=NewSocket ();   Socket.connect (endpoint); out=Socket.getoutputstream (); Inch=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 voidSay (String IP,byte[] context)throwsException {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 (?)"); Pr.setbytes (1, context);  Pr.executeupdate ();  Pr.close ();  Conn.close ();}} 2, stored procedure call Java source Create or replace procedure socket (IP varchar2, context raw) as language Java name ' Jym.say (java.lang.string,byte[]) '; 3, trigger call stored procedure Create or replace trigger Todata_after after insert on Todata forEach row begin socket (' 192.168.9.12 ',:New. Context);   End Todata_after; Once the code is written, you can test it with a stored procedure: The SQL code call socket (' 192.168.9.12 ', ' 023132 ');   In order to be able to see the error message in the console, first run the following 3 words set serveroutput on; Set serveroutput on size5000; Call Dbms_java.set_output (5000); If you run a stored procedure with a prompt to copy permissions to the user, follow the prompts to assign permissions under SYSDBA, such as: SQL code exec dbms_java.grant_permission (' Zhym ', ' SYS:java.net.SocketPermission ', ' 192.168.9.12:18002 ', ' connect,resolve ') 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 'after exec, the statement will be printed in your error message, as long as the front plus exec. 

Oracle triggers call Java programs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.