import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;

public class Example {

    private static final String DRIVER = "org.mariadb.jdbc.Driver";

    private static final String URL = "jdbc:mariadb://localhost:3300/tmp";

    private static final String USER = "XXX";
    private static final String PASSWORD = "XXX";

    private static Connection getConnection(final String driver, final String url, final String user,
            final String password) throws SQLException {
        try {
            Class.forName(driver);
        } catch (final ClassNotFoundException e) {
            throw new SQLException(e.getMessage());
        }
        return DriverManager.getConnection(url, user, password);
    }

    private static void executeUpdate(final Connection connection, final String... sqls) throws SQLException {
        Statement stmt = null;

        try {
            stmt = connection.createStatement();
            stmt.setFetchSize(Integer.MIN_VALUE);
            for (final String sql : sqls) {
                stmt.addBatch(sql);
            }
            stmt.executeBatch();
        } catch (final SQLException e) {
            throw e;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (final SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static final void execute(final String[] sqls) throws Exception {
        Connection connection = null;
        try {
            connection = getConnection(DRIVER, URL, USER, PASSWORD);
            executeUpdate(connection, "DROP TEMPORARY TABLE IF EXISTS tmp.a",
                    "CREATE TEMPORARY TABLE tmp.a(a INT NOT NULL, b INT NOT NULL)ENGINE=MYISAM");
            executeUpdate(connection, sqls);
            executeUpdate(connection, "DROP TEMPORARY TABLE IF EXISTS tmp.a");
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (final SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(final String[] args) throws Exception {
        final String[] sqls = new String[1024];
        final Random rand = new Random();
        final int max = 200000;
        for (int i = 0; i < sqls.length; i++) {
            sqls[i] = "INSERT INTO tmp.a VALUES (" + rand.nextInt(max) + "," + rand.nextInt(max) + ")";
        }
        for (int i = 0; i < 100; i++) {
            final Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        execute(sqls);
                    } catch (final Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }
}
