oracle调⽤JAVA类的⽅法
导⼊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类的⽅法主要有以下三种:
1. ⽤loadjava⽅法装载;
可能是调试⽅便,据说这种⽅法⽐较通⽤。
c:\test\hello.java
Java Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15public 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 -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 -v -resolve hello.java
loadjava -u -v -resolve hello.java
1. ⽤sql语句创建
Code
1 2 3 4 5create or replace and compile java source named hehe AS
public class hello
{
public static void msg(String name)
7 8 9 10 11 12 13 14 15 16 17 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
1. ⽤外部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
可能出现的错误
S QL> 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 '&hod>(<parameter list in java datatype>) return java datatype of return variable';
例:
登陆某⼀⽤户登录,并创建java程序资源,在pl/sql中java source中显⽰你所编写的java代码;
SQL Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16create 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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 sRtn = " try beg";
connection = Connection("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);
sRtn = "set value";
sRtn = "execute";
sRtn = String(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 ant_permission( 'ONBOARDING', 'SYS:java.io.FilePermission', '<<ALL FILES>>', 'execute' );
java replace方法收回权限的语句如下
EXEC voke_Permission('ONBOARDING', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', ''); EXEC voke_Permission('ONBOARDING', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', ''); EXEC voke_permission( 'ONBOARDING', 'SYS:java.io.FilePermission', '<<ALL FILES>>', 'execute' );
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论