Invoking a Java program in an Oracle trigger

Source: Internet
Author: User
Tags sqlplus stringbuffer

Because the project needs to have a written Java program that wants to be called in a trigger on a table in Oracle so that every time the data is updated, the Java program is called to modify the background data.

The process is now documented as follows:

1. Writing Java programs

[Java]View PlainCopy
  1. Public class Main {
  2. public static void Modify (String stattime)
  3. {
  4. String datas = "/var/spool/cron/oracle";
  5. File data = new file (datas);
  6. StringBuffer backup = null;
  7. try {
  8. StringBuffer content = new StringBuffer ();
  9. Backup = new StringBuffer ();
  10. BufferedReader san = new BufferedReader (new FileReader (data));
  11. String line = null;
  12. While ((Line=san.readline ()) =null)
  13. {
  14. Backup.append (line);
  15. Backup.append ("/n");
  16. }
  17. Content.append ("15");
  18. Content.append (Stattime.trim ());
  19. Content.append ("* * * */home/oracle/task.sh");
  20. Content.append ("/n");
  21. Content.append ("13");
  22. Content.append (Stattime.trim ());
  23. Content.append ("* * * */home/oracle/tj_task/task2.sh");
  24. Content.append ("/n");
  25. FileWriter writer = new FileWriter (data,false);
  26. Writer.write (Content.tostring ());
  27. Writer.flush ();
  28. } catch (FileNotFoundException e) {
  29. E.printstacktrace ();
  30. } catch (IOException e) {
  31. Try
  32. {
  33. FileWriter writer = new FileWriter (data,false);
  34. Writer.write (Backup.tostring ());
  35. Writer.flush ();
  36. }catch (Exception ex)
  37. {
  38. Ex.printstacktrace ();
  39. }
  40. }
  41. }
  42. }

Note that the Java program here does not need to follow the standard Java program, must have the main entry function, instead, the function here is generally not named Main, unless the format is the standard main function format, I made this mistake at the beginning, I have been unable to find the reason, Later through the csdn on the people, only to find that I put the above function name written in the main, and then change the name of the good. Also, this function must be static.

2. Load the Java program into Oracle

In the $oracle_home/bin directory, there is a Loadjava command that uses this command to load the newly written Java program into the database.

Loadjava-user test/[email protected] -o-v-f-r Main.java

If successful, will print out the message prompt success, if the program has compiled errors, will also prompt you the wrong place.

3. Modify Permissions

Because my Java program involves the read and write operation of the file, you need to modify the permissions first.

Log into the database as an administrator first

Sqlplus/as SYSDBA

And then execute

Begin

Dbms_java.grant_permission (' TEST ', ' SYS:java.io.FilePermission ', '/var/spool/cron/oracle ', ' Read,write,execute, Delete ');

End

/

After execution, when executing the above Java program in the database, you have permission to read and write the file.

4. Create a stored procedure

Into the database, I was in the Sqlplus command line

Create or Replace procedure Modify (name varchar2) as language Java name ' main.modify (java.lang.String) ';

/

You are prompted to create the stored procedure successfully.

5. Increase Trigger

I added trigger to a table directly in Toad, so I just wrote the key part of the code

Begin

If:old.name = ' time ' Then

Modify (: New.value);

End If;

End

At this point, the call to the Java program in Oracle's trigger is complete, and later experiments prove that whenever the table is updated, it is true that the Java program is executed and the file is read and written.

Invoking a Java program in an Oracle trigger

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.