Use arrays to access Oracle to improve performance

Source: Internet
Author: User

 

Oracle stored procedures support arrays, which greatly improves the performance of batch data operations. If you need to insert 1000 records, the test_info table structure is:

--------------------------------------------------------------------

Id_seq number, login_id varchar (64) and info varchar (100)

--------------------------------------------------------------------

Three fields. id_seq is a sequence;

 

Because arrays are used, you need to create a global array type in Oracle, as shown below:

Create or replace type t_stringarray as table of varchar2 (128)

 

Define a stored procedure as follows:

Function test_addmultipleidinfos (v_ids t_stringarray, v_infos t_stringarray) return number

Is

I number: = 0;

Begin

While I <v_ids.count Loop

I: = I + 1;

Insert into test_info (id_seq, login_id, Info) values (test_info_id_seq.nextval, v_ids (I), v_infos (I ));

End loop;

Commit;

Return 0;

Exception
When others then
Rollback;
Return-1;
End;

 

 

Then the development program is supported by both Java and C, and examples of writing Java and C are given respectively.

Java Syntax:

Preparedstatement pstmt = NULL;

String strsql = "{call test. test_addmultipleidinfos (?,?)} ";
Pstmt = con. preparecall (strsql );
String [] IDs = new string [2];

String [] Infos = new string [2];

IDS [0] = "test1 ";

IDS [1] = "Test2 ";

Infos [0] = "info1 ";

Infos [1] = "info2"

Oracle. SQL. arraydescriptor DESC = oracle. SQL. arraydescriptor. createdescriptor ("t_stringarray", con );
Oracle. SQL. array idarray = new Oracle. SQL. Array (DESC, Con, IDS );
Pstmt. setarray (1, idarray );

Oracle. SQL. array infoarray = new Oracle. SQL. Array (DESC, Con, Infos );

Pstmt. setarray (2, infoarray );
Resultset rset = pstmt.Executequery();

Int ret = rset. getint (1 );

 

 C ++ OCI is written as follows:

Vector <string> IDs, Infos;

IDs. push_back ("test1 ");

IDs. push_back ("Test2 ");

Infos. push_back ("info1 ");

Infos. push_back ("info2 ");

M_stmt = m_con-> createstatement ("begin: v_err: = test. test_addmultipleidinfos (: v_ids,: v_infos); end ;");
M_stmt-> registeroutparam (1, occinumber );
Setvector (m_stmt, 2, IDs, "t_stringarray ");
Setvector (m_stmt, 3, Infos, "t_stringarray ");

M_stmt-> executeupdate ();
Ret = m_stmt-> getnumber (1 );

M_con-> terminatestatement (m_stmt );
 

 

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.