wuhaoi007 2014-05-06 12:46 采纳率: 0%
浏览 4298

Eclipse用JAVA连接Mysql 插入大量数据报错 Java heap space

public void Get_WayId_Ref_isWay() throws IOException
{

    PreparedStatement ps = null; 
    Connection con=null;
    BufferedReader br=new BufferedReader(new FileReader(Path.path_read_beijing));

    StringBuffer s=null;
    StringBuffer wayId=null;
    StringBuffer isWay=new StringBuffer("false");
    String regex_nd="\\d+";
    //用匹配<way id后面的数字
    String regex_id="(?<=id=\")\\d+";
    //读取整个文件信息
    try {

        Class.forName(driver).newInstance();
        con=DriverManager.getConnection(url,user,pwd);
        con.setAutoCommit(false);
        int count=0;
        int batchCount=1;
        while((s=new StringBuffer(br.readLine()))!=null)
        {
            count++;
            ArrayList<StringBuffer> tmp=new ArrayList<StringBuffer>();
            //找出way类的信息
            if(batchCount%100==0)
            {
                ps.clearBatch();
            }
            if(s.indexOf("<way")>0)
                {

                  //获得每一组WayId
                  Pattern p1=Pattern.compile(regex_id);
                  Matcher m1=p1.matcher(s);
                  if(m1.find())
                  {
                      wayId=new StringBuffer(m1.group());
                  }
                  while (s.indexOf("</way>") <= 0) 
                  {
                     count++;
                     s=new StringBuffer(br.readLine());
                     //获得该wayid下的refid
                     if(s.indexOf("<nd")>0)
                     {
                         System.out.println("所在行数:"+count);
                         Pattern p=Pattern.compile(regex_nd);
                         Matcher m=p.matcher(s);        
                        if(m.find())
                        {
                            tmp.add(new StringBuffer(m.group()));
                        }
                     }

                     //判断该way是否为道路
                    if(s.indexOf("<tag k=\"highway\"")>0||s.indexOf("<tag k=\"cycleway\"")>0||s.indexOf("<tag k=\"bridge\"")>0)
                    {
                        isWay=new StringBuffer("true");
                    }
                }

                 for(int i=0;i<tmp.size();i++)
                 {      

                        if(isWay.toString().equals("true"))
                        {
                            batchCount++;
                            StringBuffer sql1 = new StringBuffer();
                            sql1.append("insert into test_wayid(wayid,id,isway) values(");
                            sql1.append("'" +wayId+"'");
                            sql1.append(", '"+tmp.get(i)+"', '"+"1');");                        
                            ps=con.prepareStatement(sql1.toString());
                            ps.addBatch();

                        }
                        if(isWay.toString().equals("false"))
                        {
                            batchCount++;
                            StringBuffer sql1 = new StringBuffer();

                            sql1.append("insert into test_wayid(wayid,id,isway) values(");
                            sql1.append("'" +wayId+"'");
                            sql1.append(", '"+tmp.get(i)+"', '"+"1');");

ps=con.prepareStatement(sql1.toString());
ps.addBatch();

                        }
                        ps.executeBatch();
                        if(batchCount%100==0)
                        {
                            con.commit();
                        }

                    }



                }
        }
        con.commit();
    } catch (Exception e) {
        e.printStackTrace();
        // TODO: handle exception
    }finally{
        try {
            if(ps!=null) ps.close();
            if(con!=null) con.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }


    }

报的错误是:
java.sql.SQLException: java.lang.OutOfMemoryError: Java heap space
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:430)
at com.mysql.jdbc.PreparedStatement.getInstance(PreparedStatement.java:561)
at com.mysql.jdbc.ConnectionImpl.clientPrepareStatement(ConnectionImpl.java:1395)
at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4178)
at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4077)
at Wayid_To_Sql.Get_Map_Line_beta.Get_WayId_Ref_isWay(Get_Map_Line_beta.java:128)
at Wayid_To_Sql.Get_Map_Line_beta.main(Get_Map_Line_beta.java:35)

  • 写回答

2条回答 默认 最新

  • paperbook 2014-05-12 01:32
    关注

    堆空间不足,需要在jvm中把堆的大小设大一些-Xmx

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB yalmip 可转移负荷的简单建模出错,如何解决?
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?