Oracle中怎样使用JAVA函数及自定义函数(转)
环境:windows2000 + oracle9.2
1、loadjava
2、直接在Oracle中使用Java源,Oracle内置JVM,对java支持很好
(1). 建立 java source
create or replace and compile java source named js_output_test as
public class js_test
{
public static String js_desc()
{
return "this is java source test.";
}
}
(2). 发布java source
CREATE OR REPLACE FUNCTION fn_test_js RETURN VARCHAR2
AS LANGUAGE JAVA NAME 'js_test.js_desc() return java.lang.String';
(3). 测试.
SQL> set serveroutput on;
SQL> var v_test varchar2(25);
SQL> call fn_test_js() into :v_test;
调用完成。
SQL> print v_test;
V_TEST
--------------------------------
this is java source test.
注意:发布时参数中的java.lang.String一定要写全,返回类型可以用number代替int
====================================================================
create or replace and compile java source named hello as
public class Hello {
static public String Message(String yname) {
return "Hello, " + yname;
}
}
create or replace function hello (Name VARCHAR2) return VARCHAR2
as language java name
'Hello.Message (java.lang.String) return java.lang.String';
Select hello('every body!') From dual;
====================================================================
create or replace and compile java source named sea as
public class Sea{
public static String myReplaceAll(String src,String name1,String name2) {
int a=0; int n=name1.length(); int l=src.indexOf(name1,a);
while(l>0){
src = src.substring(0,l) + src.substring(l+n);
a=a+n;
l=src.indexOf(name1,a);
}
return src;
}
public static String getSameCompany(String name11,String name22){
if(name11==null || name22==null){
return "false";