Wrapping a recursive function [message #620017] |
Sun, 27 July 2014 17:34 |
Amine
Messages: 376 Registered: March 2010
|
Senior Member |
|
|
Hi all
SQL> create or replace and compile java source named "MyClass" as
2 public class MyClass {
3 public static int facto(int n) {
4 if (n <= 1) {
5 return 1;
6 }
7 return n * facto(n-1);
8 }
9 };
10 /
Java created.
SQL>
SQL> create or replace function facto_java(n in number) return number as
2 Language java
3 name 'MyClass.facto(java.lang.Number) return java.lang.Number';
4 /
Function created.
SQL>
SQL> select facto_java(5) from dual;
select facto_java(5) from dual
*
ERROR at line 1:
ORA-29531: no method facto in class MyClass
Is that means that we can not wrap a recursif function(procedure) ?
Or, what am I missing ?
Thanks in advance,
Amine
|
|
|
|
Re: Wrapping a recursive function [message #620019 is a reply to message #620018] |
Sun, 27 July 2014 19:13 |
Solomon Yakobson
Messages: 3303 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
BlackSwan wrote on Sun, 27 July 2014 19:43It means root cause & fix is in JAVA, making this post OFF TOPIC for this forum.
Not exactly. Although it is somewhat Java related, issue is in PL/SQL function declaration:
SQL> create or replace and compile java source named "MyClass" as
2 import java.sql.*;
3 public class MyClass {
4 public static int facto(int n) {
5 if (n <= 1) {
6 return 1;
7 }
8 return n * facto(n-1);
9 }
10 };
11 /
Java created.
SQL> create or replace function facto_java(n in number) return number as
2 Language java
3 name 'MyClass.facto(java.lang.Number) return java.lang.Number';
4 /
Function created.
SQL> select facto_java(5) from dual
2 /
select facto_java(5) from dual
*
ERROR at line 1:
ORA-29531: no method facto in class MyClass
SQL> create or replace function facto_java(n in number) return number as
2 Language java
3 name 'MyClass.facto(int) return int';
4 /
Function created.
SQL> select facto_java(5) from dual
2 /
FACTO_JAVA(5)
-------------
120
SQL>
To OP: It would be a bit more efficient this way:
create or replace and compile java source named "MyClass" as
import java.sql.*;
public class MyClass {
public static int facto(int n) {
if (n <= 2) {
return n;
}
return n * facto(n-1);
}
};
/
SY.
[Updated on: Sun, 27 July 2014 19:27] Report message to a moderator
|
|
|
|
|