简体   繁体   English

在Oracle中调用Java存储过程

[英]Calling Java Stored Procedure in Oracle

I am trying to create a Java stored procedure for Oracle, following Example 7-2 Fibonacci Sequence . 我正在尝试按照示例7-2 Fibonacci Sequence的方式为Oracle创建Java存储过程。

I have gone through exactly the same process, but while executing the code 我经历了完全相同的过程,但是在执行代码时

SQL> VARIABLE n NUMBER
SQL> VARIABLE f NUMBER
SQL> EXECUTE :n := 7;
SQL> CALL fib(:n) INTO :f;

I get following response: 我得到以下回应:

anonymous block completed

Error starting at line 4 in command:
CALL fib(:n) INTO :f
Error report:
SQL Error: ORA-01008: not all variables bound
01008. 00000 -  "not all variables bound"
*Cause:    
*Action:

Can anyone help? 有人可以帮忙吗?

Oracle docs have a lot of coding errors in them (sometimes omissions, sometimes bugs)... anyway, I think the issue is with how that class got loaded. Oracle文档中有很多编码错误(有时会遗漏,有时会出现错误)...无论如何,我认为问题在于该类的加载方式。

Try loading the class like this: 尝试像这样加载类:

create or replace and compile java source named Fibonacci as
public class Fibonacci
{
  public static int fib (int n)
  {
    if (n == 1 || n == 2)
      return 1;
    else
      return fib(n - 1) + fib(n - 2);
  }
}

Make sure the function wrapper is valid and try again. 确保函数包装器有效,然后重试。

CREATE OR REPLACE FUNCTION fib (n NUMBER) RETURN NUMBER
AS LANGUAGE JAVA
NAME 'Fibonacci.fib(int) return int';

I got the correct output when I tried it. 我尝试时得到了正确的输出。

SQL> VARIABLE n NUMBER
SQL> VARIABLE f NUMBER
SQL> EXECUTE :n := 7;
PL/SQL procedure successfully completed
n
---------
7
SQL> CALL fib(:n) INTO :f;
Method called
n
---------
7
f
---------
13
SQL> PRINT f
f
---------
13

I don't know enough about the SQL Developer product and what's above might only work in SQL Plus. 我对SQL Developer产品了解不多,上面的内容可能仅适用于SQL Plus。

Try: 尝试:

SELECT fib(7) FROM dual;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM