从 Spring DAO 调用 Sql 过程

发布于 2024-10-12 18:47:09 字数 1148 浏览 4 评论 0原文

CREATE OR REPLACE TYPE NEW_ADDRS_OBJ
AS OBJECT (newAddress1 varchar2(20),
newAddress2 varchar2(20),
city varchar2(20),
state varchar2(20),
zip number(20))

create or replace type NEW_ADDRS_OBJ_ARRAY as table of NEW_ADDRS_OBJ

CREATE OR REPLACE TYPE ACCOUNT_OBJ
AS OBJECT (fname varchar2(20),
newAddress NEW_ADDRS_OBJ_ARRAY)

create or replace type ACCOUNT_OBJ_ARRAY as table of ACCOUNT_OBJ

现在,当调用该过程时,

    CREATE OR REPLACE PROCEDURE INSERT_NEW_ADDRESS ( account_array in ACCOUNT_OBJ_ARRAY ) AS
tempaddres NEW_ADDRS_OBJ_ARRAY;
begin
  for i in 1..account_array.count loop
   tempaddres := NEW_ADDRS_OBJ_ARRAY();
   tempaddres := account_array(i).newAddress;
  for j in 1..tempaddres.count loop
    insert into TEST_ACCOUNT (ACCOUNT,NEWADDRESS1 ,NEWADDRESS2 ,CITY ,STATE ,ZIP )
       values(account_array(i).fname,
        tempaddres(j).newAddress1,
       tempaddres(j).newAddress2,
       tempaddres(j).city,
        tempaddres(j).state,
         tempaddres(j).zip);
  end loop;
  end loop;
end;

我可以利用 arraydescriptor 来传递父数组 ACCOUNT_OBJ_ARRAY ,但是如何从 Spring 过程调用传递其中的数组(新地址对象的数组)?

CREATE OR REPLACE TYPE NEW_ADDRS_OBJ
AS OBJECT (newAddress1 varchar2(20),
newAddress2 varchar2(20),
city varchar2(20),
state varchar2(20),
zip number(20))

create or replace type NEW_ADDRS_OBJ_ARRAY as table of NEW_ADDRS_OBJ

CREATE OR REPLACE TYPE ACCOUNT_OBJ
AS OBJECT (fname varchar2(20),
newAddress NEW_ADDRS_OBJ_ARRAY)

create or replace type ACCOUNT_OBJ_ARRAY as table of ACCOUNT_OBJ

Now when calling the procedure

    CREATE OR REPLACE PROCEDURE INSERT_NEW_ADDRESS ( account_array in ACCOUNT_OBJ_ARRAY ) AS
tempaddres NEW_ADDRS_OBJ_ARRAY;
begin
  for i in 1..account_array.count loop
   tempaddres := NEW_ADDRS_OBJ_ARRAY();
   tempaddres := account_array(i).newAddress;
  for j in 1..tempaddres.count loop
    insert into TEST_ACCOUNT (ACCOUNT,NEWADDRESS1 ,NEWADDRESS2 ,CITY ,STATE ,ZIP )
       values(account_array(i).fname,
        tempaddres(j).newAddress1,
       tempaddres(j).newAddress2,
       tempaddres(j).city,
        tempaddres(j).state,
         tempaddres(j).zip);
  end loop;
  end loop;
end;

i can make use of arraydescriptor to pass parent array ACCOUNT_OBJ_ARRAY ,but how to pass array inside it (the array of new address objects) from Spring procedure call?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请爱~陌生人 2024-10-19 18:47:09

请参阅SO问题“如何在java中调用包含用户定义类型的oracle存储过程?"查看如何在java中绑定SQL对象的SQL嵌套表的示例。这里的 SQL 对象本身也包含一个对象的嵌套表。方法类似(java中SQL数组通过ARRAY对象绑定,SQL对象通过STRUCT绑定)。

