Home » Developer & Programmer » JDeveloper, Java & XML » Wrapping a recursive function (11.2.0.3.0)
Wrapping a recursive function [message #620017] Sun, 27 July 2014 17:34 Go to next message
Amine
Messages: 264
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 #620018 is a reply to message #620017] Sun, 27 July 2014 18:43 Go to previous messageGo to next message
BlackSwan
Messages: 22843
Registered: January 2009
Senior Member
[oracle@localhost ~]$ oerr ora 29531
29531, 00000, "no method %s in class %s"
// *Cause: An attempt was made to execute a non-existent method in a
//          Java class.
// *Action: Adjust the call or create the specified method.
[oracle@localhost ~]$ 


It means root cause & fix is in JAVA, making this post OFF TOPIC for this forum.
Re: Wrapping a recursive function [message #620019 is a reply to message #620018] Sun, 27 July 2014 19:13 Go to previous messageGo to next message
Solomon Yakobson
Messages: 2036
Registered: January 2010
Senior Member
BlackSwan wrote on Sun, 27 July 2014 19:43
It 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

Re: Wrapping a recursive function [message #620060 is a reply to message #620019] Mon, 28 July 2014 08:15 Go to previous messageGo to next message
Amine
Messages: 264
Registered: March 2010
Senior Member

Thanks Solomon,
Efficient because of
if (n <= 2)
or because of :
import java.sql.*;


Thanks,

Amine
Re: Wrapping a recursive function [message #620153 is a reply to message #620060] Tue, 29 July 2014 08:58 Go to previous message
Solomon Yakobson
Messages: 2036
Registered: January 2010
Senior Member
Because of if (n <= 2).

SY.
Previous Topic: Grants for loadjava
Next Topic: Oracle REFCURSOR issue. Front end app is slow
Goto Forum:
  


Current Time: Wed Oct 01 09:55:24 CDT 2014

Total time taken to generate the page: 0.09961 seconds