oracle 创建或替换并解析 Java 源代码,我无法使用 Java 源代码创建函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15249424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:29:40  来源:igfitidea点击:

Create or replace and resolve Java source, I am not able to create a function using java source

javaoracle

提问by UltraCommit

Oracle environment.

甲骨文环境。

The following Java source has been correctly compiled:

以下 Java 源代码已正确编译:

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED hr.roman
   AS package Package.NumeriRomani;

class Numero{
    String r="";
    int a=0;

    Numero(int n){
        a=n;
            if(n==0){
                r="NULL";
                return;}
            while(n>=1000){
                    r+="M";
                    n-=1000;}
            if(n>=900){
                    r+="CM";
                    n-=900;}
            while(n>=500){
                    r+="D";
                    n-=500;}
            if(n>=400){
                    r+="CD";
                    n-=400;}
            while(n>=100){
                    r+="C";
                    n-=100;}
            if(n>=90){
                    r+="XC";
                    n-=90;}
            while(n>=50){
                    r+="L";
                    n-=50;}
            if(n>=40){
                    r+="XL";
                    n-=40;}
            while(n>=10){
                    r+="X";
                    n-=10;}
            if(n>=9){
                    r+="IX";
                    n-=9;}
            if(n>=5){
                    r+="V";
                    n-=5;}
            if(n==4){
                    r+="IV";
                    n-=4;}
            while(n>=1){
                    r+="I";
                    n--;}
    }//Numero(int)
    Numero(String k){
        int vCor,vNext;
        r=k;
        for(int i=0;;i++){
            vCor=valore(k.charAt(i));
            if(i==k.length()-1){
                a+=vCor;
                break;}
            vNext=valore(k.charAt(i+1));
            if(vCor>=vNext)
                a+=vCor;
            else
                a-=vCor;
        }
    }

    static public int valore(char c){
        switch(c){
          case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
            default: return 0;
        }
    }

    public int getArabo(){
        return a;
    }
    public String getRomano(){
        return r;
    }
    }
/

I would like to create a function for getArabo and a function for getRoman, but I always receive errors.

我想为 getArabo 创建一个函数,为 getRoman 创建一个函数,但我总是收到错误消息。

I have tried with:

我试过:

CREATE OR REPLACE FUNCTION hr.converti (alfa IN varchar2)
   RETURN VARCHAR2
AS
   LANGUAGE JAVA
   NAME 'ROMAN.getArabo(java.lang.String) return java.lang.String';
/

but I receive the error:

但我收到错误:

select hr.converti('XII') from dual;

ORA-29540: ROMAN class does not exist

ORA-29540: ROMAN 类不存在

How could I fix this?

我怎么能解决这个问题?

采纳答案by Vincent Malgrat

Here are the things you need to correct to be able to use this java class:

以下是您需要更正才能使用此 Java 类的事项:

  1. Use static methods so that they can be called from Oracle without object instantiation
  2. Your source is named ROMAN, but your class is named Numero. You will reference the class when calling it from Oracle. Giving both the class and the source the same name will prevent confusion.
  3. java is strongly typed, you can't convert the method int getArabo()to:

    getArabo(java.lang.String) return java.lang.String
    
  1. 使用静态方法,以便无需对象实例化即可从 Oracle 中调用它们
  2. 您的源已命名ROMAN,但您的类已命名Numero。从 Oracle 调用该类时将引用该类。为类和源提供相同的名称可以防止混淆。
  3. java 是强类型的,您不能将该方法转换int getArabo()为:

    getArabo(java.lang.String) return java.lang.String
    

Here's a working example:

这是一个工作示例:

SQL> CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Roman" AS
  2  class Roman {
  3     public static int getArabo(java.lang.String x){
  4          return 0;
  5      }
  6  }
  7  /

Java created

SQL> CREATE OR REPLACE FUNCTION converti (alfa IN varchar2)
  2     RETURN NUMBER
  3  AS LANGUAGE JAVA NAME 'Roman.getArabo(java.lang.String) return int';
  4  /

Function created

SQL> select converti ('aaa') from dual;

CONVERTI('AAA')
---------------
              0


Additionally, you can use Oracle functions to convert a number to its character value in roman with TO_CHAR(but the TO_NUMBERfunction can't be used for the reverse operation):

此外,您可以使用 Oracle 函数将数字转换为其罗马字符值TO_CHAR(但该TO_NUMBER函数不能用于反向操作):

SQL> SELECT to_char(2013, 'RN') FROM dual;

TO_CHAR(2013,'RN')
------------------
         MMXIII