SQL> CREATE OR REPLACE PROCEDURE insert_new_address (
  2     account_array IN ACCOUNT_OBJ_ARRAY
  3     ) AS
  4     tempaddres NEW_ADDRS_OBJ_ARRAY;
  5  BEGIN
  6     FOR i IN 1 .. account_array.COUNT LOOP
  7        FOR j IN 1 .. account_array(i).newAddress.COUNT LOOP
  8           dbms_output.put_line(
  9              account_array(i).fname || ', ' || 
 10              account_array(i).newAddress(j).newAddress1 || ', ' || 
 11              account_array(i).newAddress(j).newAddress2 || ', ' || 
 12              account_array(i).newAddress(j).city || ', ' || 
 13              account_array(i).newAddress(j).state || ', ' || 
 14              account_array(i).newAddress(j).zip);
 15        END LOOP;
 16     END LOOP;
 17  END;
 18  /

Procedure created

我将展示一个使用 Oracle java jvm 的演示:

SQL> CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "ArrayDemo"
  2  as
  3  
  4  import java.io.*;
  5  import java.sql.*;
  6  import oracle.sql.*;
  7  import oracle.jdbc.driver.*;
  8  
  9  public class ArrayDemo {
 10  
 11     public static void passArray() throws SQLException {
 12  
 13        Connection conn = new OracleDriver().defaultConnection();
 14  
 15        /*creation of NEW_ADDRS_OBJ*/
 16        StructDescriptor addrObjDescriptor =
 17           StructDescriptor.createDescriptor("NEW_ADDRS_OBJ",conn);
 18        Object[] addrAtributes = new Object[] {new String("address1"),
 19                                               new String("address2"),
 20                                               new String("city"),
 21                                               new String("state"),
 22                                               new Integer(12345)};
 23  
 24        /*creation of NEW_ADDRS_OBJ_ARRAY*/
 25        ArrayDescriptor addrTabDescriptor =
 26           ArrayDescriptor.createDescriptor( "NEW_ADDRS_OBJ_ARRAY", conn );
 27        STRUCT addr1 = new STRUCT(addrObjDescriptor,conn,addrAtributes);
 28        STRUCT[] adrStructArray = {addr1};
 29        ARRAY arrayAddr = new ARRAY(addrTabDescriptor,conn,adrStructArray);
 30  
 31        /*creation of ACCOUNT_OBJ*/
 32        StructDescriptor acctObjDescriptor =
 33           StructDescriptor.createDescriptor("ACCOUNT_OBJ",conn);
 34        Object[] acctAttributes = new Object[] {new String("AcctName"),
 35                                                arrayAddr};
 36  
 37        /*creation of ACCOUNT_OBJ_ARRAY*/
 38        ArrayDescriptor acctTabDescriptor =
 39           ArrayDescriptor.createDescriptor( "ACCOUNT_OBJ_ARRAY", conn );
 40        STRUCT acct1 = new STRUCT(acctObjDescriptor,conn,acctAttributes);
 41        STRUCT[] acctStructArray = {acct1};
 42        ARRAY acctArray = new ARRAY(acctTabDescriptor,conn,acctStructArray);
 43  
 44        /* statement */
 45        OraclePreparedStatement ps =
 46           (OraclePreparedStatement)conn.prepareStatement
 47              ("begin insert_new_address(:x); end;" );
 48  
 49        ps.setARRAY( 1, acctArray );
 50        ps.execute();
 51  
 52     }
 53  }
 54  /

Java created

现在我们调用 java 过程:

SQL> CREATE OR REPLACE
  2  PROCEDURE show_java_calling_plsql
  3  AS LANGUAGE JAVA
  4  NAME 'ArrayDemo.passArray()';
  5  /

Procedure created

SQL> exec show_java_calling_plsql

AcctName, address1, address2, city, state, 12345

PL/SQL procedure successfully completed

See the SO question "How to call oracle stored procedure which include user-defined type in java?" for an example of how to bind a SQL nested table of SQL object in java. Here the SQL object also itself contains a nested table of object. The method is similar (SQL arrays are bound by ARRAY Object in java, SQL objects are bound by STRUCT).

