簡體   English   中英

將oracle對象類型傳遞給j​​ava存儲過程

[英]Pass oracle object type to java stored procedure

是否可以將 oracle 對象類型傳遞給 java 存儲過程。 這是我到目前為止所擁有的。

Java存儲過程:

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();
        }

    }
}

甲骨文程序:

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

Oracle 對象類型:

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

調用過程的腳本:

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;

運行腳本時,我得到:

ORA-29531: 類示例中沒有方法測試

ORA-06512:在“示例”,第 1 行

ORA-06512:在第 17 行

我認為這是因為 oracle object_type 沒有正確映射到 java 類。 有沒有辦法做到這一點,如果是這樣,我做錯了什么?

下面提到的代碼問題可能是原因。

1) 您必須聲明一個object type的變量,以便您可以使用它。 您不能直接使用object作為datatype 見下文 :

演示:

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;

/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM