The batchUpdate() is a method provided by the JdbcTemplate class in Spring Boot that allows multiple SQL queries to be executed in a batch. It takes an SQL query string and a BatchPreparedStatementSetter object that specifies how to set the parameters for each query.
The batchUpdate() method then executes the SQL query for each set of parameters in a single round-trip to the database, improving performance by reducing the number of database calls.
This method is helpful when multiple queries need to be executed in a single transaction, or when a large number of queries need to be executed with minimal overhead.
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@Repository
public class JDBCTemplateRepo implements CommandLineRunner {
private final JdbcTemplate jdbcTemplate;
public JDBCTemplateRepo(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void batchUpdateRecords(int[] employeeIds, int[] newSalaries) {
String sql = "UPDATE employee SET employeeSalary = ? WHERE employeeId = ?";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setInt(1, newSalaries[i]);
ps.setInt(2, employeeIds[i]);
}
@Override
public int getBatchSize() {
return employeeIds.length/2; //set per your batch
}
});
}
@Override
public void run(String... args) throws Exception {
int empIdArr[] = {101,201,301,404,505,606,707};
int empSalaryArr[] = {1000,2000,3000,4000,5000,6000,7000};
batchUpdateRecords(empIdArr,empSalaryArr);
}
}
Facing issues? Have Questions? Post them here! I am happy to answer!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- [fix] Java NullPointerException ComparableTimSort countRunAndMakeAscending when sorting a List - Java
- Fix: SpringFramework BeanDefinitionValidationException: Could not find an init method named - Java
- Program 37: Store your name in a variable and print it - Python-Programs
- How to do screen recording on Mac - MacOS
- How to switch between sftp and Terminal shell - FTP
- How to reload zsh shell profile file? - zsh
- Add days/weeks/months/years to LocalDate in Java 8 and above examples - Java
- ls command to list only directories - Linux