2017-01-23 99 views
-2

是否可以将oracle对象类型传递给j​​ava存储过程。这是迄今为止我所拥有的。将oracle对象类型传递给j​​ava存储过程

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

    } 
} 

Oracle过程:

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

甲骨文对象类型:

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

如果你的'ExObj'构造真的有两个参数,当SQL对象只有一个fieild?为什么你将'sqlType'传递给构造函数 - 应该是常量? –

+0

是的,它应该在那里,它应该将java类映射到oracle对象类型。它适用于我将java类返回给oracle的f.e. public static ExObj test1(String str){return new ExObj(str);}。所以我推测它应该在将对象类型传递给j​​ava时也有效。 –

回答

1

下面提到的问题与您的代码可能是原因。

1)您必须声明objecttype的变量,以便您可以使用它。您不能直接使用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; 

/

+0

你是说我只能将一个数组传递给存储过程? –

+1

不,我说你需要首先创建一个你的对象的类型,然后用它作为数据类型。我创建了一个类型的对象作为关联数组并使用它。我可以使用它作为嵌套表,但这会带来一些额外的事情,在我的代码中做 – XING

+0

你能用例子更新你的答案吗?我需要如何改变我调用java存储过程的过程?我是否需要使用java.sql.Array作为java参数呢? –

相关问题