How to return an array from Java to PL/SQL?

后端 未结 2 2013
有刺的猬
有刺的猬 2020-12-19 10:33

I have no problems to pass numbers and strings back and forth from PL/SQL to Java, but how do I pass arrays ? I\'m calling Java from PL/SQL - not other way round.

相关标签:
2条回答
  • 2020-12-19 11:01

    I dont know about the pl/sql thing but what u can do is process the result from PL/SQL and insert the result into a set method and then later u can get it.

    0 讨论(0)
  • 2020-12-19 11:09
    /* The type has to be SQL type so that it is also visible for Java. */
    create or replace type widgets_t is table of varchar2(32767);
    /
    
    create or replace and compile java source named "so19ja" as
    import java.lang.*;
    public class so19ja {
        public static String[] get_widgets_as_array() {
            String[] widgets = new String[]{"foo", "bar", "zoo"};
            return widgets;
        }
    
        public static java.sql.Array array_wrapper(
            String typeName,
            Object elements
        ) throws java.sql.SQLException {
            oracle.jdbc.OracleDriver ora = new oracle.jdbc.OracleDriver();
            java.sql.Connection conn = ora.defaultConnection();
            oracle.jdbc.OracleConnection oraConn =
                (oracle.jdbc.OracleConnection)conn;
            /* Yeah - typeName have to be UPPERCASE, really. */
            java.sql.Array arr = 
                oraConn.createARRAY(typeName.toUpperCase(), elements);
            return arr;
        }
    
        public static java.sql.Array get_widgets_as_array_wrapped()
        throws java.sql.SQLException {
            return array_wrapper("widgets_t", get_widgets_as_array());
        }
    };
    /
    show errors java source "so19ja"
    
    create or replace function get_widgets_as_array return widgets_t as
    language java name 'so19ja.get_widgets_as_array_wrapped() return java.sql.Array';
    /
    show errors
    
    declare
      widgets constant widgets_t := get_widgets_as_array;
    begin
      for i in widgets.first .. widgets.last loop
        dbms_output.put_line('widgets(' || i || ') = ' || widgets(i));
      end loop;
    end;
    /
    

    Prints:

    widgets(1) = foo
    widgets(2) = bar
    widgets(3) = zoo
    
    PL/SQL procedure successfully completed.
    

    See also: How to create an oracle.sql.ARRAY object?

    0 讨论(0)
提交回复
热议问题