是否可以將oracle對象類型傳遞給java存儲過程。這是迄今爲止我所擁有的。將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();
}
}
}
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類。有沒有辦法做到這一點,如果是的話,我做錯了什麼?
如果你的'ExObj'構造真的有兩個參數,當SQL對象只有一個fieild?爲什麼你將'sqlType'傳遞給構造函數 - 應該是常量? –
是的,它應該在那裏,它應該將java類映射到oracle對象類型。它適用於我將java類返回給oracle的f.e. public static ExObj test1(String str){return new ExObj(str);}。所以我推測它應該在將對象類型傳遞給java時也有效。 –