When executing queries in batches using JDBC, you can add multiple statements to a batch and then execute them all at once for improved performance. Below are the examples updated to demonstrate how to run select, insert, and delete (purge) queries in batches.
1. Batch Insert Query
This example shows how to insert multiple employees into the "employees" table in a single batch operation.
String INSERT_SQL = "INSERT INTO employees (first_name, last_name, email, department) VALUES (?, ?, ?, ?)";
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement pstmt = conn.prepareStatement(INSERT_SQL)) {
// Adding multiple employees to the batch
pstmt.setString(1, "John");
pstmt.setString(2, "Doe");
pstmt.setString(3, "john.doe@example.com");
pstmt.setString(4, "Sales");
pstmt.addBatch();
pstmt.setString(1, "Jane");
pstmt.setString(2, "Smith");
pstmt.setString(3, "jane.smith@example.com");
pstmt.setString(4, "Marketing");
pstmt.addBatch();
pstmt.setString(1, "Bob");
pstmt.setString(2, "Johnson");
pstmt.setString(3, "bob.johnson@example.com");
pstmt.setString(4, "HR");
pstmt.addBatch();
// Execute the batch
int[] rowsAffected = pstmt.executeBatch();
System.out.println(rowsAffected.length + " row(s) inserted.");
} catch (SQLException e) {
e.printStackTrace();
}
2. Batch Delete Query
This example demonstrates how to delete multiple employees from the "employees" table in a single batch operation.
String DELETE_SQL = "DELETE FROM employees WHERE id = ?";
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement pstmt = conn.prepareStatement(DELETE_SQL)) {
// IDs of employees to delete
int[] idsToDelete = {1, 2, 3}; // Example IDs
for (int id : idsToDelete) {
pstmt.setInt(1, id);
pstmt.addBatch(); // Add to the batch
}
// Execute the batch
int[] rowsAffected = pstmt.executeBatch();
System.out.println(rowsAffected.length + " row(s) deleted.");
} catch (SQLException e) {
e.printStackTrace();
}
3. Batch Select Query
The batch processing concept is generally not applicable for select queries since you typically want to retrieve data in response to specific criteria. However, if you need multiple specific selects, you can run them sequentially or use IN clauses. If you were to run multiple specific selects, it would look like this:
String SELECT_SQL = "SELECT * FROM employees WHERE id = ?";
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement pstmt = conn.prepareStatement(SELECT_SQL)) {
// IDs of employees to retrieve
int[] idsToSelect = {1, 2, 3}; // Example IDs
for (int id : idsToSelect) {
pstmt.setInt(1, id);
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
int employeeId = rs.getInt("id");
String firstName = rs.getString("first_name");
String lastName = rs.getString("last_name");
String email = rs.getString("email");
String department = rs.getString("department");
// Process the data as needed
System.out.println(employeeId + ", " + firstName + ", " + lastName + ", " + email + ", " + department);
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
Key Points:
- Batch Inserts: The
addBatch() method is used to add multiple insert statements for execution together.
- Batch Deletes: Similar to inserts, add multiple delete statements using
addBatch() and execute them once.
- Batch Selects: Not commonly used, but if you need to perform multiple selects based on IDs, loop through them sequentially.