早上,老大给我丢过来任务,让我将配置文件(Transformer.xml)配置项一一对应到数据表default_description。
打开配置文件,就想问候他妈。1000多项,这得弄到什么时候。而且我对这样非创造性的东东很排斥的。任务安排下来还是得干。改了几项,眼睛实在疼。就想能通过程序解决。
解决方案:1.解析xml文件得到List数据,2.遍历该list将数据导入到表中
一下是我的主要代码:
private Connection connection = null;
private PreparedStatement ps = null;
/*
* 导数据的 fault_description
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
ImportData fst = new ImportData();
fst.hashMap.put("隐患", 4L);
fst.hashMap.put("一般", 3L);
fst.hashMap.put("重大", 2L);
fst.hashMap.put("紧急", 1L);
List faultList = fst.parse(new File("d:\\transformer.xml"));
for(int i=0;i<faultList.size();i++){
fst.insert(faultList.get(i));
Thread.sleep(10);
}
}
public Map<String, Long> hashMap = new HashMap<String, Long>();
@SuppressWarnings("unchecked")
public List<FaultDescription> parse(File file) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document doc = null;
List faultList = new ArrayList();
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(file);
} catch (Exception e) {
e.printStackTrace();
}
if (file.exists()) {
NodeList nodeList = doc.getElementsByTagName("Location");
for (int i = 0; i < nodeList.getLength(); i++) {
Node locationNode = doc.getElementsByTagName("Location")
.item(i);
if (locationNode.getNodeType() == Node.ELEMENT_NODE) {
Long locationCode = Long.parseLong(locationNode
.getAttributes().getNamedItem("typeCode")
.getNodeValue());
for (int j = 0; j < locationNode.getChildNodes()
.getLength(); j++) {
Node categoryNode = locationNode.getChildNodes()
.item(j);
if (categoryNode.getNodeType() == Node.ELEMENT_NODE) {
Long categoryCode = Long.parseLong(categoryNode
.getAttributes().getNamedItem("typeCode")
.getNodeValue());
for (int k = 0; k < categoryNode.getChildNodes()
.getLength(); k++) {
Node faultNode = categoryNode.getChildNodes()
.item(k);
if (faultNode.getNodeType() == Node.ELEMENT_NODE) {
String desc = faultNode.getAttributes()
.getNamedItem("desc")
.getNodeValue();
Long id = Long.parseLong(faultNode
.getAttributes().getNamedItem(
"typeCode").getNodeValue());
Long grade = hashMap.get(faultNode
.getAttributes().getNamedItem(
"grade").getNodeValue());
FaultDescription faultDescription = new FaultDescription();
faultDescription
.setFaulty_location_code(locationCode);
faultDescription
.setFaulty_type_code(categoryCode);
faultDescription
.setFaulty_grade_code(grade);
faultDescription.setId(id);
faultDescription.setDescription(desc);
faultList.add(faultDescription);
}
}
}
}
}
}
}
return faultList;
}
public void insert(FaultDescription faultDescription) throws SQLException {
connection = this.smcTemplate.getDataSource().getConnection();
connection.setAutoCommit(false);
ps = connection
.prepareStatement("INSERT INTO FAULTY_DESCRIPTION (id,faulty_location_code,faulty_type_code,description,faulty_grade_code,rstat_type_code) VALUES (?, ?, ?, ?, ?, 1)");
ps.setLong(1, faultDescription.getId());
ps.setLong(2, faultDescription.getFaulty_location_code());
ps.setLong(3, faultDescription.getFaulty_type_code());
ps.setString(4, faultDescription.getDescription());
ps.setLong(5, faultDescription.getFaulty_grade_code());
ps.executeUpdate();
connection.commit();
connection.setAutoCommit(true);
}
}
class FaultDescriptions {
long id;
long faulty_location_code;
long faulty_type_code;
String description;
long faulty_grade_code;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getFaulty_location_code() {
return faulty_location_code;
}
public void setFaulty_location_code(long faultyLocationCode) {
faulty_location_code = faultyLocationCode;
}
public long getFaulty_type_code() {
return faulty_type_code;
}
public void setFaulty_type_code(long faultyTypeCode) {
faulty_type_code = faultyTypeCode;
}
public long getFaulty_grade_code() {
return faulty_grade_code;
}
public void setFaulty_grade_code(long faultyGradeCode) {
faulty_grade_code = faultyGradeCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}