Spring Batch Example

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.

context.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">
                                    destroy-method="close">




    

       class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/>
    


Main.java

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 Main.java.

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.context.support.ClassPathXmlApplicationContext;
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) {
e.printStackTrace();
        }
}
}

Output of the program

Data updated successfully
BUILD SUCCESSFUL (total time: 2 seconds)

Table before updation Table after updation

Download source code

0 comments:

Post a Comment