Java function return array of number

Hi,
I have a Java method that returns a BigDecimal[]. Already loaded it into Oracle. Now, how can I write the call spec?
I tried creating a PL/SQL table type, but I either I'm missing something or I'm doing this wrong:
CREATE OR REPLACE TYPE TRANS_TAB AS TABLE OF NUMBER;Then, the function:
FUNCTION FN_TRANS(id NUMBER, name VARCHAR2, amount NUMBER)
    RETURN TRANS_TAB AS LANGUAGE JAVA
    NAME 'compTrans(int, java.lang.String, java.math.BigDecimal) return java.math.BigDecimal[]';Executing this gives an error:
Error: ORA-00932: inconsistent datatypes: expected a return value that is an instance of a user defined Java class convertible to an Oracle type got an object that could not be converted
Question, how do we create the Java function with return value? Thank you. :)

Hi,
I found this example on Asktom: http://asktom.oracle.com/pls/asktom/f?p=100:11:3258786576394300::::P11_QUESTION_ID:986230504001
Looks like you have to use Oracle.sql.ARRAY and to conversion on the Java side.
Hopes this will help.
Herald ten Dam
Superconsult.nl

Similar Messages

  • Stored Procedure: Function returns Array

    I was just wondering if following scenario is possible in a stored procedure.
    I have a package, which includes 4 different functions and 1 procedure. Procdure uses ref cursor to return values in my reporting tool. What I am trying to do is to write a function which takes input and return array of varchar. Something like..
    Package ABC
    Function student_classes (student_no in Varchar2) return [array]
    begin
    select class_nbr into [array]
    from student_table
    where student_nbr = student_no;
    return [array];
    end student_classes;
    procedure students_info (rpt_cursor IN OUT rpt_type)
    begin
    OPEN rpt_cursor FOR
    select
    student_name,
    student_nbr,
    student_address,
    student_classes (student_nbr) --returns array
    from student_table;
    end student_info; --end of procedure
    end abc; --end of pacakge.
    I used [array], as I am ont sure how to define (syntax) it in spec part of package or in body. I will really appreciate if someone could help me with this function.
    Thank you

    Thanks for the reply Tonguc Y.
    I declared an array in my package declaration
    CREATE OR REPLACE PACKAGE rpt_TESTING_pkg
    AS TYPE rpt_type IS REF CURSOR;
         TYPE DIAGARRAY IS VARRAY(20) OF VARCHAR2(6);
         FUNCTION DIA_CODES (MNO IN VARCHAR2) RETURN DIAGARRAY;
         PROCEDURE member (rpt_cursor IN OUT rpt_type);
    END rpt_TESTING_pkg;
    but then when I try to use it in my function it gives me error that I should declare DIAGARRAY.
    CREATE OR REPLACE PACKAGE BODY rpt_TESTING_pkg
    as
    FUNCTION DIA_CODES (MNO IN VARCHAR2)
                   RETURN DIGARRAY AS diags DIGARRAY;
    begin
    <sql block>
    end dia_codes;
    end rpt_TESTING_pkg;
    Any suggestions.

  • Call stored function return array

    Hi all,
    I have a function as follow:
    create or replace TYPE string_table IS TABLE OF VARCHAR2(2000);
    create or replace TYPE ARRAYTYPE is VARRAY(20) OF VARCHAR2(30);
    create or replace FUNCTION getEmpArray(s varchar2, t varchar2, st string_table) RETURN ARRAYTYPE AS
    l_data ARRAYTYPE := ARRAYTYPE();
    BEGIN
    l_data.extend; l_data(l_data.count) := s; l_data.extend; l_data(l_data.count) := t; l_data.extend; l_data(l_data.count) := st(1); RETURN l_data;
    END;
    I want to call this function by StoredFunctionCall
    code:
    StoredFunctionCall fun = new StoredFunctionCall();
    fun.setProcedureName("getEmpArray".toUpperCase());
    Object[] arr = new Object[]{"aa", "fgfg", "bbb"};
    ArrayDescriptor arrDescriptor =
    ArrayDescriptor.createDescriptor("string_table".toUpperCase(),
    connection);
    ARRAY arrayToPass = new ARRAY(arrDescriptor, connection, arr);
    fun.addUnamedArgumentValue("a");
    fun.addUnamedArgumentValue("b");
    fun.addUnamedArgumentValue(arrayToPass);
    fun.setResult("FUNCTION_RESULT"); // for get result by this name
    Vector<DatabaseRecord> list = session.executeSelectingCall(fun);
    But Exception
    PLS-00382: expression is of wrong type
    ORA-06550: line 1, column 7:
    PL/SQL: Statement ignored
    Error Code: 6550
    Call: BEGIN ? := GETEMPARRAY(?, ?, ?); END;
         bind => [=> FUNCTION_RESULT, a, b, oracle.sql.ARRAY@21fbc01]
    Please help me
    Edited by: fbg on 21:52 26-04-2010

    A few issues,
    1 - JDBC does not support the PLSQL TABLE type, you must use a VARRAY type, or wrap the TABLE function call in a function that takes a VARRAY.
    TopLink also has support for PLSQL types in its PLSQLStoredProcedureCall class, but no support is currently offered for StoredFunctions.
    You can't pass the VARRAY type for the TABLE argument.
    2 - Your function returns a VARRAY, so you need to define this type in the StoredFunctionCall result.
    We don't currently expose the API to set a Array type for the result, so you would need to access the call's first parameter directly, or use a StoredProcedureCall (and convert your function to a procedure).
    You may also wish to investigate returning a cursor from a stored procedure instead of the varray.
    You could also access the JDBC connection directly and perform the call using JDBC code.
    Feel free to log these issues in EclipseLink.
    James : www.eclipselink.org

  • A java function involving arrays

    I need to write a simple java function that checks whether or not the elements in an array first increase up to a certain point and then decrease.
    A[0] < A[3] < A[4] < ... < A[k] > A[k+1] > A[k+2] > ... > A[A.length-1]
    I have no idea how to do this

    coo8 wrote:
    not a clue just i know i need to use reccursion.Because you think it's necessary? Or because the assignment requires it?
    for ( int i = 0; i<n; i++)
    }Okay, great. That says "Do {nothing} for each element of the array." So what do you want to do for each element of the arrary?
    And I still don't understand exactly what you're supposed to do. Determine whether an array first increases element by element and then decreases element by element to the end? Can it turn around at any element? Or at a specified index? Or a specified value? Does each element have to be larger/smaller than the previous by a certain amount, or just larger/smaller?
    The more clearly you explain your problem, the better the help you'll get.

  • Db function returning three db number columns...

    Hi ,
    Is it possible a db function to return three number columns fetched from a table...??
    If for example , there is a db function such as...:
    function x
    return number
    is
    begin
    select number_col_1 , number_col_2 , number_col_3
    into var1 , var2 , var3
    from table_a
    where <a_condition>;
    return <the_three_columns_selected>;
    end;
    so as in a select statement should write:
    select a , b , x from a_table
    many thanks ,
    Simon

    Alternatively you can return a SQL collection:
    SQL> create or replace function x (empno integer)
      2     return sys.dbms_debug_vc2coll
      3  is
      4     var1   emp.job%type;
      5     var2   emp.sal%type;
      6     var3   emp.deptno%type;
      7  begin
      8     select job, sal, deptno
      9       into var1, var2, var3
    10       from emp
    11      where empno = x.empno;
    12
    13     return sys.dbms_debug_vc2coll (var1, var2, var3);
    14  end;
    15  /
    Function created.
    SQL>
    SQL> select empno, ename, x(empno) x from emp
      2   where empno in (7788,7900)
      3  /
         EMPNO ENAME      X
          7788 SCOTT      DBMS_DEBUG_VC2COLL('ANALYST', '3000', '20')
          7900 JAMES      DBMS_DEBUG_VC2COLL('CLERK', '950', '30')

  • Function returns array

    hi
    can anyone help me out to create a function that accepts a comma separated string and return the value as array, so that i can use it in a query as follows
    select * from emp where emp_num in ( func_array('12,10,15,16,8'));
    here the emp_num is passed as a string '12,10,15,16,8'
    reagrds
    Benn

    Hi,
    SQL> create or replace type T_Id_List AS TABLE OF INTEGER
      2  /
    Type created.
    SQL> create or replace function Func_Array(
      2    pStr VARCHAR2
      3  ) return T_Id_List AS
      4    vRes T_Id_List;
      5  begin
      6    execute immediate
      7      'select ' || replace(pStr, ',', ' from dual union all select ') || ' from dual'
      8      bulk collect into vRes;
      9    return vRes;
    10  end;
    11  /
    Function created.
    SQL> select * from table(Func_Array('12,10,15,16,8,7369,7499'));
    COLUMN_VALUE
              12
              10
              15
              16
               8
            7369
            7499
    7 rows selected.
    SQL> select * from emp where empno in (select * from table(func_array('12,10,15,16,8,7369,7499')));
         EMPNO ENAME      JOB              MGR HIREDATE          SAL       COMM
        DEPTNO
          7369 SMITH      CLERK           7902 17.12.1980        800
            20
          7499 ALLEN      SALESMAN        7698 20.02.1981       1600        300
            30Regards,
    Dima

  • JavaFX : How to call java function that returns hashtable and manipulate

    I have a requirement to {color:#0000ff}create a java object in JavaFX script code{color}. Then call a java function using the created java object. The java function returns hashtable. Then traverse through each element of hashtable. Finally I need to create a similar structure in JavaFX.

    If you need to use a Java class that uses generics you need to take special steps. Since JavaFX does not support generics you need to create a java wrapper to hide the calls that use generics and call the wrapper class from FX.

  • Can Evaluate function return object type

    Hi
    Evaluate function can be used to call db functions in OBIEE. I have a function which returns an object ( pl/sql table).
    Created a simple report in Oracle Answers and added following in one of the columns
    evaluate( 'get_ccid(%1)' as t_ccid , @{p_request})
    When I try to run this in Oracle answers, getting syntax error. If same function returns varchar or number it works well.

    evaluate( 'get_ccid(%1)' as t_ccid , @{p_request})Eakta, You syntax seems to be wrong here. What type of data your presentation variable contains here ?? You are saying its working fine with Number datatype..so can you try..somthing like below with some default value..
    EVALUATE('get_ccid(%1)',@{p_request}{2})
    OR
    EVALUATE('get_ccid(%1)' as varchar(250),@{p_request}{ABC})
    Also, refer
    Syntax for Evaluate function in OBIEE
    http://108obiee.blogspot.com/2009/04/using-presentation-variable-from-first.html
    Hope its useful

  • Trying to fetch a value in a java function and returning the array.

    hello....I am trying to fetch a value in a java function and returning a array......I already write the pl/sql function which is working fine....but i think i m lost......when i run it through the jsp it shows me error........pls help
    java code:=
    public String [] viewx(String bid) throws SQLException, Exception {
    String [] values;
    try {
    CallableStatement cstmt = null;
    String SQL = "{?=call vi_dis.v_dis(?)}";
    cstmt = con.prepareCall(SQL);
    cstmt.registerOutParameter(1,Types.ARRAY);
    cstmt.setString(2, bid);
    cstmt.execute();
    Array simpleArray = cstmt.getArray(1);
    values = (String [])simpleArray.getArray();
    cstmt.close();
    } catch (SQLException sqle) {
    error = "SQLException: Could not execute the query.";
    throw new SQLException(error);
    } catch (Exception e) {
    error = "An exception occured while retrieving emp.";
    throw new Exception(error);
    return values;
    pl/sql function
    create or replace package vi_dis
    as
    function v_dis(vbid IN student.bid%type) return stuarray ;
    end;
    create or replace
    package body vi_dis
    as
    function v_dis(vbid IN student.bid%type) return stuarray
    is
    l_stu stUarray :=stuarray();
    cursor c_sel
    is
    SELECT CNAME
    FROM COURSE C,ENROLL E
    WHERE C.CID=E.CID
    AND E.BID=vbid;
    BEGIN
    OPEN c_sel;
    FETCH c_sel BULK COLLECT INTO l_stu;
    l_stu.extend;
    CLOSE c_sel;
    RETURN l_stu;
    EXCEPTION
    WHEN NO_DATA_FOUND
    THEN DBMS_OUTPUT.PUT_LINE('NO RESULT AVAILABLE');
    CLOSE c_sel;
    END;
    END;
    /

    BalusC wrote:
    It is comparing the selected value against the List<SelectItem> returned by getSetoresOrigem() as it is during the apply request values phase of the form submit request.Ok. That's what I supposed JSF was doing.
    BalusC wrote:
    If the selected value isn't in there, then you will get this error.I can understand this, but is this right? As I said, the old value isn't really there because I changed the list values to new ones. But the new value (the value of fSetorOrigem ) corresponds to a value that exist in the new list items, so a valid value. So JSF is not considering that I also changed the list, not just the value. It is comparing the new value with the old list, not the new one. Acting like this JSF is making the page looks like a static HTML page, not a dynamic one. If I can't change the list and the value, what's the point of that? In my point of view I'm not doing anything wrong, I'm not violating any JSF rules.
    Marcos

  • How to return array of String to a java program.! ??

    Hi All,
    I am new to JNI programming. so trying with simple example. I am having one problem. I have one array of String in my Java program. I want to reverse all the elements in this array and want back this new array in the same form i.e. array of string. So how can I do this ??
    Please guide me.....
    The function of defination of array in my java code is
    public native String[] getReverseStringArray(String[] arrString);
    where as my array is like:
    String[] arrStr = {"one", "two", "three", "four", "��������","�c��","�J�^�J�i"};
    and I am calling function in C file like:
    arrStr = employee.getReverseStringArray(arrStr);
    and the function defination in my c file is
    JNIEXPORT jobjectArray JNICALL
    Java_Emp_getReverseStringArray (JNIEnv *env, jobject obj, jobjectArray arr)
    // some code will come here......
    So can anybody tell me how to do this ??
    Thanx in advance...
    Pandurang
    [email protected]

    Hi,
    this is quite simple. Here an example function that works like this:
    JNIEXPORT jobjectArray JNICALL Java_cadagent_ugopen_UgAssembly_jni_1getStructure
    (JNIEnv *env, jclass cls, jstring part)
    jobjectArray retvals = NULL;
    jstring j_val = NULL;
    int child_count = 5;
    /* initialize return array */
    j_val = (*env)->NewStringUTF(env, "");
    retvals = (*env)->NewObjectArray(env, child_count, (*env)->FindClass(env, "java/lang/String"), j_val);
    for (i=0;i<child_count;i++)
    j_val = (*env)->NewStringUTF(env, "some text");
    (*env)->SetObjectArrayElement(env, retvals,i, j_val);
    return retvals;
    Hope this answers you question.

  • Return array of varchar from oracle to java

    Hi,
    I am trying to return an array of varchar from database to java. However, when I print the values in java, it prints hexadecimal values like this.
    0x656E7472792031
    0x656E7472792032
    0x656E7472792033
    0x656E7472792034
    I am using oracle 9.2.0.5.0 and jdk 1.4
    This happens only when the NLS character set of the database is WE8MSWIN1252 (default character set).
    It works fine on databases with UTF8 and US7ASCII character sets.
    Any leads to the reason behind this behaviour is greatly aprpeciated.
    Here's the code snippet...
    <code>
    //USER DEFINED TYPE
    create or replace type SimpleArray
     as table of varchar2(30)
    </code>
    <code>
    // FUNCTION RETURNING USER DEFINED TYPE
    create or replace function getSimpleArray return SimpleArray
     as
     l_data simpleArray := simpleArray();
     begin
      for i in 1 .. 10 loop
          l_data.extend;
          l_data(l_data.count) := 'entry ' || i;
        end loop;
        return l_data;
      end;
    </code>
    <code>
    // JAVA METHOD
    public static void main(String[] args)
         try {
              Class.forName("oracle.jdbc.driver.OracleDriver");
              String url = "jdbc:oracle:thin:@localhost:1521:db1252";
              Connection conn = DriverManager.getConnection(url, "ring", "ring");
              CallableStatement stmt = conn.prepareCall("{? = call "
                             + "getSimpleArray" + "}");
              Array rs = null;
              ResultSet rsltSet = null;
              stmt.registerOutParameter(1, Types.ARRAY, "SIMPLEARRAY");
              stmt.execute();
              rs = stmt.getArray(1);
              String[] values = (String[]) rs.getArray();
              System.out.println(rs.getBaseTypeName());
              System.out.println(values.length);
              for (int i = 0; i < values.length; i++) {
                   System.out.println(values);
              rsltSet = rs.getResultSet();
         } catch (Exception e) {
              System.err.println("Got an exception! ");
              System.err.println(e.getMessage());
    </code>
    Thanks,
    Vivek

    Actually, check nls_charset12.zip. It looks like 9.2.0.5 driver contains only the *.zip version. Make sure that the file exists in the directory pointed by the CLASSPATH. Use full path, do not use %ORACLE_HOME%.
    I have reproduced your problem without nls_charset12.zip and I could not reproduce it with the file in the class path. Double-check how classpath is set in your environment. Maybe it is overriden by some configuration or batch file.
    -- Sergiusz

  • Using jdbc StoredProcedure to return from a function an array of objects

    I am trying to call a stored procedure(function) using jdbcTemplates with no inputs which returns a list of objects.
    I previously used the same function returning a cursor and used the following example:
    http://www.saarathi.com/wiki/Processing_JDBC_ResultSet_from_an_Oracle_stored_procedure_with_Spring#Calling_the_stored_procedure_and_function_from_Spring_JDBC_DAO
    which worked fine.
    I now want to return the same objects which relates to a table but in a list and have tried to follow the example in
    http://forum.springframework.org/archive/index.php/t-10042.html
    But have had no success and did not understand the example GetPersonProc.
    I dont understand why it works for the cursor and not a list.
    I would very much appreciate some help.

    Hi
    Can anybody help me with the code of how to return a resultset from a web service. i have put the resultset data's in an object array and tried to return it, but in the client side no data comes ,,, i mean it is printed as null.... and plz tell me where to specify the return type of a object in the wsdl file....
    thanks..

  • How to make a function return number(10,0) data type (ORACLE 10g)?

    With 10g, how to make a function return number(10,0) data type?
    here is the function, it returns a number type :
    create or replace FUNCTION Get_portfolio_Id3 (p_HistObjTable In Varchar2,p_LHISTOBJID IN NUMBER) RETURN view_cpu_STD_Asset.LPORTFOLIOITEMID%Type IS
    v_Id view_cpu_STD_Asset.LPORTFOLIOITEMID%Type;
    BEGIN
    If p_HistObjTable ='amPortfolio'
    then v_Id:=p_LHISTOBJID ;
    elsIf p_HistObjTable = 'amComputer' then
    select litemid into v_Id from smcdba.amComputer c where c.LCOMPUTERID=p_LHISTOBJID ;
    else v_Id:=-99;
    End If;
    RETURN v_Id;
    END Get_portfolio_Id3;
    Thanks.
    Message was edited by:
    user631701

    create or replace FUNCTION Get_portfolio_Id3 (p_HistObjTable In Varchar2,p_LHISTOBJID IN NUMBER) RETURN view_cpu_STD_Asset.LPORTFOLIOITEMID%Type IS
    v_Id view_cpu_STD_Asset.LPORTFOLIOITEMID%Type;
    BEGIN
    If p_HistObjTable ='amPortfolio'
    then v_Id:=p_LHISTOBJID ;
    elsIf p_HistObjTable = 'amComputer' then
    select litemid into v_Id from smcdba.amComputer c where c.LCOMPUTERID=p_LHISTOBJID ;
    else v_Id:=-99;
    End If;
    RETURN round(v_Id);
    END Get_portfolio_Id3;

  • Whee the function return the value store in java

    in c if i write this program
    int add()
    return 1;
    main()
    add();
    if i run this program it give s error that lvalue required
    but in java it works fine though the function return some value

    in c if i write this program
    int add()
    return 1;
    main()
    add();
    if i run this program it give s error that lvalue
    requiredYou mean, if you try to run it as a C program it gave an error?
    Then take your question to a "C" discussion forum. This is Java.
    >
    but in java it works fine though the function return some value
    In Java this does NOT work fine.
    It definitely does not compile! So you cannot run it.

  • Please help how to get return array from rpg program on java code?

    Hi
    I have created a rpg program that returns 2 parameter 1 is the id and another one is list of array, when I called this program I passed two programparameter from my java code (see the code below) but when i checked what value would be return it is returned only first value of array. how will i get all array values ?
    please suggest me regarding this issues I amn't so much aware on java & AS400.
    try
    ProgramParameter[] parmList = new ProgramParameter[2];
    AS400Text p1 = new AS400Text(10);
    AS400Text p2 = new AS400Text(30);
    try
    parmList[0] = new ProgramParameter(10);
    parmList[1] = new ProgramParameter(30);
    parmList[0].setParameterType(ProgramParameter.PASS_BY_REFEREN CE);
    parmList[1].setParameterType(ProgramParameter.PASS_BY_REFEREN CE);
    parmList[0].setInputData(p1.toBytes("Pune"));
    parmList[1].setInputData(p2.toBytes(" "));
    catch(Exception ex)
    ProgramCall pgm = new ProgramCall(o);
    pgm.setProgram("/QSYS.LIB/XXX/XXX.PGM",parmList);
    if (pgm.run())
    byte s[] = parmList[1].getOutputData(); // HERE I got only first value of returning array.
    parmList[1].getOutputDataLength();
    //String sts = ((String) (new AS400Text(10,o).toBytes(s[0])));
    else
    AS400Message[] messageList = pgm.getMessageList();
    for (int msg = 0; msg < messageList.length; msg++) {
    catch(Exception ex)
    AS400Message[] messageList = null;
    finally
    o.disconnectAllServices();
    Reply With Quote

    Try this :
    try
    ProgramParameter[] parmList = new ProgramParameter[2];
    AS400Text p1 = new AS400Text(10);
    AS400Text p2 = new AS400Text(30);
    AS400Array arrP2 = new AS400Array(p2, 4);
    try
    parmList[0] = new ProgramParameter(10);
    parmList[1] = new ProgramParameter(30);
    parmList[0].setParameterType(ProgramParameter.PASS_BY_REFEREN CE);
    parmList[1].setParameterType(ProgramParameter.PASS_BY_REFEREN CE);
    parmList[0].setInputData(p1.toBytes("Pune"));
    parmList[1].setInputData(arrP2.toBytes({"","","",""}));
    catch(Exception ex)
    ProgramCall pgm = new ProgramCall(o);
    pgm.setProgram("/QSYS.LIB/XXX/XXX.PGM",parmList);
    if (pgm.run())
         Object[] objArr =  (Object [])arrP2.toObject( parmList[1].getOutputData() );
         for(int i =0; i<objArr.length;i++){
                System.out.println( " SKU " + i +" : " + objArr.toString());
    else
    AS400Message[] messageList = pgm.getMessageList();
    for (int msg = 0; msg < messageList.length; msg++) {
    catch(Exception ex)
    AS400Message[] messageList = null;
    finally
    o.disconnectAllServices();

Maybe you are looking for