存儲結構接收數組
之前 一直報org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection 後來根據session拿到的connnection 然後再去獲取一次con = con.getMetaData().getConnection();
以下是完整代碼:
------這裏是創建存儲過程
create or replace type enroll as object(
name varchar2(50),
idcard varchar2(20),
age number(8),
place varchar2(50),
enrolltime date,
enrollresult varchar2(20),
enrollfailinfo varchar2(100),
enrollcanal varchar2(100)
);
CREATE OR REPLACE TYPE enrollinfolist as table of enroll;
-- Create sequence
create sequence enrollinfo_ID
minvalue 1
start with 1
increment by 1;
--過程
create or replace procedure pullenrollinfo(
enrollinfo in enrollinfolist
)
is
v_sql varchar2(200);
num number:=0;
begin
for i in 1..enrollinfo.count loop
v_sql:= ‘select count(*) from enrollinfo where IDcard=‘||enrollinfo(i).idcard;
execute immediate v_sql into num;
update enrollinfo set name=enrollinfo(i).name ,sex=enrollinfo(i).sex , age=enrollinfo(i).age ,place=enrollinfo(i).place ,enrolltime=enrollinfo(i).enrolltime ,enrollresult=enrollinfo(i).enrollresult ,enrollfailinfo=enrollinfo(i).enrollfailinfo ,enrollcanal=enrollinfo(i).enrollcanal where IDcard=enrollinfo(i).idcard;
else
insert into enrollinfo(id,name,idcard,sex,age,place ,enrolltime,enrollresult,enrollfailinfo,enrollcanal)values(enrollinfo_ID.nextval,enrollinfo(i).name,enrollinfo(i).idcard,enrollinfo(i).sex
,enrollinfo(i).age,enrollinfo(i).place,enrollinfo(i).enrolltime,enrollinfo(i).enrollresult,enrollinfo(i).enrollfailinfo,enrollinfo(i).enrollcanal);
end if ;
if mod(i,500)=0 then
commit;
end if;
end loop;
commit;
end pullenrollinfo;
----java代碼
public void insertClientData(List<Enrollinfo> infos) {
Connection con = null;
CallableStatement stmt = null;
try {
con = SessionFactoryUtils.getDataSource(
this.getHibernateTemplate().getSessionFactory())
.getConnection();
con = con.getMetaData().getConnection();
List<STRUCT> structs = new ArrayList<STRUCT>();
ARRAY array = getArray(con, "ENROLL", "ENROLLINFOLIST", infos);
stmt = con.prepareCall("{call pullenrollinfo(?)}");
stmt.setArray(1, array);
stmt.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
log.error("拉取客戶信息出錯:" + e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public ARRAY getArray(Connection con, String OraObjType, String OraArrType,
List<Enrollinfo> infos) throws Exception {
ARRAY list = null;
if (infos != null && infos.size() > 0) {
// Oracle識別的集合對象,匹配java對象集合
STRUCT[] structs = new STRUCT[infos.size()];
// Oracle識別的對象模板,匹配單個java對象
StructDescriptor structdesc = new StructDescriptor(OraObjType, con);
// 遍歷stuList,將每個Student對象轉換為Oracle可識別的模板對象
for (int i = 0; i < infos.size(); i++) {
// java對象
Enrollinfo info= infos.get(i);
// 數組大小應和你定義的數據庫對象(STUDENT)的屬性的個數
Object[] tObject = new Object[9];
tObject[0] = info.getName();
tObject[1] = info.getIdCard();
tObject[2] = info.getSex();
tObject[3] = info.getAge();
tObject[4] = info.getPlace();
tObject[5] = new Date(123);
tObject[6] = info.getEnrollResult();
tObject[7] = info.getEnrollFailInfo();
tObject[8] = info.getEnrollCanal();
structs[i] = new STRUCT(structdesc, con, tObject);
}
// 匹配list
ArrayDescriptor desc = ArrayDescriptor.createDescriptor(OraArrType,
con);
list = new ARRAY(desc, con, structs);
}
return list;
}
存儲結構接收數組