Which JDBC interface is used for batch updates?

Java MCQ: Which JDBC interface is used for batch updates?

a) Statement
b) PreparedStatement
c) BatchStatement
d) BatchUpdate

Answer:

a) Statement

Explanation:

The Statement interface in JDBC is used for batch updates. Batch updates allow you to execute multiple SQL statements as a single batch, which can improve performance by reducing the number of database round-trips.

Here’s an example of using Statement for batch updates:

Statement stmt = conn.createStatement();
stmt.addBatch("INSERT INTO employees (name, role) VALUES ('John', 'Manager')");
stmt.addBatch("INSERT INTO employees (name, role) VALUES ('Jane', 'Developer')");
stmt.addBatch("INSERT INTO employees (name, role) VALUES ('Jack', 'Analyst')");
int[] results = stmt.executeBatch();

In this example, three SQL INSERT statements are added to a batch using addBatch(). The executeBatch() method is then called to execute all the statements as a single batch.

Batch updates are useful for efficiently executing a large number of similar SQL statements, such as inserting multiple records or updating multiple rows in a database.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top