数组、二维数组、及类型,存储过程调用

来源:互联网 发布:医学类搜题软件 编辑:程序博客网 时间:2024/04/24 07:50

Oracle部分:

--------------------------------------------------------------------------------------------------------------------------------------

create table parent(
    id number(10),
    name varchar2(100),
    title varchar2(10)
);

create table child(
    id number(10),
    parent_id number(10),
    child_name varchar2(100),
    child_title varchar2(10),
    child_content varchar2(200),
    child_time timestamp
);

create sequence seq_p_c_id
minvalue 1
maxvalue 9999999999
start with 1
increment by 1
nocache;

drop type t_child_lst_map;
drop type t_child_lst;
drop type t_parent_lst;

--创建表对象类型
create or replace type t_parent as object (
    name varchar2(100),
    title varchar2(10)
);
/

--创建表对象类型
create or replace type t_child as object (
    child_name varchar2(100),
    child_title varchar2(10),
    child_content varchar2(200)
);
/

--创建自定义类型变量
create or replace type t_parent_lst as table of t_parent;
/

--创建自定义类型变量
create or replace type t_child_lst as table of t_child;
/

--创建自定义类型变量
create or replace type t_child_lst_map as table of t_child_lst;
/

--创建存储过程
create or replace procedure proc_ins_parent_child(
    i_parent_lst in t_parent_lst,        --parent列表
    i_child_map_lst in t_child_lst_map,  --child列表集合,一个map元素对应一个child_lst,其下标与 parent列表的下标相同。
    o_ret out number
) as
var_parent t_parent;
var_child_lst t_child_lst;
var_child t_child;
var_parent_id number;
var_child_id number;
begin
    for i in 1..i_parent_lst.count loop
        --取得parent各列的值
        var_parent := i_parent_lst(i);

        --取得parent_id;
        select seq_p_c_id.nextVal into var_parent_id from dual;
        
        --插入parent表
        insert into parent(
            id,
            name,
            title
        )
        values(
            var_parent_id,
            var_parent.name,
            var_parent.title
        );
        
        --取得该parent对应的child列表
        var_child_lst := i_child_map_lst(i);

        for j in 1..var_child_lst.count loop
            var_child := var_child_lst(j);
            
            --取得child_id;
            select seq_p_c_id.nextVal into var_child_id from dual;

            --插入child表
            insert into child(
                id,
                parent_id,
                child_name,
                child_title,
                child_content,
                child_time
            )
            values(
                var_child_id,
                var_parent_id,
                var_child.child_name,
                var_child.child_title,
                var_child.child_content,
                systimestamp
            );
        end loop;
    
    end loop;
    o_ret := 0;

    exception when others then
    begin
        o_ret := -1;
        raise;
    end;
end proc_ins_parent_child;
/

Java部分:

----------------------------------------------------------------------------------------------------------------------------------

  1. package test.oracle.conn;   
  2. import java.sql.Connection;   
  3. import java.sql.DriverManager;   
  4. public class OConnection {   
  5.     public static Connection getConn() {   
  6.         String URL = "jdbc:oracle:thin:@127.0.0.1:1521:oradb";   
  7.         String user = "cartoon";// 这里替换成你自已的数据库用户名   
  8.         String password = "oracle";// 这里替换成你自已的数据库用户密码   
  9.         Connection connection = null;   
  10.         try {   
  11.             Class.forName("oracle.jdbc.driver.OracleDriver");   
  12.             System.out.println("类实例化成功!");   
  13.             connection = DriverManager.getConnection(URL, user, password);   
  14.             System.out.println("创建连接对像成功!");   
  15.         } catch (Exception err) {   
  16.             err.printStackTrace();   
  17.             return null;   
  18.         }   
  19.         return connection;   
  20.     }   
  21. }  

构造Java映射对象

  1. package test.oracle.array;   
  2. public class Parent {   
  3.     private String id;   
  4.     private String name;   
  5.     private String title;   
  6.     public String getId() {   
  7.         return id;   
  8.     }   
  9.     public void setId(String id) {   
  10.         this.id = id;   
  11.     }   
  12.     public String getName() {   
  13.         return name;   
  14.     }   
  15.     public void setName(String name) {   
  16.         this.name = name;   
  17.     }   
  18.     public String getTitle() {   
  19.         return title;   
  20.     }   
  21.     public void setTitle(String title) {   
  22.         this.title = title;   
  23.     }   
  24.     public String toString() {   
  25.         return "Parent{id=" + this.id + ",name=" + this.name + ",title="  
  26.                 + this.title + "}";   
  27.     }   
  28. }   

构造Java映射对象

  1. package test.oracle.array;   
  2. public class Child {   
  3.     private String id;   
  4.     private String parentId;   
  5.     private String childName;   
  6.     private String childTitle;   
  7.     private String childContent;   
  8.     private String childTime;   
  9.     public String getChildContent() {   
  10.         return childContent;   
  11.     }   
  12.     public void setChildContent(String childContent) {   
  13.         this.childContent = childContent;   
  14.     }   
  15.     public String getChildName() {   
  16.         return childName;   
  17.     }   
  18.     public void setChildName(String childName) {   
  19.         this.childName = childName;   
  20.     }   
  21.     public String getChildTime() {   
  22.         return childTime;   
  23.     }   
  24.     public void setChildTime(String childTime) {   
  25.         this.childTime = childTime;   
  26.     }   
  27.     public String getChildTitle() {   
  28.         return childTitle;   
  29.     }   
  30.     public void setChildTitle(String childTitle) {   
  31.         this.childTitle = childTitle;   
  32.     }   
  33.     public String getId() {   
  34.         return id;   
  35.     }   
  36.     public void setId(String id) {   
  37.         this.id = id;   
  38.     }   
  39.     public String getParentId() {   
  40.         return parentId;   
  41.     }   
  42.     public void setParentId(String parentId) {   
  43.         this.parentId = parentId;   
  44.     }   
  45.     public String toString() {   
  46.         return "Child{id=" + this.id + ",parentId=" + this.parentId   
  47.                 + ",childName=" + this.childName + ",childTitle="  
  48.                 + this.childTitle + ",childContent=" + this.childContent   
  49.                 + ",childTime=" + this.childTime + "}";   
  50.     }   
  51. }  

Java方法实现Oracle对象数组

  1. package test.oracle.array;   
  2. import java.sql.CallableStatement;   
  3. import java.sql.Connection;   
  4. import java.sql.SQLException;   
  5. import java.util.ArrayList;   
  6. import oracle.jdbc.driver.OracleTypes;   
  7. import oracle.sql.ARRAY;   
  8. import oracle.sql.ArrayDescriptor;   
  9. import oracle.sql.STRUCT;   
  10. import oracle.sql.StructDescriptor;   
  11. public class OracleArray {   
  12.     private static final String T_PARENT = "T_PARENT";   
  13.     private static final String T_PARENT_LST = "T_PARENT_LST";   
  14.     private static final String T_CHILD = "T_CHILD";   
  15.     private static final String T_CHILD_LST = "T_CHILD_LST";   
  16.     private static final String T_CHILD_LST_MAP = "T_CHILD_LST_MAP";   
  17.     private static final String PROC_INS_PARENT_CHILD = "{ call PROC_INS_PARENT_CHILD(?,?,?)}";   
  18.     public static int insParentChils(ArrayList<Parent> plst,   
  19.             ArrayList<ArrayList<Child>> clstMap, Connection con)   
  20.             throws Exception {   
  21.         CallableStatement cstmt = null;   
  22.         int retVal = -1;   
  23.         try {   
  24.             ArrayDescriptor parentLstDesc = ArrayDescriptor.createDescriptor(   
  25.                     T_PARENT_LST, con);   
  26.             StructDescriptor parentDesc = StructDescriptor.createDescriptor(   
  27.                     T_PARENT, con);   
  28.             ArrayDescriptor childLstMapDesc = ArrayDescriptor.createDescriptor(   
  29.                     T_CHILD_LST_MAP, con);   
  30.             ArrayDescriptor childLstDesc = ArrayDescriptor.createDescriptor(   
  31.                     T_CHILD_LST, con);   
  32.             StructDescriptor childDesc = StructDescriptor.createDescriptor(   
  33.                     T_CHILD, con);   
  34.             ArrayList<STRUCT> pstruct = new ArrayList<STRUCT>();   
  35.             // 转换plst为Oracle 对象数组   
  36.             for (int i = 0; i < plst.size(); i++) {   
  37.                 Parent p = plst.get(i);   
  38.                 Object[] record = new Object[2];   
  39.                 record[0] = p.getName();   
  40.                 record[1] = p.getTitle();   
  41.                 STRUCT item = new STRUCT(parentDesc, con, record);   
  42.                 pstruct.add(item);   
  43.             }   
  44.             ARRAY dataps = new ARRAY(parentLstDesc, con, pstruct.toArray());   
  45.             ArrayList<ARRAY> cMap = new ArrayList<ARRAY>();   
  46.             // 转换clst为Oracle 对象数组   
  47.             for (int i = 0; i < clstMap.size(); i++) {   
  48.                 ArrayList<Child> childLst = clstMap.get(i);   
  49.                 ArrayList<STRUCT> cstruct = new ArrayList<STRUCT>();   
  50.                 for (int j = 0; j < childLst.size(); j++) {   
  51.                     Child c = childLst.get(j);   
  52.                     Object[] record = new Object[3];   
  53.                     record[0] = c.getChildName();   
  54.                     record[1] = c.getChildTitle();   
  55.                     record[2] = c.getChildContent();   
  56.                     STRUCT item = new STRUCT(childDesc, con, record);   
  57.                     cstruct.add(item);   
  58.                 }   
  59.                 ARRAY datacs = new ARRAY(childLstDesc, con, cstruct.toArray());   
  60.                 cMap.add(datacs);   
  61.             }   
  62.             ARRAY datacsMap = new ARRAY(childLstMapDesc, con, cMap.toArray());   
  63.             cstmt = con.prepareCall(PROC_INS_PARENT_CHILD);   
  64.             cstmt.setArray(1, dataps);   
  65.             cstmt.setArray(2, datacsMap);   
  66.             cstmt.registerOutParameter(3, OracleTypes.INTEGER);   
  67.             cstmt.execute();   
  68.             retVal = cstmt.getInt(3);   
  69.         } catch (Exception ex) {   
  70.             ex.printStackTrace();   
  71.         } finally {   
  72.             try {   
  73.                 if (cstmt != null) {   
  74.                     cstmt.close();   
  75.                 }   
  76.             } catch (SQLException sqle) {   
  77.                 sqle.printStackTrace();   
  78.             }   
  79.         }   
  80.         return retVal;   
  81.     }   
  82. }  

