
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;

public class FillTable {

    // Total number of rows to insert
    private static final int NUM_ROWS = 100000;
    // Rows to insert with a single insert statement (make sure NUM_ROWS is a mutliple of this)
    private static final int ROWS_PER_INSERT = 1;
    // Number of insert statements addBatch()ed between executeBatch()
    private static final int BATCH_SIZE = 1024;

    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.put("user", "mySQL64621");
        props.put("password", "1234");
        props.put("useServerPrepStmts", "true");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/mySQL64621", props);
        connection.setAutoCommit(false);
        connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);

        PreparedStatement dropTable = connection.prepareStatement("DROP TABLE IF EXISTS test");
        dropTable.execute();
        connection.commit();

        PreparedStatement createTable = connection.prepareStatement("CREATE TABLE test ("
                + "field0 BIGINT, field1 BIGINT, field2 BIGINT, field3 BIGINT,"
                + "field4 BIGINT, field5 BIGINT, field6 BIGINT, field7 BIGINT,"
                + "field8 BIGINT, field9 BIGINT, field10 BIGINT, field11 BIGINT,"
                + "field12 BIGINT, field13 BIGINT, field14 BIGINT, field15 BIGINT) ENGINE = MYISAM");

        createTable.execute();


        StringBuilder str = new StringBuilder();
        str.append("INSERT INTO test ("
                + "field0, field1, field2, field3, "
                + "field4, field5, field6, field7, "
                + "field8, field9, field10, field11, "
                + "field12, field13, field14, field15"
                + ") VALUES ");
        for (int i = 0; i < ROWS_PER_INSERT; i++) {
            if (i > 0) {
                str.append(", ");
            }
            str.append("("
                    + "?,?,?,?,"
                    + "?,?,?,?,"
                    + "?,?,?,?,"
                    + "?,?,?,?)");
        }

        PreparedStatement insert = connection.prepareStatement(str.toString());

        long startTime = System.currentTimeMillis();
        long sillyCounter = 0;
        for (int i = 0; i < NUM_ROWS / ROWS_PER_INSERT; i++) {
            for (int row = 0; row < ROWS_PER_INSERT; row++) {
                for (int field = 0; field < 16; field++) {
                    insert.setLong(row * 16 + field + 1, sillyCounter++);
                }
            }

            //insert.execute();
            
            insert.addBatch();
            if (i % BATCH_SIZE == 0) {
                insert.executeBatch();
            }
        }
        
        insert.executeBatch();
        
        connection.commit();
        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;
        System.out.println(duration);
        connection.close();
    }
}
