Building a datasource for a web of data

JDBC Template Batch update example, In the tutorial we have discussed about  batchUpdate()  method of class JdbcTemplate in Spring framework. The Batch update methods are used to executes multiple SQL updates query on a single JDBC Statement. The example given below consists of the code to delete and update data of the table Simultaneously.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
          <bean id="dataSource"
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
       <bean id="lobHandler">
        <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
        <bean id="nativeJdbcExtractor"
       <bean id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"/>

ApplicationContext ac = new ClassPathXmlApplicationContext(“context.xml”, Main.class):-ApplicationContext is the interface that is used to  provide Bean factory methods for accessing application components. Here we Creates an instance of this interface to access context.xml and

DataSource source = (DataSource) ac.getBean(“dataSource”):-Data source is an Interface which provides a way for connecting to the physical data source. Here we created datasource for making connection to our xml document in which we have declared the bean.

jt.batchUpdate(new String[]{“update employee set departement = ‘Finance#'”, “delete from employee where EmployeeId =31” }):-With the use of this method we are executing two SQLquery simultaneously. This method executes multiple Sql updates on a single JDBC statement.

import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
class Main {
    public static void main(String args[]) {
        try {
            ApplicationContext ac = new
                    ClassPathXmlApplicationContext("context.xml", Main.class);
            DataSource source = (DataSource) ac.getBean("dataSource");
            JdbcTemplate jt = new JdbcTemplate(source);
            jt.batchUpdate(new String[]{"update employee set departement = 'Finance#'",
                        "delete from employee where EmployeeId =31"
            System.out.println("Data updated successfully");
        } catch (Exception e) {

Output of the program

Data updated successfully
BUILD SUCCESSFUL (total time: 2 seconds)
  Table before updation    Table after updation

Download source code

Leave a Reply

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