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
- Public class Main {
- public static void Modify (String stattime)
- {
- String datas = "/var/spool/cron/oracle";
- File data = new file (datas);
- StringBuffer backup = null;
- try {
- StringBuffer content = new StringBuffer ();
- Backup = new StringBuffer ();
- BufferedReader san = new BufferedReader (new FileReader (data));
- String line = null;
- While ((Line=san.readline ()) =null)
- {
- Backup.append (line);
- Backup.append ("/n");
- }
- Content.append ("15");
- Content.append (Stattime.trim ());
- Content.append ("* * * */home/oracle/task.sh");
- Content.append ("/n");
- Content.append ("13");
- Content.append (Stattime.trim ());
- Content.append ("* * * */home/oracle/tj_task/task2.sh");
- Content.append ("/n");
- FileWriter writer = new FileWriter (data,false);
- Writer.write (Content.tostring ());
- Writer.flush ();
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- Try
- {
- FileWriter writer = new FileWriter (data,false);
- Writer.write (Backup.tostring ());
- Writer.flush ();
- }catch (Exception ex)
- {
- Ex.printstacktrace ();
- }
- }
- }
- }
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