Subject | Bulk insert |
---|---|
Author | hhn10 |
Post date | 2003-07-09T21:54:43Z |
Hi all,
I am writing a program to insert big text files (up to 300 MB
containing approximatelty 750000 rows of data, the longest record is
1000 chars) into a firebird 1.5 rc3 database. I am a java-newbie and
wanted to ask the experts in this forum if my code looks good :-) One
more question: Is the call to commit() after the while loop correct or
should I check if I haven't just commited in the insertStr()-method? -
Do you have any suggestions on how to improve my code? How would you
choose the number of inserts between commits?
Thanks a lot, regards
Hans
try {
conn.setAutoCommit(false);
...
String s;
while ((s = in.readLine ()) != null) {
insertStr(s);
}
conn.commit();
conn.setAutoCommit(true);
}
catch (IOException _ex) {
_ex.printStackTrace ();
}
catch (SQLException _ex) {
_ex.printStackTrace ();
}
public void insertStr(String str){
try {
is.setString ( 1, str.substring( 0, 3).trim());
is.setString ( 2, str.substring( 3, 6).trim());
...
is.executeUpdate();
rc++;
if (rc % 1000 == 0) {
conn.commit();
}
} catch (SQLException e) {
showSQLException (e);
}
}
I am writing a program to insert big text files (up to 300 MB
containing approximatelty 750000 rows of data, the longest record is
1000 chars) into a firebird 1.5 rc3 database. I am a java-newbie and
wanted to ask the experts in this forum if my code looks good :-) One
more question: Is the call to commit() after the while loop correct or
should I check if I haven't just commited in the insertStr()-method? -
Do you have any suggestions on how to improve my code? How would you
choose the number of inserts between commits?
Thanks a lot, regards
Hans
try {
conn.setAutoCommit(false);
...
String s;
while ((s = in.readLine ()) != null) {
insertStr(s);
}
conn.commit();
conn.setAutoCommit(true);
}
catch (IOException _ex) {
_ex.printStackTrace ();
}
catch (SQLException _ex) {
_ex.printStackTrace ();
}
public void insertStr(String str){
try {
is.setString ( 1, str.substring( 0, 3).trim());
is.setString ( 2, str.substring( 3, 6).trim());
...
is.executeUpdate();
rc++;
if (rc % 1000 == 0) {
conn.commit();
}
} catch (SQLException e) {
showSQLException (e);
}
}