Skip to content

Instantly share code, notes, and snippets.

@1ik
Created October 29, 2013 15:08
Show Gist options
  • Save 1ik/7216442 to your computer and use it in GitHub Desktop.
Save 1ik/7216442 to your computer and use it in GitHub Desktop.
JDBC sql (mysql & sqlite) large data insert snippet
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