如何将List从java传递给Oracle程序?

我想从Java发送一个List到Oracle程序。 例如,有一所学校,学校有一份学生名单。 此外,学生还有一个讲座列表。 我创建了一个讲座列表,一个列有讲座列表的学生列表,一个学校列出了学生列表。

讲座。

ArrayList lecture1 = new ArrayList(); lecture1.add("Mat"); lecture1.add("physics"); ArrayList lecture2 = new ArrayList(); lecture2.add("English"); lecture2.add("Spanish"); ArrayList lecture3 = new ArrayList(); lecture3.add("Germany"); lecture3.add("French"); 

讲座清单。

 ArrayList<ArrayList> lectureList1 = new ArrayList<ArrayList>(); lectureList1.add(lecture1); lectureList1.add(lecture3); ArrayList<ArrayList> lectureList2 = new ArrayList<ArrayList>(); lectureList2.add(lecture2); lectureList2.add(lecture3); 

和讲座的学生名单。

  ArrayList<ArrayList> StudentList = new ArrayList<ArrayList>(); StudentList.addAll(lectureList2); StudentList.addAll(lectureList2); ArrayList<ArrayList> StudentList2 = new ArrayList<ArrayList>(); StudentList2.addAll(lectureList1); StudentList2.addAll(lectureList2); 

和学校

  ArrayList<ArrayList<ArrayList>> school = new ArrayList<ArrayList<ArrayList>>(); school.add(StudentList2); school.add(StudentList); 

我想把“ 学校 ”发送到oracle程序。 但是我无法直接发送列表。 Oracle库允许发送数组,但我想发送列表。

我该怎么做这个操作? 你可以帮帮我吗。

谢谢。

将列表转换为多维数组,然后您可以执行以下操作:

Oracle安装程序

 CREATE TYPE stringlist AS TABLE OF VARCHAR2(100); / CREATE TYPE stringlist_list AS TABLE OF stringlist; / CREATE TYPE stringlist_list_list AS TABLE OF stringlist_list; / CREATE PROCEDURE load_list ( in_list IN stringlist_list_list ) AS BEGIN NULL; -- Do something with the list END; / 

Java

 import java.sql.Connection; import java.sql.DriverManager; import java.sql.CallableStatement; import java.sql.ResultSet; import java.sql.SQLException; import oracle.jdbc.OracleCallableStatement; import oracle.sql.ARRAY; import oracle.sql.ArrayDescriptor; public class TestDatabase2 { public static void main(String args[]){ try{ Class.forName("oracle.jdbc.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","username","password"); // Convert your lists to arrays using #toArray( T[] ) String[] l1 = { "Math", "Physics" }; String[] l2 = { "English", "Spanish" }; String[] l3 = { "French", "German" }; ARRAY school = new ARRAY( des, con, newString[][][]{ new String[][]{ l1, l3 }, new String[][]{ l2, l3 } } ); ArrayDescriptor des = ArrayDescriptor.createDescriptor("STRINGLIST_LIST_LIST", con); CallableStatement st = con.prepareCall("{ call add_school( :school )}"); // Passing an array to the procedure - ((OracleCallableStatement) st).setARRAYAtName( "school", school ); st.execute(); } catch(ClassNotFoundException | SQLException e) { System.out.println(e); } } }