SQL> CREATE OR REPLACE PROCEDURE insert_new_address (
  2     account_array IN ACCOUNT_OBJ_ARRAY
  3     ) AS
  4     tempaddres NEW_ADDRS_OBJ_ARRAY;
  5  BEGIN
  6     FOR i IN 1 .. account_array.COUNT LOOP
  7        FOR j IN 1 .. account_array(i).newAddress.COUNT LOOP
  8           dbms_output.put_line(
  9              account_array(i).fname || ', ' || 
 10              account_array(i).newAddress(j).newAddress1 || ', ' || 
 11              account_array(i).newAddress(j).newAddress2 || ', ' || 
 12              account_array(i).newAddress(j).city || ', ' || 
 13              account_array(i).newAddress(j).state || ', ' || 
 14              account_array(i).newAddress(j).zip);
 15        END LOOP;
 16     END LOOP;
 17  END;
 18  /

Procedure created

I will show a demo using the Oracle java jvm:

SQL> CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "ArrayDemo"
  2  as
  3  
  4  import java.io.*;
  5  import java.sql.*;
  6  import oracle.sql.*;
  7  import oracle.jdbc.driver.*;
  8  
  9  public class ArrayDemo {
 10  
 11     public static void passArray() throws SQLException {
 12  
 13        Connection conn = new OracleDriver().defaultConnection();
 14  
 15        /*creation of NEW_ADDRS_OBJ*/
 16        StructDescriptor addrObjDescriptor =
 17           StructDescriptor.createDescriptor("NEW_ADDRS_OBJ",conn);
 18        Object[] addrAtributes = new Object[] {new String("address1"),
 19                                               new String("address2"),
 20                                               new String("city"),
 21                                               new String("state"),
 22                                               new Integer(12345)};
 23  
 24        /*creation of NEW_ADDRS_OBJ_ARRAY*/
 25        ArrayDescriptor addrTabDescriptor =
 26           ArrayDescriptor.createDescriptor( "NEW_ADDRS_OBJ_ARRAY", conn );
 27        STRUCT addr1 = new STRUCT(addrObjDescriptor,conn,addrAtributes);
 28        STRUCT[] adrStructArray = {addr1};
 29        ARRAY arrayAddr = new ARRAY(addrTabDescriptor,conn,adrStructArray);
 30  
 31        /*creation of ACCOUNT_OBJ*/
 32        StructDescriptor acctObjDescriptor =
 33           StructDescriptor.createDescriptor("ACCOUNT_OBJ",conn);
 34        Object[] acctAttributes = new Object[] {new String("AcctName"),
 35                                                arrayAddr};
 36  
 37        /*creation of ACCOUNT_OBJ_ARRAY*/
 38        ArrayDescriptor acctTabDescriptor =
 39           ArrayDescriptor.createDescriptor( "ACCOUNT_OBJ_ARRAY", conn );
 40        STRUCT acct1 = new STRUCT(acctObjDescriptor,conn,acctAttributes);
 41        STRUCT[] acctStructArray = {acct1};
 42        ARRAY acctArray = new ARRAY(acctTabDescriptor,conn,acctStructArray);
 43  
 44        /* statement */
 45        OraclePreparedStatement ps =
 46           (OraclePreparedStatement)conn.prepareStatement
 47              ("begin insert_new_address(:x); end;" );
 48  
 49        ps.setARRAY( 1, acctArray );
 50        ps.execute();
 51  
 52     }
 53  }
 54  /

Java created

Now we call the java procedure:

SQL> CREATE OR REPLACE
  2  PROCEDURE show_java_calling_plsql
  3  AS LANGUAGE JAVA
  4  NAME 'ArrayDemo.passArray()';
  5  /

Procedure created

SQL> exec show_java_calling_plsql

AcctName, address1, address2, city, state, 12345

PL/SQL procedure successfully completed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文