Note: This article is from the shenzhen gg java How to pass a list to Oracle stored procedure
One:
a PL/SQL array is built on the database side.
1 CREATE OR REPLACE TYPE Tables_array as Varray (+) of VARCHAR2 (+); 2 3 drop table test purge; CREATE TABLE Test 5 ( 6 name VARCHAR2 (+) 7 ); 9 10
Create or Replace procedure T_list_to_p (arr_t in Tables_array) is one begin< Span style= "color: #008080;" > for i in Arr_t.first. Arr_t.last loop INSERT into test values (arr_t (i)); end Loop; commit; end T_list_to_p;
Two:java code:
1 ImportJava.sql.CallableStatement;2 ImportJava.sql.Connection;3 ImportJava.sql.DriverManager;4 ImportJava.sql.SQLException;5 ImportJava.util.ArrayList;6 ImportJava.util.List;7 8 ImportOracle.sql.ARRAY;9 ImportOracle.sql.ArrayDescriptor;Ten One A Public classtestlisttoprocedure { - Static FinalString Driver_class = "Oracle.jdbc.driver.OracleDriver"; - Static FinalString Connectionurl = "JDBC:ORACLE:THIN:@10.150.15.150:1521:ORCL"; the Static FinalString UserID = "Test"; - Static FinalString UserPassword = "Test"; - Public voidRuntest () { -Connection con =NULL; +CallableStatement stmt =NULL; - Try{ +Class.forName (Driver_class). newinstance (); Acon = drivermanager.getconnection (Connectionurl, UserID, UserPassword); atstmt = Con.preparecall ("{call T_list_to_p (?)}"); -Arraydescriptor descriptor = Arraydescriptor.createdescriptor ("Tables_array", con); -List List =NewArrayList (); -List.add ("Zhang San"); -List.add ("John Doe"); -List.add ("Harry"); inArray array =NewARRAY (Descriptor,con,list.toarray ()); -Stmt.setarray (1, array); toStmt.execute (); +}Catch(SQLException e) { -E.printstacktrace (); the}Catch(Exception e) { *E.printstacktrace (); $}finally{Panax Notoginseng if(stmt! =NULL){ - Try{ theStmt.close (); +}Catch(SQLException e) { AE.printstacktrace (); the} +} - if(Con! =NULL){ $ Try{ $Con.close (); -}Catch(SQLException e) { -E.printstacktrace (); the} -}Wuyi} the} - Wu Public Static voidMain (string[] args) { -Testlisttoprocedure testlisttoprocedure =NewTestlisttoprocedure (); AboutTestlisttoprocedure.runtest (); $} - -}
How Java passes a list to an Oracle stored procedure