您的位置:首页 > 数据库 > Oracle

复杂xml文件解析并导入oracle和db2

2016-07-29 10:03 218 查看
1.将jdbc查询到的结果集存到map

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();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: