一、新建数据

  1. -- 1. 创建一个对象类型
  2. create or replace type STUDENT as object(
  3. id number(4),
  4. name varchar2(50),
  5. age number(3)
  6. );
  7. -- 2. 创建一个数组类型 (任意选择下面的一句执行)
  8. -- 方式1:用table来盛放对象
  9. create or replace type STU_LIST as table of STUDENT;
  10. -- 方式2:用数组来盛放对象,同时可指定数组的大小[用as varray(100)也是可以滴]
  11. create or replace type STU_LIST as array(100) of STUDENT;
  12. -- 3. 创建一个过程引用集合类型的参数
  13. create or replace procedure test_in_list(
  14. -- 参数区域
  15. studentlist in STU_LIST,
  16. message out varchar2
  17. )
  18. is
  19. -- 变量区域
  20. begin
  21. -- 执行区域
  22. for i in 1 .. studentlist.count loop
  23. message:=message||'{id:'||studentlist(i).id||', name:'||studentlist(i).name||', age'||studentlist(i).age||'},';
  24. end loop;
  25. end test_in_list;

二、java代码调用

  1. import java.sql.CallableStatement;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.util.ArrayList;
  6. import oracle.sql.ARRAY;
  7. import oracle.sql.ArrayDescriptor;
  8. import oracle.sql.STRUCT;
  9. import oracle.sql.StructDescriptor;
  10. public class TestProInList {
  11. // 测试main
  12. public static void main(String[] args) {
  13. new TestProInList().testProInList();
  14. }
  15. // 调用存储过程并返回执行结果
  16. public void testProInList() {
  17. // 定义需要的变量
  18. Connection ct = null;
  19. CallableStatement cs = null;
  20. ResultSet rs = null;
  21. try {
  22. // 加载驱动
  23. Class.forName("oracle.jdbc.driver.OracleDriver");
  24. // 得到连接
  25. ct = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:myora1", "sccot", "tiger");
  26. // 创建CallableStatement接口
  27. cs = ct.prepareCall("{call TEST_IN_LIST(?,?)}");
  28. // 给in?赋值
  29. ArrayList<Student> list = new ArrayList<Student>() {
  30. {
  31. this.add(new Student(1, "ZhangSan", 23));
  32. this.add(new Student(2, "LiSi", 22));
  33. this.add(new Student(3, "WangWu", 21));
  34. }
  35. };
  36. ARRAY array = getArray(ct, "STUDENT", "STU_LIST", list);// 该函数调用的第二三个参数就是上面自定义的两个类型,在此必须大写
  37. // 设置入参
  38. cs.setArray(1, array);
  39. // 注册出参
  40. cs.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);
  41. // 执行
  42. cs.execute();
  43. // 获取返回结果
  44. String message = cs.getString(2);
  45. System.out.println(message);
  46. // 打印结果:
  47. // {id:1, name:ZhangSan, age23},{id:2, name:LiSi, age22},{id:3,
  48. // name:WangWu, age21},
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. } finally {
  52. // 关闭资源
  53. try {
  54. if (cs != null) {
  55. cs.close();
  56. }
  57. if (rs != null) {
  58. rs.close();
  59. }
  60. if (ct != null) {
  61. ct.close();
  62. }
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. cs = null;
  67. rs = null;
  68. ct = null;
  69. }
  70. }
  71. private ARRAY getArray(Connection con, String OraObjType, String OraArrType, ArrayList<Student> stuList)
  72. throws Exception {
  73. ARRAY list = null;
  74. if (stuList != null && stuList.size() > 0) {
  75. // Oracle识别的集合对象,匹配java对象集合
  76. STRUCT[] structs = new STRUCT[stuList.size()];
  77. // Oracle识别的对象模板,匹配单个java对象
  78. StructDescriptor structdesc = new StructDescriptor(OraObjType, con);
  79. // 遍历stuList,将每个Student对象转换为Oracle可识别的模板对象
  80. for (int i = 0; i < stuList.size(); i++) {
  81. // java对象
  82. Student student = stuList.get(i);
  83. // 数组大小应和你定义的数据库对象(STUDENT)的属性的个数
  84. Object[] oneRow = new Object[3];
  85. oneRow[0] = student.getId(); // 将Student对象的每个属性按顺序设置到oneRow数组中
  86. oneRow[1] = student.getName();
  87. oneRow[2] = student.getAge();
  88. structs[i] = new STRUCT(structdesc, con, oneRow);
  89. }
  90. // 匹配list
  91. ArrayDescriptor desc = ArrayDescriptor.createDescriptor(OraArrType, con);
  92. list = new ARRAY(desc, con, structs);
  93. }
  94. return list;
  95. }
  96. }
  97. class Student {
  98. private int id;
  99. private String name;
  100. private int age;
  101. // 构造函数
  102. public Student(int id, String name, int age) {
  103. super();
  104. this.id = id;
  105. this.name = name;
  106. this.age = age;
  107. }
  108. // getters/setters(略)
  109. }

引用

https://blog.csdn.net/weixin_34381687/article/details/86228963