首页 > 编程语言 > 详细

存储结构接收数组

时间:2018-03-24 18:27:15      阅读:254      评论:0      收藏:0      [点我收藏+]

之前 一直报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),
       sex number(1),
       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
maxvalue 99999999
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;
    if num>0 then
      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;
    }

存储结构接收数组

原文:https://www.cnblogs.com/xiaohuachen/p/8640621.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!