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.
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.