复杂xml文件解析并导入oracle和db2
2016-07-29 10:03
218 查看
1.将jdbc查询到的结果集存到map
2.jdbc批量插入数据
public static List<Map<String, Object>> getIfTDesc() { String sql = "select d.IP as NODE,i.GROUPNAME as OIDGROUP,p.IFDESCR as PORTNAME from NETWORK_DEVICE_PORT p,(select ID,IP from NETWORK_DEVICE_INFO) as d,(select SUBSTR(n.VALUE,22) as ifdex,g.GROUPNAME from NETWORK_OID_LIST n,(select ID,GROUPNAME from NETWORK_OID_GROUP where GROUPNAME like 'IFUTI%') as g where GROUPID =g.ID and NAME ='ifInOctets') as i where p.DEVICEID=d.ID and p.IFINDEX=i.ifdex"; //String sql = "select * from KIS_PORT"; PreparedStatement ps = null; ResultSet rs = null; Statement stmt = null; Connection conn = null; List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>(); InputStream in = DBManage.class.getResourceAsStream("/rsbxconfig.properties"); Properties prop = new Properties(); try { prop.load(in); Class.forName(prop.getProperty("driverClass")); conn = DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("name"), prop.getProperty("pass")); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); ResultSetMetaData rsmd = ps.getMetaData(); int columnCount = rsmd.getColumnCount(); Map<String, Object> data =null; while(rs.next()){ data = new HashMap<String, Object>(); for(int i = 1 ; i <= columnCount ; i++){ data.put(rsmd.getColumnLabel(i), rs.getObject(rsmd .getColumnLabel(i))); } datas.add(data); } } catch (Exception e) { logger.error("查询Description报错"+e); e.printStackTrace(); }finally{ try { if(rs!=null){ rs.close(); rs = null; } if(stmt!=null){ stmt.close(); stmt = null; } if(conn!=null){ conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } logger.info("查询Decription结果集成功,共有"+datas.size()+"条数据"); return datas; }
2.jdbc批量插入数据
public static void insertKis(List<KisSnmp> kisSnmpList) { final int batchSize = 5000; int count = 0; PreparedStatement ps = null; InputStream in = DBManage.class.getResourceAsStream("/rsbxconfig.properties"); Properties prop = new Properties(); try { System.out.println("共有"+kisSnmpList.size()+"条数据"); prop.load(in); String dataBase = prop.getProperty("database"); String sql = "insert into "+ dataBase + " values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; Class.forName(prop.getProperty("driverClass")); Connection conn = DriverManager.getConnection(prop.getProperty("jdbcUrl"), prop.getProperty("username"), prop.getProperty("password")); ps = conn.prepareStatement(sql); for(KisSnmp kis:kisSnmpList){ setPrepared(ps,kis.getTMZDIFF(),kis.getWRITETIME(),kis.getNode(),kis.getTimestamp(),kis.getGuid(),kis.getMonitorLocation(),kis.getIdentChecksum(), kis.getService(),kis.getProfile(),kis.getDescription(),kis.getHost(),kis.getOidGroup(),kis.getSnmpVersion(),kis.getServiceLevelString(),kis.getServiceLevel(),kis.getResultString(), kis.getResultMessage(),kis.getOIDNameZero(),kis.getOIDNameOne(),kis.getOIDNameTwo(),kis.getOIDNameThree(),kis.getOIDNameFour(),kis.getOIDNameFive(),kis.getOIDNameSix(),kis.getOIDNameSeven(), kis.getOIDNameEight(),kis.getOIDNameNine(),kis.getSnmpResultZero(),kis.getSnmpResultOne(),kis.getSnmpResultTwo(),kis.getSnmpResultThree(),kis.getSnmpResultFour(),kis.getSnmpResultFive(), kis.getSnmpResultSix(),kis.getSnmpResultSeven(),kis.getSnmpResultEight(),kis.getSnmpResultNine(),kis.getTotalTime(),kis.getLastServiceLevel()); ps.addBatch(); if(++count % batchSize == 0) { ps.executeBatch(); } } ps.executeBatch(); ps.close(); } catch (SQLException e) { while(e!=null){ System.out.println("SQLERROR:\n"+e.getErrorCode()+ ",SQLState:"+e.getSQLState()+ ",Message:"+e.getMessage()+ ",Vendor:"+e.getErrorCode()); logger.error("SQLERROR:\n"+e.getErrorCode()+ ",SQLState:"+e.getSQLState()+ ",Message:"+e.getMessage()+ ",Vendor:"+e.getErrorCode()); e = e.getNextException(); } logger.error("sql执行报错"+e); //e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); }finally{ if(ps != null){ try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
相关文章推荐
- SQL0668N 原因码为 "1",所以不允许操作 SQLSTATE=57016 ORACLE一次失败的导入到DB2
- Oracle与DB2中内置函数substr(char,position,substring_length)的解析
- Oracle面试题之:复杂的查询与实例解析
- ORACLE 11G导入数据报ORA-12154错误解析
- pull解析复杂xml文件
- [导入]各种数据库系统(Oracle,SQLServer,DB2等)的JDBC驱动下载网址
- Oracle解析复杂json的方法
- 解析Oracle中sql语句的复杂查询
- oracle导入导出工具exp/imp 命令使用解析
- Oracle解析复杂json的方法实例详解
- ORACLE导入导出命令解析
- ajax简单解析xml文件(复杂的解析可以通过后台程序实现,如一般处理程序)
- Oracle中得blob字段导入到DB2的Blob字段
- 采用dom4j解析XML文件实现Java与oracle连接
- ORACLE导入导出命令解析
- [导入] xml文件解析-DOM4J
- 将DB2中某表单导入到ORACLE中
- 一次解决DB2接口文件到Oracle无法导入问题的经历
- android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件
- Oracle解析复杂json的方法