During the writing and storage process, we often encounter converting the arraylist in Java into an array in Oracle. (The arraylist contains some Java objects.
For example, some people objects exist in arraylist.
1. First, you must create corresponding Java objects and arrays in the database,
For example:/* database objects corresponding to Java objects */
- Create type peopleoracleobject as object
- (
- Peopleoracleid number (8 ),
- Nameoracle varchar (50 ),
- Ageoracle number (3)
- )
/Array in the database
- Create type people_oracle_list as varray (500) of peopleoracle;
/
2. Convert arraylist in Java
- Private Static array getoraclearray (connection con, string oraclelist,
- Arraylist objlist) throws exception {
- Array list = NULL;
- If (objlist! = NULL & objlist. Size ()> 0 ){
- Structdescriptor structdesc = new structdescriptor (
- "Peopleoracleobject", con );
- Struct [] structs = new struct [objlist. Size ()];
- Object [] result = new object [0];
- For (INT I = 0; I <objlist. Size (); I ++ ){
- Result = new object [2];
- Result [0] = new long (people) (objlist. Get (I). getpeopleid ());
- Result [1] = new long (people) (objlist. Get (I). getpeoplename ());
- Result [2] = new long (people) (objlist. Get (I). getpeopleage ());
- Structs [I] = new struct (structdesc, Con, result );
- }
- Arraydescriptor DESC = arraydescriptor. createdescriptor (oraclelist,
- Con );
- List = new array (DESC, Con, structs );
- } Else {
- Arraydescriptor DESC = arraydescriptor. createdescriptor (oraclelist,
- Con );
- Struct [] structs = new struct [0];
- List = new array (DESC, Con, structs );
- }
- Return list;
- } // Function
3./* Add the converted array to the stored procedure */
- Public static int updateadinfo (arraylist peolelist, int ID ){
- Connection con = NULL;
- Callablestatement stmt = NULL;
- Int backval;
- Try {
- Con = pool. getconnection ();
- If (con! = NULL ){
- Stmt = con. preparecall ("{call updatepeople (?,?,?)} ");
- Array adarray = getoraclearray (con, "people_oracle_list ",
- Peolelist );
- (Oraclecallablestatement) stmt). setarray (1, adarray );
- Stmt. setint (2, ID );
- Stmt. registeroutparameter (3, java. SQL. types. integer );
- Stmt.exe cute ();
- } Else {
- Backval = 1;
- }
- } Catch (exception e ){
- E. printstacktrace ();
- } Finally {
- Pool. freedbresource (con, stmt, null );
- }
- Return backval;
- }
4. Calling during stored procedures
- Create or replace procedure nad_sp_createwebcosttoad (
- P_peoplearray in people_oracle_list,
- P_id in number,
- P_out out varchar2
- )
- As
- Vpeopleid number (8): = 0;
- Vpeoplename varchar (50): = 0;
- Vage number (3): = 0;
- Begin
- For I 1... p_peoplearray.count Loop
- Leleobj: = p_peoplearray (I );
- Vpeopleid: = peopleobj. peopleoracleid;
- Vpeoplename: = leleobj. nameoracle;
- Vage: = leleobj. ageoracle;
- .................
- End loop;
- Commit;
- Exception
- When others then
- P_out: = '-1' | sqlerrm;
- Rollback;
- End;
- /
- Show errors