How to MAP nested table,associative array or varray to JAVA SOURCE in Oracle DB

自作多情 提交于 2021-02-11 15:45:53

问题


I need to pass a collection to a JAVA SOURCE in the oracle database.

This is the illustration of the current setup without collection where oracle varchar2 is mapped to java String and it is working fine

The code below:-

The java source:-

    import java.io.*;
     public class Test_java extends Object{
       public static String print_test(String test)
        throws Exception
        {
        try{
            System.out.println(test);
            return "UP";
        }
        catch(Exception e)
           {
                return "DOWN" + e.getMessage();
           }
       }
    }; 

The invoking function:-

      CREATE OR REPLACE FUNCTION FN_TEST_JAVA (p_test in varchar2)RETURN VARCHAR2 AS 
      LANGUAGE JAVA NAME 'Test_java.print_test(java.lang.String) return String';

The testing:-

       begin
       dbms_java.set_output(1000000);
       dbms_output.put_line(FN_TEST_JAVA('this is a java source test 2'));
       end;

The Output:-

       this is a java source test 2
       UP

So my question is in my function instead of VARCHAR2 can I pass an associative array, nested table, or varray??

In that case what would be the mapping in the java source ??

Current progress:-

I have created the java source in Db like below

         import java.io.*;
         public class Testarray_java extends Object{
           public static String print_test(String test[])
            throws Exception
            {
            try{
                for (int i = 0; i < test.length; i++)
                System.out.println("Element at index " + i + 
                                " : "+ test[i]);
                return "UP";
            }
            catch(Exception e)
               {
                    return "DOWN" + e.getMessage();
               }
           }
        };   

and created the function like below

    create type test_java as table of varchar2(4000);

    create or replace FUNCTION FN_TESTARRAY_JAVA (p_test in test_java)RETURN VARCHAR2 AS 
    LANGUAGE JAVA NAME 'Testarray_java.print_test(java.lang.String[]) return String';

Testing like below but it is not working

   declare
   pp test_java:=test_java();
   begin
   pp.extend;
   pp(pp.last):='san test 1';
   pp.extend;
   pp(pp.last):='san test 2';
   dbms_output.put_line(FN_TESTARRAY_JAVA(pp));
   end;

Getting the below error:-

  ORA-00932: inconsistent datatypes: expected a Java type at argument position 2 to which some Oracle 
  value can be converted got something else

来源:https://stackoverflow.com/questions/61674282/how-to-map-nested-table-associative-array-or-varray-to-java-source-in-oracle-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!