How Java passes a list to an Oracle stored procedure

Source: Internet
Author: User
Tags stmt

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.