简体   繁体   中英

Pass oracle object type to java stored procedure

Is is possible to pass oracle object type to java stored procedure. Here's what i have so far.

Java stored procedure:

create or replace and compile java source named Example as
import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;

public class Example {
    
    public static void test(ExObj obj) {
        System.out.println(obj.client);
    }
    
    public static class ExObj implements SQLData {

        public String client = "";

        public String sql_type = "EXAMPLE_OBJECT";

        public ExObj(String client, String sql_type) {
            this.client = client;

            this.sql_type = sql_type;
        }

        /* IMPLEMENTS SQLData */

        public void setSqlType(String sqlType) {
            sql_type = sqlType;
        }

        public void writeSQL(SQLOutput stream) throws SQLException {
            stream.writeString(this.client);
        }

        public String getSQLTypeName() {
            return sql_type;
        }

        public void readSQL(SQLInput stream, String sqlTypeName) throws SQLException {
            sql_type = sqlTypeName;

            this.client = stream.readString();
        }

    }
}

Oracle procedure:

CREATE OR REPLACE PROCEDURE example(ex_obj in example_object)
AS LANGUAGE JAVA
NAME 'Example.test(java.sql.Struct)';

Oracle object type:

create or replace type example_object as object(
  client varchar2(40)
)

Script to call procedure:

declare
  l_output DBMS_OUTPUT.chararr;
  l_lines  INTEGER := 1000;
  
  l_obj example_object;
begin
  DBMS_OUTPUT.enable(1000000);
  DBMS_JAVA.set_output(1000000);
  
  l_obj := example_object('client');
  example(l_obj);

  DBMS_OUTPUT.get_lines(l_output, l_lines);

  FOR i IN 1 .. l_lines LOOP
    -- Do something with the line.
    -- Data in the collection - l_output(i)
    DBMS_OUTPUT.put_line(l_output(i));
  END LOOP;
end;

When running script i get:

ORA-29531: no method test in class Example

ORA-06512: at "EXAMPLE", line 1

ORA-06512: at line 17

I presume it's because oracle object_type doesn't map to java class correctly. Is there a way to do this and if so what am i doing wrong?

The below mentioned issue with your code can be the reason.

1) You must declare a varaible of type of your object so that you can use it. You cannot directly use object as a datatype . See below :

Demo:

create or replace type example_object as object(
  client varchar2(40)
  );

 /

--Created a type of the object
create or replace type x is table of example_object;

/


 CREATE OR REPLACE PROCEDURE example(ex_obj in X)
    AS
  begin

    DBMS_OUTPUT.put_line('Example.test(java.sql.Struct');
    DBMS_OUTPUT.put_line('Inside the Procedure');
    DBMS_OUTPUT.put_line (ex_obj(1).client);

    end;
  /

    DECLARE
       l_output   DBMS_OUTPUT.chararr;
       l_lines    INTEGER := 1000;

       l_obj      x := x();      

    BEGIN
       DBMS_OUTPUT.enable (1000000);
       --DBMS_JAVA.set_output (1000000);

       --You need to mention the position where the record will be saved in table.
       l_obj.extend(); 
       l_obj(1) := example_object('client1');

       example (l_obj);

       DBMS_OUTPUT.put_line (l_obj(1).client);

       DBMS_OUTPUT.get_lines (l_output, l_lines);

       FOR i IN 1 .. l_lines
       LOOP
          -- Do something with the line.
          -- Data in the collection - l_output(i)
          DBMS_OUTPUT.put_line (l_output (i));
       END LOOP;
    END;

/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM