Created
October 29, 2013 15:08
-
-
Save 1ik/7216442 to your computer and use it in GitHub Desktop.
JDBC sql (mysql & sqlite) large data insert snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void mysqlinserttest() throws SQLException{ | |
Connection c = DB.getConnection(); | |
c.setAutoCommit(false); | |
String insertTableSQL = "INSERT INTO test" | |
+ "(email , phone) VALUES" | |
+ "(?,?)"; | |
PreparedStatement preparedStatement = c.prepareStatement(insertTableSQL); | |
for (int i =0; i<50000; i++) { | |
preparedStatement.setString(1, "email"+i); | |
preparedStatement.setString(2, "00000000"+i); | |
preparedStatement.addBatch(); | |
if(i!=0 && i%1000==0){//for every 1000 records, insert as a batch | |
preparedStatement.executeBatch(); | |
preparedStatement.clearBatch(); | |
c.commit(); | |
} | |
} | |
preparedStatement.executeBatch(); | |
c.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment