Use addBatch() method for each SQL statement and executeBatch() method for running the batch query.
Statement stmt = conn.createStatement();
stmt.addBatch("INSERT INTO Employees (Id, Name) VALUES(9517,Jane)");
stmt.addBatch("INSERT INTO Employees (Id, Name) VALUES(9518,Betty)");
stmt.executeBatch();
Use PreparedStatement to collect multiple statements for a single execution.
PreparedStatement stmt = conn.createStatement();
stmt.collect("INSERT INTO Employees (Id, Name) VALUES(:0,:1)");
stmt.collect("INSERT INTO Employees (Id, Name) VALUES(:2,:3)");
stmt.execute();
Separate statements by semicolon and use one of execute... methods
Statement stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO Employees (Id, Name) VALUES(9517,Jane);"+
"INSERT INTO Employees (Id, Name) VALUES(9518,Betty)");
Collect multiple statements in the list and use PreparedStatement for execution.
PreparedStatement stmt = conn.createStatement();
ArrayList listOfStatements = new ArrayList();
listOfStatements.add("INSERT INTO Employees (Id, Name) VALUES(:0,:1)");
listOfStatements.add("INSERT INTO Employees (Id, Name) VALUES(:2,:3)");
....
stmt.execute(listOfStatements);
Using Transactions we can accelerate performance of multiple statements
|