How can I store large amount of data from a database to XML (memory problem)?

前端 未结 4 659
情书的邮戳
情书的邮戳 2021-02-09 02:18

First, I had a problem with getting the data from the Database, it took too much memory and failed. I\'ve set -Xmx1500M and I\'m using scrolling ResultSet so that was taken care

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-09 02:55

    Why not write all data to one file and open the file with the "append" option? There is no need to read in all the data in the file if you are just going to write to it.

    However, this might be a better solution:

    PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream("data.xml")));
    
    while(rs.next()){
        i++;
        writer.print("\n\t");
        writer.print("\n\t\t" + Util.transformToHTML(rs.getInt("id")) + "");
        writer.print("\n\t\t" + Util.transformToHTML(rs.getInt("jed_id")) + "");
        writer.print("\n\t\t" + Util.transformToHTML(rs.getString("ime_pj")) + "");
        //...
    
        writer.print("\n\t");
    }
    
    writer.close();
    

    The BufferedOutputStream will buffer the data before printing it, and you can specify the buffer size in the constructor if the default value does not suit your needs. See the java API for details: http://java.sun.com/javase/6/docs/api/.

提交回复
热议问题