Oracle触发器反向调用Java程序

时间:2021-09-30 09:56:38
导入jar包
在oracle中导入需要的jar包,我们把编辑好的java类打成jar包,直接在oarcle里面写简单的调用就可以了,
 1、操作系统需要拥有支持loadjava命令的jdk。
 2、加载jlha.jar包,到oracle数据库中。
操作过程:在dos环境下,输入命令: loadjava -r -f -o -user usscares/usscares@usscares jlha.jar
注意:jar包要在1.4的环境下编译,项目右键 properties java compiler compoler compliance level 1.4 即可 否则报version 49 类似的错误
调用JAVA类
oracle调用JAVA类的方法主要有以下三种: 用loadjava方法装载;
可能是调试方便,据说这种方法比较通用。
c:\test\hello.java
public class hello
{ public static void main(String[] args)
{
System.out.println("Hello");
hello h = new hello();
h.insertM(9);
}
public static void insertM(int pid)
{
System.out.println("This is the method insertM.");
}
} C:\test>loadjava -u test/test@mydb -v -resolve hello.java SQL> create procedure prc_hehe as language java name 'hello.main(java.lang.String[]) 过程已创建。 SQL> call prc_hehe(); 调用完成。 SQL> set serveroutput on size 2000
SQL> call prc_hehe(); 调用完成。 SQL> exec dbms_java.set_output(2000); PL/SQL 过程已成功完成。 SQL> call prc_hehe();
Hello
This is the method insertM. 调用完成。 SQL>show errors; 修改java类,先删除再装载,方法: dropjava -u test/test@mydb -v -resolve hello.java loadjava -u test/test@mydb -v -resolve hello.java 用sql语句创建
create or replace and compile java source named hehe
AS
public class hello
{
public static void msg(String name)
{
System.out.println("hello," + name);
}
}; create or replace procedure prc_hehe
(
p_name VARCHAR2
)
as
language java name 'hello.msg(java.lang.String)'; -- 调用结果 SQL> call prc_hehe('oopp');
hello,oopp 用外部class文件来装载创建
create or replace directory CLASS_DIR as 'c:\test'; create or replace java class using bfile(class_dir,'hello.class'); create or replace procedure prc_hello
(
p_name VARCHAR2
)
as
language java name 'hello.msg(java.lang.String)'; -- 测试结果 SQL> call prc_hello('java');
java 可能出现的错误
SQL> call prc_hello('Jerry');
call prc_hello('Jerry')
*
第 1 行出现错误:
ORA-29516: Aurora 断言失败: Assertion failure at eox.c:359
Uncaught exception System error: java/lang/UnsupportedClassVersionError 原因:机器装了多个java版本,oracle的java版本低于环境变量设置的版本。 解决方法:用$ORACLE_HOME/jdk/javac 重新编译java文件 核对java已经导入数据库 select * from user_source where type LIKE 'JAVA%' AND NAME = '<java file>' 建立function CREATE OR REPLACE FUNCTION <FUNCTION_NAME> (<PARAMETER LIST IN ORACLE DATATYPE>) RETURN <ORACLE DATATYPE OF RETURN VARIABLE> AS
LANGUAGE JAVA
NAME '<clase.method>(<parameter list in java datatype>) return java datatype of return variable'; 例: 登陆某一用户登录,并创建java程序资源,在pl/sql中java source中显示你所编写的java代码;
create or replace and compile java source named bb_wx_replosssbk as
import java.sql.*;
import oracle.jdbc.driver.*; public class bb_wx_replosssbk
{
/**
* 社保卡挂失
*/
public static String callProc(String sSfzh, String sPwd, String sType)
{
OracleDriver driver = new OracleDriver();
Connection connection = null;
CallableStatement cstmt = null;
String sRtn = "beg";
try
{
sRtn = " try beg";
connection = DriverManager.getConnection("jdbc:oracle:thin:user/pwd@127.0.0.1:1521:orcl");
sRtn = "con";
cstmt = connection.prepareCall("{call run_replosssbk(?,?,?,?)}");
sRtn = "invoke";
cstmt.setString(1, sSfzh);
cstmt.setString(2, sPwd);
cstmt.setString(3, sType);
cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
sRtn = "set value";
cstmt.executeUpdate();
sRtn = "execute";
sRtn = cstmt.getString(4);
}
catch (Exception e)
{
sRtn = e.toString();
e.printStackTrace();
}
finally
{
try
{
if (cstmt != null)
{
cstmt.close();
}
if (connection != null)
{
connection.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
return sRtn;
}
} 创建调用Java资源的函数
create or replace function run_bb_wx_replosssbk(
sSfz in varchar2,
sPwd in varchar2,
sType in varchar2
)
return varchar2
as
language java name 'bb_wx_replosssbk.callProc(java.lang.String,java.lang.String,java.lang.String) return java.lang.String'; 建立一过程调用存储过程
create or replace procedure RUN(
sSfz in varchar2,
sPwd in varchar2,
sType in varchar2
sRtn out varchar2
)
as
begin
--sRtn := run_bb_wx_replosssbk(sSfz in varchar2,sPwd in varchar2,sType in varchar2);
Select run_bb_wx_replosssbk(sSfz in varchar2,sPwd in varchar2,sType in varchar2)
Into sRtn from dual;
end;
/ 附: 如果需要java存取文件,需要使用dba用户赋权 EXEC Dbms_Java.Grant_Permission('ONBOARDING', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
EXEC Dbms_Java.Grant_Permission('ONBOARDING', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
EXEC dbms_java.grant_permission( 'ONBOARDING', 'SYS:java.io.FilePermission', '<<ALL FILES>>', 'execute' );
收回权限的语句如下 EXEC Dbms_Java.revoke_Permission('ONBOARDING', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
EXEC Dbms_Java.revoke_Permission('ONBOARDING', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
EXEC dbms_java.revoke_permission( 'ONBOARDING', 'SYS:java.io.FilePermission', '<<ALL FILES>>', 'execute' );