In this example, we can evaluate executebatch() method of the jdbc batch processing.
In this example, we can evaluate executebatch() method of the jdbc batch processing.In this example, we can evaluate executebatch() method of the jdbc batch processing. The executebatch() method are use to execute the batch on the statement.
Syntax:
statement.executeBatch();
In the syntax, statement is the object of the Statement class. The executeBatch() method execute all the SQL statements on created statement object.
In this example, we are discuss with SQL delete statement with batch. We are create connection with the data source as:
Connection connection =
null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/roseindia_jdbc_tutorials",
"root","root
");
Now we will create statement and set auto commit false as:
Statement stmt = connection.createStatement();
connection.setAutoCommit(false);
Now we will create SQL statement and add this statement in the batch as:
String insertquery1 =
"Delete from user where user_id=3";
stmt.addBatch(insertquery1);
Now execute this batch by using executeBatch() method on the created statement object as:
stmt.executeBatch();
The code of the executebatch() example is:
import java.sql.*; public class BatchExecuteBatch { public static void main(String[] args) { try{ Connection connection = null; Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/roseindia_jdbc_tutorial", "root","root"); try{ // Create statement object Statement stmt = connection.createStatement(); // Set auto-commit to false connection.setAutoCommit(false); // insert query String insertquery1 = "Delete from user where user_id=3"; stmt.addBatch(insertquery1); stmt.executeBatch(); System.out.println("Batch executeBatch() example." ); // connection commited connection.commit(); } catch (SQLException s){ System.out.println("SQL Exception " + s); } } catch (Exception e){ e.printStackTrace(); } } } |
Now run this example on eclipse IDE and see the output.
This eclipse console output is:
The database table output is: