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

php中文网
发布: 2016-06-06 09:44:43
原创
2217人浏览过

mysql大量数据java

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();

秒哒
秒哒

秒哒-不用代码就能实现任意想法

秒哒 535
查看详情 秒哒
                        }                        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)

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号