简体   繁体   English

java.lang.OutOfMemoryError:批量插入mysql数据库期间Java堆空间错误?

[英]java.lang.OutOfMemoryError: Java heap space error during bulk insert to mysql database?

package textfileimport;
import catalog.Root;
import java.io.*;
import java.sql.PreparedStatement;

public class Textfileimport {


    public static void main(String[] args)throws Exception {


        Root oRoot = null;

        PreparedStatement oPrStmt = null;

        FileReader in = null;

        BufferedReader br=null;

        try {
            oRoot = Root.createDbConnection(null);  
            in = new FileReader("C:/Users/i2cdev001/Desktop/snomedinfo_data.txt");      

            br = new BufferedReader(in);
            String strLine;

            while ((strLine = br.readLine()) != null){

                String [] splitSt =strLine.split("\\t");
                String dat1="",dat2="",dat3="",dat4="",dat5="",dat6="",dat7="",dat8="",dat9="";
                dat1=splitSt[0];
                dat2=splitSt[1];
                dat3=splitSt[2];
                dat4=splitSt[3];
                dat5=splitSt[4];
                dat6=splitSt[5];
                dat7=splitSt[6];
                dat8=splitSt[7];
                dat9=splitSt[8];        

                String sql = "INSERT INTO textfiledata (col1,col2,col3,col4,col5,col6,col7,col8,col9) VALUES( ?, ?, ?,?,?,?,?,?,?)";
                oPrStmt = oRoot.con.prepareStatement(sql);
                oPrStmt.setString(1, dat1);
                oPrStmt.setString(2,dat2);
                oPrStmt.setString(3, dat3);
                oPrStmt.setString(4, dat4);  
                oPrStmt.setString(5, dat5);
                oPrStmt.setString(6, dat6);
                oPrStmt.setString(7, dat7);
                oPrStmt.setString(8, dat8);
                oPrStmt.setString(9, dat9);
                oPrStmt.executeUpdate();
            }

            System.out.println("sucessfully imported");
        }
        catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
        } finally {
            oPrStmt = Root.EcwClosePreparedStatement(oPrStmt);
            br.close();
            in.close();
            oRoot = Root.closeDbConnection(null, oRoot);
        }
    }

}

iam trying to read around 1383709 lines from a text file and insert them into my mysql table.I get a java heap space error when i run this application.how do i solve this problem.I have already tried setting VM arguments in the configurations menu in eclipse with no success. 我试图从文本文件中读取1383709行并将它们插入到我的mysql表中。运行此应用程序时出现Java堆空间错误。我该如何解决此问题。我已经尝试在配置菜单中设置VM参数日食没有成功。

Use a simple text scanner which can parse primitive types and strings using regular expressions, instead of using string operations and buffered reader. 使用简单的文本扫描器,该扫描器可以使用正则表达式解析原始类型和字符串,而不是使用字符串操作和缓冲的阅读器。 This should use less memory. 这应该使用较少的内存。 A Scanner is safe if not multithreaded and can be used with no synchronization. 如果不是多线程的,则扫描程序是安全的,并且可以不同步使用。

Also try using a batch execute, something like below. 也可以尝试使用批处理执行,如下所示。

final FileInputStream fileInputStream = null;
final Scanner inputScanner = null;
final String sql = "INSERT INTO textfiledata (col1,col2,col3,col4,col5,col6,col7,col8,col9) VALUES( ?, ?, ?,?,?,?,?,?,?)";
oPrStmt = db.prepareStatement(sql);
db.setAutoCommit(false); 
try {
    fileInputStream = new FileInputStream(path);
    inputScanner = new Scanner(fileInputStream);
    while (inputScanner.hasNextLine()) {
        String line = inputScanner.nextLine();
        Scanner s = new Scanner(line).useDelimiter("\\t");
        int i =1;
        while(s.hasNext()) {
         String dat = s.next();
         oPrStmt.setString(i++,dat);
        }
        oPrStmt.addBatch();
        s.close();
    }
    if (inputScanner.ioException() != null) {
        throw inputScanner.ioException();
    }
    oPrStmt.executeBatch();
    db.commit();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
        e.printStackTrace();
    }  finally {
       if (fileInputStream != null) {
         fileInputStream.close();
       }
       if (inputScanner != null) {
         inputScanner.close();
       }
       oPrStmt.close();
       db.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM