How Java passes a set of objects to an Oracle stored procedure

Source: Internet
Author: User

How Java passes a set of objects to an Oracle stored procedure. It is important to note that JAR packages are required, assuming that improper use can result in only numbers passing through, and string passing is just going. Suppose that oracle11g would need to use a jar package like the following,F:\app\Administrator\product\11.2.0\dbhome_1\jlib\orai18n.jar,

D:\program\weblogic\oracle_common\modules\oracle.jdbc_11.2.0\ojdbc6.jar

Examples include the following:

CREATE OR REPLACE TYPE test_object as OBJECT (  ID number,  name VARCHAR2 (32)); CREATE OR REPLACE TYPE Tables_array as Varray (+) of Test_object;drop table Test purge;create table test (  ID number,<  C4/>name varchar2 (+)); Create or replace procedure T_list_to_p (arr_t in Tables_array) Isbegin for  i in 1..arr_t.count Loop      INSERT into  test values (arr_t (i). id,arr_t (i). name);  End Loop;  Commit;end t_list_to_p;


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;import Oracle.sql.struct;import Oracle.sql.structdescriptor;public Class    testlisttoprocedure {static final String Driver_class = "Oracle.jdbc.driver.OracleDriver";    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);        Structdescriptor tdescriptor = Structdescriptor.createdescriptor ("Test_object", con);        list<struct> structs = new arraylist<struct> (); object[] TObject = null;        Ability to convert Vo,dto into Object object in system, first create struts for (int i = 0; i<10; i++) {tobject = new object[2];        Tobject[0] = i;                TOBJECT[1] = "name" +I;                struct tstruct = new struct (Tdescriptor, con, tobject);           Structs.add (tstruct);        } arraydescriptor Arraydescriptor = Arraydescriptor.createdescriptor ("Tables_array", con);            Array tarray = new Array (Arraydescriptor, con, Structs.toarray ()); stmt = Con.preparecall ("{Call T_list_to_p (?

)}"); Stmt.setarray (1, Tarray); 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 set of objects 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.