测试Java调用Oracle对象数组

  1. package test.oracle;   
  2. import java.sql.Connection;   
  3. import java.sql.SQLException;   
  4. import java.util.ArrayList;   
  5. import java.util.Date;   
  6. import test.oracle.array.Child;   
  7. import test.oracle.array.OracleArray;   
  8. import test.oracle.array.Parent;   
  9. import test.oracle.conn.OConnection;   
  10. public class TestOracleArray {   
  11.     public static void main(String[] args) {   
  12.         ArrayList<Parent> plst = new ArrayList<Parent>();   
  13.         for (int i = 0; i < 100; i++) {   
  14.             Parent p = new Parent();   
  15.             p.setName("name" + i);   
  16.             p.setTitle("title" + i);   
  17.             plst.add(p);   
  18.         }   
  19.         ArrayList<ArrayList<Child>> clstMap = new ArrayList<ArrayList<Child>>();   
  20.         for (int i = 0; i < 100; i++) {   
  21.             ArrayList<Child> clst = new ArrayList<Child>();   
  22.             for (int j = 0; j < 100; j++) {   
  23.                 Child c = new Child();   
  24.                 c.setChildName("childName" + j);   
  25.                 c.setChildTitle("childT" + j);   
  26.                 c.setChildContent("childContent"+j);   
  27.                 clst.add(c);   
  28.             }   
  29.             clstMap.add(clst);   
  30.         }   
  31.         Connection con = null;   
  32.         try {   
  33.             long startTime = 0;   
  34.             long endTime = 0;   
  35.             con = OConnection.getConn();   
  36.             startTime = new Date().getTime();   
  37.             OracleArray.insParentChils(plst, clstMap, con);   
  38.             endTime = new Date().getTime();   
  39.             System.out.println("It takes " + (endTime - startTime)   
  40.                     + " milliseconds to execute");   
  41.         } catch (Exception e) {   
  42.             e.printStackTrace();   
  43.         } finally {   
  44.             try {   
  45.                 if (con != null) {   
  46.                     con.close();   
  47.                     System.out.println("disconnected");   
  48.                 }   
  49.             } catch (SQLException sqle) {   
  50.                 sqle.printStackTrace();   
  51.             }   
  52.         }   
  53.         System.exit(0);   
  54.     }   
  55. }  

 

 

 

 

原创粉丝点击