導數據

        早上,老大給我丟過來任務,讓我將配置文件(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;
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章