Java Implementation Method for adding/deleting collection elements in Oracle databases

Source: Internet
Author: User

Oracle DatabaseMediumAdd/delete set ElementsHow to Implement the operation? Actually useJavaFunction can be easily implemented. This article mainly introduces this implementation method.

The source program is as follows:

 
 
  1. package zgdx.action; import java.math.BigDecimal;  
  2. import java.sql.Array;  
  3. //import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.SQLException;  
  6. import java.util.ArrayList;  
  7. import java.util.logging.Level;  
  8. import java.util.logging.Logger;  
  9. import oracle.jdbc.*;  
  10. import oracle.sql.ARRAY;  
  11. import oracle.sql.ArrayDescriptor; public class OraTools {  
  12.     static public Array addElementOfArray(ARRAY ary, Object e){  
  13.         try {  
  14.             Object[] bigs = null;  
  15.             if(ary==null)   bigs = new Object[0];  
  16.             else  bigs =  (Object[]) ary.getArray();  
  17.             for(int i=0; i<bigs.length; i++)  
  18.                 if(e.equals(bigs[i])){ return ary; }  
  19.             Object[] objs = new Object[bigs.length+1];  
  20.             for(int i=0; i<bigs.length; i++)  objs[i] = bigs[i];  
  21.             objs[objs.length-1] = e;  
  22.             OracleConnection conn = (OracleConnection) DriverManager.getConnection("jdbc:default:connection:");  
  23.             //((OracleConnection)conn.unwrap(OracleConnection.class))  
  24.             //Array arr = conn.createARRAY(typeName.toUpperCase(), objs );  //11g r1  
  25.             Array arr = new ARRAY(ArrayDescriptor.createDescriptor( ary.getSQLTypeName(), conn), conn, objs);  
  26.             return arr;  
  27.         } catch (SQLException ex) {  
  28.             //Logger.getLogger(OraTools.class.getName()).log(Level.SEVERE, null, ex);  
  29.             System.err.println("ERROR! addElementOfVarray: " + ex.getMessage());  
  30.             return ary;     }     static public Array delElementOfArray(ARRAY ary, Object e){  
  31.         try {  
  32.             if( ary==null)  return null;  
  33.             Object[] bigs =  (Object[]) ary.getArray();  
  34.             ArrayList list = new ArrayList(bigs.length);  
  35.             for(int i=0; i<bigs.length; i++)  
  36.                 if( ! e.equals(bigs[i])){ list.add(bigs[i]); }  
  37.             if(list.size()==bigs.length)  return ary;  
  38.             Object[] objs = list.toArray();  
  39.             OracleConnection conn = (OracleConnection) DriverManager.getConnection("jdbc:default:connection:");  
  40.             //Array arr = conn.createARRAY(typeName.toUpperCase(), objs );  //11g r1  
  41.             Array arr = new ARRAY(ArrayDescriptor.createDescriptor(ary.getSQLTypeName(), conn), conn, objs);  
  42.             return arr;  
  43.         } catch (SQLException ex) {  
  44.             System.err.println("ERROR! addElementOfVarray: " + ex.getMessage());  
  45.             return ary;     }  
  46. }  

Then release: loadjava-f-v-user username/passwd-r OraTools. java

Create call spec:

 
 
  1. CREATE OR REPLACE function addElementOfInt_nt(ary int_nt, dept_id NUMBER)   
  2.  
  3. return int_nt AS LANGUAGE JAVA NAME 'zgdx.action.OraTools.addElementOfArray(java.sql.Array, java.lang.Object) return java.sql.Array'; 

Test: update test1 set cates = addElementOfInt_nt (cates, 10) where code = 1;

The implementation of Java functions for adding and deleting collection elements in Oracle databases is introduced here. I hope this introduction will be helpful to you.

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.