Batch Processing

Batch Processing

To perform Batch Updations in Spring JDBC we have to use the following method from JdbcTemplate class.

public int[] batchUpdate(String sql_prepared_Statement, BatchPreparedStatementSetter setter)

Where BatchPreparedStatementSetter interface contains the following two methods

public void setValues(PreparedStatement ps, int index)
public int getBatchSize()

Where setValues() method will be executed for each and every record to set values to the positional parameters existed in PreparedStatement object by getting values from the provided List.

E.g

package com.ashok.spring.dao.batchprocessing.dto;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private String empAddress;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public String getEmpAddress() {
      return empAddress;
   }

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + "]";
   }
}
package com.ashok.spring.dao.batchprocessing;

import java.util.List;
import com.ashok.spring.dao.batchprocessing.dto.Employee;
/**
 * 
 * @author ashok.mariyala
 *
 */
public interface EmployeeDao {
   public int[] insert(List<Employee> list);
}
package com.ashok.spring.dao.batchprocessing;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import com.ashok.spring.dao.batchprocessing.dto.Employee;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class EmployeeDaoImpl implements EmployeeDao {
   JdbcTemplate jdbcTemplate;

   public void setDataSource(DataSource dataSource) {
      jdbcTemplate = new JdbcTemplate(dataSource);
   }

   @Override
   public int[] insert(List<Employee> list) {
      int[] rowCounts = null;
      try {
         String sql = "insert into employee values(?,?,?)";
         rowCounts = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
               ps.setString(1, list.get(i).getEmpId());
               ps.setString(2, list.get(i).getEmpName());
               ps.setString(3, list.get(i).getEmpAddress());
            }

            @Override
            public int getBatchSize() {
               return list.size();
            }
         });
      } catch (Exception e) {
         e.printStackTrace();
      }
      return rowCounts;
   }
}
package com.ashok.spring.dao.batchprocessing;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.dao.batchprocessing.dto.Employee;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class TestSpringBatchProcessing {
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/dao/batchprocessing/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      EmployeeDao dao = (EmployeeDao) context.getBean("empDao");
      List<Employee> list = new ArrayList<>();
      Employee e1 = new Employee();
      e1.setEmpId("Emp0087");
      e1.setEmpName("Ashok Kumar");
      e1.setEmpAddress("Bhimavaram");
      list.add(e1);
      Employee e2 = new Employee();
      e2.setEmpId("Emp65894");
      e2.setEmpName("Vinod Kumar");
      e2.setEmpAddress("Banglore");
      list.add(e2);
      Employee e3 = new Employee();
      e3.setEmpId("Emp85974");
      e3.setEmpName("Naresh Kumar");
      e3.setEmpAddress("Hyderabad");
      list.add(e3);
      int[] rowCounts = dao.insert(list);
      for (int i = 0; i < rowCounts.length; i++) {
         System.out.println(rowCounts[i]);
      }
   }
}

Output

1
1
1
Batch Processing


Scroll to top