How Java passes a list to an Oracle stored procedure

Source: Internet
Author: User
Tags stmt

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:@";    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

Related Article

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: 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.