//-------------------------------Oracle部分------------------------------------
1. CREATE OR REPLACE TYPE CDW_OBJECT AS OBJECT(
lablename nVARCHAR2(50),
useaccount nVARCHAR2(50)
2.CREATE OR REPLACE TYPE CDW_TABLE AS TABLE OF CDW_OBJECT;
3.CREATE OR REPLACE PROCEDURE recordlabel
(
useaccount_in IN varchar2,
lablenames_in IN CDW_TABLE //自定义类型
)
IS
isExist number;
BEGIN
for i in 1..lablenames_in.count loop
select count(*) into isExist from labelsystem_log where useaccount=useaccount_in and lablename=lablenames_in(i).lablename;
if(isExist>0) then
....
else
...
end if;
end loop;
commit;
END;
注:如果自定义类型错了,在删除的时候需要先删除该类型依赖的对象,否则报如下错
“ORA-02303: 无法使用类型或表的相关性来删除或取代一个类型”
//-----------------------------------Java 部分-----------------------------
public static long recordLables(String useaccount,ArrayList list) {
Connection con = null;
CallableStatement stmt = null;
int backVal = 0;
try {
con = StorageDao.getDBConnection();
if (con != null) {
ARRAY aArray = getArray(con, "CDW_OBJECT","CDW_TABLE", list);//该函数调用的第二三个参数就是上面自定义的两个类型,在此必须大写
//System.out.println(aArray.toString());
stmt = con.prepareCall("{call RECORDLABEL(?,?)}");
stmt.setString(1,useaccount);
((OracleCallableStatement) stmt).setARRAY(2, aArray);
stmt.execute();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try
{
if(stmt!=null){
stmt.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
}
try
{
if(con!=null){
con.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
return backVal;
}
private static ARRAY getArray(Connection con, String OracleObj, String Oraclelist, ArrayList objlist) throws Exception {
ARRAY list = null;
if (objlist != null && objlist.size() > 0) {
StructDescriptor structdesc = new StructDescriptor(OracleObj, con);
STRUCT[] structs = new STRUCT[objlist.size()];
Object[] result = null;
for (int i = 0; i < objlist.size(); i++) {
result = new Object[1];//数组大小应和你定义的数据库对象(CDW_OBJECT )的属性的个数
result[0] = objlist.get(i); //将list中元素的数据传入result数组
structs[i] = new STRUCT(structdesc, con, result);
}
ArrayDescriptor desc = ArrayDescriptor.createDescriptor(Oraclelist,con);
list = new ARRAY(desc, con, structs);
}
return list;
}