Chamando um classe Java do PL/SQL (Oracle)

3 respostas
N

Bom dia!

Estou tentando chamar uma classe java do PL/SQL, mas ocorre um erro de inconsistência de tipo. Acredito que seja porque passo como parâmetro um array de number e o java espera receber double. Apesar da bibliografia dizer que estes tipos são compatíveis.
Alguém pode me ajudar, sabe o que pode ser de errado?

Abaixo segue o código
CREATE OR REPLACE TYPE NumberArrayType AS TABLE OF number

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED calcula_tir AS

public class tir

{

static public double getIRR(final double [] cashFlows, final double estimatedResult)

{

double result = Double.NaN;

if (cashFlows != null && cashFlows.length > 0)

{//check if business startup costs is not zero:

if (cashFlows[0] != 0.0)

{

final double noOfCashFlows = cashFlows.length;

double sumCashFlows = 0.0;

//check if at least 1 positive and 1 negative cash flow exists:

int noOfNegativeCashFlows = 0;

int noOfPositiveCashFlows = 0;

for (int i = 0; i < noOfCashFlows; i++)

{

sumCashFlows += cashFlows[i];

if (cashFlows[i] > 0)

{

noOfPositiveCashFlows++;

}

else if (cashFlows[i] < 0)

{

noOfNegativeCashFlows++;

}

}
if (noOfNegativeCashFlows > 0 && noOfPositiveCashFlows > 0) 
    {   //at least 1 negative and 1 positive cash flow available?
        //set estimated result:
        double irrGuess = 0.1; //default: 10%
        if (estimatedResult != Double.NaN) 
        {
            irrGuess = estimatedResult;
            if (irrGuess <= 0.0) irrGuess = 0.5;
         }
         //initialize first IRR with estimated result:
        double irr = 0.0;
        if (sumCashFlows < 0) 
        { //sum of cash flows negative?
           irr = -irrGuess;
        } 
    else 
        { //sum of cash flows not negative
           irr = irrGuess;
         }

  //iteration:
  final double minDistance = .0000001; //the smaller the distance, the smaller the interpolation error
  final double cashFlowStart = cashFlows[0]; //business startup costs
  final int maxIteration = 50;
  boolean wasHi = false;
  double cashValue = 0.0; //cash value (Kapitalwert)
  
  for (int i = 0; i <= maxIteration; i++) 
  {  //for each iteration
     //calculate cash value with current irr:
     cashValue = cashFlowStart; //init with startup costs
     
     for (int j = 1; j < noOfCashFlows; j++) 
     { //for each cash floe
      cashValue += cashFlows[j] / Math.pow(1.0 + irr, j);
     }//next cash flow

     if (Math.abs(cashValue) < 0.01) 
     { //cash value is nearly zero
     result = irr;
     break;
     }

     //adjust irr for next iteration:
     if (cashValue > 0.0) 
     { //cash value > 0 => next irr > current irr
           if (wasHi) { irrGuess /= 2; }
           irr += irrGuess;
           if (wasHi) {
               irrGuess -= minDistance;
               wasHi = false;
           }
     } 
     else { //cash value < 0 => next irr < current irr
          irrGuess /= 2;
          irr -= irrGuess;
          wasHi = true;
     }

      if (irrGuess <= minDistance) { //estimated result too small to continue => end calculation
         result = irr;
         break;
      }
             }//next iteration
          }//else: noOfNegativeCashFlows == 0 || noOfPositiveCashFlows == 0
      }//else: first cash flow is 0
  }//else: cashFlows unavailable
return result;

}//getIRR()

}
CREATE OR REPLACE function calcula_tir ( vArray in NumberArrayType , v_resultado number)

return number as

LANGUAGE JAVA NAME tir.getIRR( double [] , double) return double;

oracle.sql.ARRAY[]

create or replace

function move_arquivos( p_sourceFile in varchar2,p_destFile in varchar2 )

return number

as language java

name ManipulaFiles.moveArquivos( java.lang.String,java.lang.String ) return java.lang.String;
DECLARE

vArray NumberArrayType := NumberArrayType();

v_result number;

BEGIN

vArray.EXTEND(13);

vArray(1):= -5216.00;

vArray(2) := 525.00;

vArray(3) := 525.00;

vArray(4) := 525.00;

vArray(5) := 525.00;

vArray(6) := 525.00;

vArray(7) := 525.00;

vArray(8) := 525.00;

vArray(9) := 525.00;

vArray(10) := 525.00;

vArray(11) := 525.00;

vArray(12) := 525.00;

vArray(13) := 525.00;

v_result := calcula_tir(vArray,0.1);

DBMS_OUTPUT.PUT_LINE(‘tir=’||to_char(v_result));
END;

3 Respostas

dicabeca

coloca seu codigo dentro da tag Code!!!

N

Desculpe, mas não entendi o que tu quis dizer com tag Code. Não conheço quase nada de java e sou nova no grupo…

dicabeca

nao olha so a tag Code nao tem haver com o java e sim com o seu Post,no editor do assunto logo em cima de onde vc escreve tem varias tags para colocar em negrito “[B]” italico [I] e assim vai c vc ver em cima tem o “”,coloca o seu codigo no meio da tag para ele ficar assim na tela :

public class Mengaooo()
 {
    public String melhorDoMundo()
     {
           return "Flamengooooo!!!!!"
     }
 }

entendeu hehehe?

Criado 12 de março de 2008
Ultima resposta 12 de mar. de 2008
Respostas 3
Participantes 2