Oracle数据库中增删集合元素的操作该如何实现呢?其实利用Java Function就可以轻松的实现,本文我们就主要介绍这一实现方法。
源程序如下:
class="dp-xml">
- package zgdx.action; import java.math.BigDecimal;
- import java.sql.Array;
- //import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import oracle.jdbc.*;
- import oracle.sql.ARRAY;
- import oracle.sql.ArrayDescriptor; public class OraTools {
- static public Array addElementOfArray(ARRAY ary, Object e){
- try {
- Object[] bigs = null;
- if(ary==null) bigs = new Object[0];
- else bigs = (Object[]) ary.getArray();
- for(int i=0; i<bigs.length; i++)
- if(e.equals(bigs[i])){ return ary; }
- Object[] objs = new Object[bigs.length+1];
- for(int i=0; i<bigs.length; i++) objs[i] = bigs[i];
- objs[objs.length-1] = e;
- OracleConnection conn = (OracleConnection) DriverManager.getConnection("jdbc:default:connection:");
- //((OracleConnection)conn.unwrap(OracleConnection.class))
- //Array arr = conn.createARRAY(typeName.toUpperCase(), objs ); //11g r1
- Array arr = new ARRAY(ArrayDescriptor.createDescriptor( ary.getSQLTypeName(), conn), conn, objs);
- return arr;
- } catch (SQLException ex) {
- //Logger.getLogger(OraTools.class.getName()).log(Level.SEVERE, null, ex);
- System.err.println("ERROR! addElementOfVarray: " + ex.getMessage());
- return ary; } static public Array delElementOfArray(ARRAY ary, Object e){
- try {
- if( ary==null) return null;
- Object[] bigs = (Object[]) ary.getArray();
- ArrayList list = new ArrayList(bigs.length);
- for(int i=0; i<bigs.length; i++)
- if( ! e.equals(bigs[i])){ list.add(bigs[i]); }
- if(list.size()==bigs.length) return ary;
- Object[] objs = list.toArray();
- OracleConnection conn = (OracleConnection) DriverManager.getConnection("jdbc:default:connection:");
- //Array arr = conn.createARRAY(typeName.toUpperCase(), objs ); //11g r1
- Array arr = new ARRAY(ArrayDescriptor.createDescriptor(ary.getSQLTypeName(), conn), conn, objs);
- return arr;
- } catch (SQLException ex) {
- System.err.println("ERROR! addElementOfVarray: " + ex.getMessage());
- return ary; }
- }
然后发布:loadjava -f -v -user username/passwd -r OraTools.java
建call spec:
- CREATE OR REPLACE function addElementOfInt_nt(ary int_nt, dept_id NUMBER)
- return int_nt AS LANGUAGE JAVA NAME 'zgdx.action.OraTools.addElementOfArray(java.sql.Array, java.lang.Object) return java.sql.Array';
测试:update test1 set cates = addElementOfInt_nt(cates, 10) where code=1;
关于Oracle数据库中增删集合元素的Java Function的实现方法就介绍到这里了,希望本次的介绍能够对您有所帮助。