How Java passes a list to an Oracle stored procedure. Examples include the following:
An array of PL/SQL is built on the database side.
CREATE OR REPLACE TYPE Tables_array as Varray (+) of VARCHAR2 (+);d ROP table Test purge;create table Test ( name Varc HAR2 (+)); Create or replace procedure T_list_to_p (arr_t in Tables_array) Isbegin for i in Arr_t.first: Arr_t.last Lo OP insert into test values (arr_t (i)); End Loop; Commit;end t_list_to_p;
Java code:
Import Java.sql.callablestatement;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.sqlexception;import Java.util.arraylist;import Java.util.list;import Oracle.sql.ARRAY;import Oracle.sql.arraydescriptor;public class Testlisttoprocedure {static final String Driver_class = "ORACLE.JDBC.DRIVER.O Racledriver "; Static final String Connectionurl = "JDBC:ORACLE:THIN:@10.150.15.150:1521:ORCL"; Static final String UserID = "Test"; Static final String UserPassword = "Test"; public void Runtest () {Connection con = null; CallableStatement stmt = null; try {class.forname (driver_class). newinstance (); con = drivermanager.getconnection (Connectionurl, UserID, UserPassword); stmt = Con.preparecall ("{Call T_list_to_p (?)}"); Arraydescriptor descriptor = arraydescriptor.createdescriptor ("Tables_array", con); List List = new ArrayList (); List.add ("Zhang San"); List.add ("John Doe"); List.add ("Harry"); Array array = new Array (Descriptor,con,list.toarray ()); Stmt.setarray (1, array); Stmt.execute (); } catch (SQLException e) {e.printstacktrace (); } catch (Exception e) {e.printstacktrace (); }finally{if (stmt! = null) {try {stmt.close ();} catch (SQLException e) {e.printstacktrace ();} } if (con! = null) {try {con.close ();} catch (SQLException e) {e.printstacktrace ();} }}} public static void Main (string[] args) {testlisttoprocedure testlisttoprocedure = new Testlisttopro Cedure (); Testlisttoprocedure.runtest (); }}
How Java passes a list to an Oracle stored procedure