论坛首页 入门技术论坛

dom4j从数据读2W条数据写入文件时内存溢出

浏览 2707 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-06-07  
DAO
主要是报内存溢出,我把写的操作放在循环里面的话,就只能把子节点写进去,没有了头和根节点,如果最后把整个document写进去的话,就内存溢出了,请大侠们指点指点啊,小弟在此谢过了
以下是代码:

package hrsys.out;

import org.dom4j.*;
import org.dom4j.io.*;
import org.xml.sax.SAXException;

import hrsys.db.dbcon;
import hrsys.outdata.Friends;

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;

public class FriendsViewByTableName
{

        private XMLWriter writer = null;
       
        //调用dom4j中的一个类来解决中文问题,在得到writer时把format传进去
        OutputFormat format = OutputFormat.createPrettyPrint();

        public  void viewData(Adm_chgtmp adm)
        {
        /** 指定XML编码 */
            format.setEncoding("GBK"); 
           
            String tableName = null;
            String columnName = null;
            String columnValue = null;
           
            Element friendElement = null;
            Element ageElement = null;
            Document document  = null;
            String sql = "";
            String sql1 = "";
           
            ResultSet rs = null;
            ResultSet rs1 = null;
            dbcon connect = null;
          
            String TMPID = adm.getTMPID();
            String TMPDESC = adm.getTMPDESC();
            String ALLROWS = adm.getALLROWS();
            String MARKISNEW = adm.getMARKISNEW();
        String DELETEON = adm.getDELETEON();
        String TMPDATA =adm.getTMPDATA();
            String ISNEW = adm.getISNEW();
        Timestamp LASTUPDATE = adm.getLASTUPDATE();
       
        List list = stringToken(TMPDATA);
        Iterator it  = list.iterator();
        try
        {
        document = DocumentHelper.createDocument();
       
        Element friendsElement = document.addElement("DATA");
       
        //document.setRootElement(friendsElement);
       
//         try {
// writer.processingInstruction("xml version=\"1.0\"", "encoding=\"GBK\"");
// } catch (SAXException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
        while(it.hasNext())
        {
        tableName = (String)it.next();
        sql ="select * from " + tableName +" where ISNEW = 1 ";
        sql1 = "select * from syscolumns where id=object_id('"+ tableName + "')" ;
        try{
        connect = new dbcon();                
                 rs = connect.executeQuery(sql);
                 rs1 = connect.executeQuery(sql1);
                 rs1.last();
                 int row = rs1.getRow();
                 String column[] = new String[row];
                 rs1.beforeFirst();
                
               
                System.out.println(row);
               
                while(rs1.next())
                     {
                for(int i=0;i<row;i++)
                     {
                column[i] = rs1.getString("name");
                rs1.next();
                     }
                     }
                
               
                
                      
                   while(rs.next())
                   {
                   friendElement = friendsElement.addElement(tableName);

                  for(int i = 0;i<column.length;i++)
                  {
                   columnName = column[i];
               
                   columnValue = rs.getString(columnName);
                   System.out.println("列名:"+columnName);
                   System.out.println("值:"+columnValue);
                   ageElement = friendElement.addElement(columnName);
                   ageElement.setText((columnValue+"").toString().trim());
                  }
                
//                   writer.write(friendElement);
//                   friendElement.clearContent();//释放这个Element的空间,不然会导致内存耗尽
                 //friendsElement.clearContent();
                   }
                   //writer.write(friendsElement);  

               } catch( SQLException e){
                   e.printStackTrace();
             
               } catch( Exception e){
                   e.printStackTrace();
         
               }
               finally
               {
                   try
                   {
                       if(rs!=null)
                       rs.close();             
                       if(rs1!=null)
                       rs1.close();
                       connect.closeConnect();
                   }
                   catch(SQLException e)
                   {
                       e.printStackTrace();
                     
                   }

               }
               writer = new XMLWriter(new FileWriter(new File("C:/exp/friend.xml"),true),format);
               writer.write(document);
                   System.out.println("创建文件成功");
       
        }
       
} catch (IOException e)
{
e.printStackTrace();
}
finally
             {
                     if(writer != null)
                     {
                             try
                             {
                                     writer.close();
                             }
                             catch(Exception err)
                             {
                                     err.printStackTrace();
                             }
                     }
             }
 
               
        }
        public List stringToken(String tableName)
        {
        List list = new ArrayList();
        StringTokenizer stok = new StringTokenizer(tableName,",");
        while(stok.hasMoreElements())
        {
        list.add(stok.nextToken());
        }
        return list;
        }
    
}

    
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics