如何根据列文件将数据表拆分为多个文件?

时间:2017-10-16 11:49:01

标签: java xml database jdbc

我需要根据特定的列值将表转储到多个xml文件中 例如。 表

Progressive     ID   Content
1               A    Test1
2               A    Test2
3               A    Test3
4               B    Test1
5               B    Test2
6               B    Test3
7               C    Test1
8               C    Test2

我的问题是我要根据ID列的值将此表输出到xml文件中 我试图写一个算法来做到这一点,但我没有跟踪ID值 我的pesudo代码是

tmp_id = ""; 
while (resultset.next()) {
    if(resultset.getString("ID")!=tmp_id){
        create new xml file
        tmp_id =  resultset.getString("ID");
    }
    write the remain data corresponding to same id 
    *****if (current resultset.getString("ID")!= the next resultset.getString("ID")) 
    {
       close the xml file 
    }
}

我的主要问题是*****点我无法检查当前id是否与下一个id不同或者没有 请帮助我并纠正我的算法 out应该是那样的

file1.xml should have 
1               A    Test1
2               A    Test2
3               A    Test3


file2.xml should have 
4               B    Test1
5               B    Test2
6               B    Test3

file3.xml should have 
7               C    Test1
8               C    Test2

更新原始帖子以添加我现在使用的当前算法,以便更容易理解我的问题

int tmp_blockid = 0;
boolean flagIsOpen = false;
while (rs.next()) {

     //Check if the IDBLOCK is different from the tmp_blockid if yes write new file
     if (rs.getInt("FEIDBLC") != tmp_blockid) {                    
        flagIsOpen = true;
        outputFactory = XMLOutputFactory.newInstance();
        fileOutputStream = new 
        FileOutputStream(newFile("C:\\Users\\test\\Desktop\\Projects\\"+ "new.xml"));
        writer = outputFactory.createXMLStreamWriter(fileOutputStream);

        //Method to write the header of the xml
        writeHeader(progrS, rs, writer);
        System.out.println("Start");
        tmp_blockid = rs.getInt("FEIDBLC");
     }

     //Method to write the body of the xml
     writeBody(rs, writer);

     //This is where is my problem this condition should check if the
     //current id different from the next id on the next resultset row but i failed here 
     if (rs.getInt("FEIDBLC") != tmp_blockid) {
         flagIsOpen = false;

        //Method to write the footer of the xml
        writeFooter(writer);
       if (fileOutputStream != null) {
          fileOutputStream.close();
       }
       System.out.println("End");
     }
}
//to close the file in case if the resultset have some rows 
    if (flagIsOpen) {
         flagIsOpen = false;
         writeFooter(writer);
         if (fileOutputStream != null) {
             fileOutputStream.close();
         }                
     }

提前谢谢

2 个答案:

答案 0 :(得分:0)

使用一个变量(如Writer)写入XML文件。在创建新的之前直接关闭它。循环之后需要再关闭一次。请确保您的查询按ID列对记录进行排序。

tmp_id = ""; 
Writer out = null;
while (resultset.next()) {
    if(resultset.getString("ID")!=tmp_id){ 
        if(out != null) {
          out.close();
        }
        //create new xml file
        out = new FileWriter(...);
        tmp_id =  resultset.getString("ID");
    }
    write the remain data corresponding to same id 
}
if(out != null) {
  out.close();
}

答案 1 :(得分:0)

正确答案归功于@vanje

while (rs.next()) {

     //Check if the IDBLOCK is different from the tmp_blockid if yes write new file
     if (rs.getInt("FEIDBLC") != tmp_blockid) {
     //to close the file in case if the resultset have some rows 
    if (flagIsOpen) {
         flagIsOpen = false;
         writeFooter(writer);
         if (fileOutputStream != null) {
             fileOutputStream.close();
         }                
     }
        flagIsOpen = true;
        outputFactory = XMLOutputFactory.newInstance();
        fileOutputStream = new 
        FileOutputStream(newFile("C:\\Users\\test\\Desktop\\Projects\\"+ "new.xml"));
        writer = outputFactory.createXMLStreamWriter(fileOutputStream);

        //Method to write the header of the xml
        writeHeader(progrS, rs, writer);
        System.out.println("Start");
        tmp_blockid = rs.getInt("FEIDBLC");
     }

     //Method to write the body of the xml
     writeBody(rs, writer);


}
//to close the file in case if the resultset have some rows 
    if (flagIsOpen) {
         flagIsOpen = false;
         writeFooter(writer);
         if (fileOutputStream != null) {
             fileOutputStream.close();
         }                
     }