Monthly Archives: January 2015

Insert Operations in to Multiple Tables By Using SimpleJdbcInsert Class Of Spring Framework

When we want to insert data in table by using spring framework,We use usually use update() method from JdbcTemplate class.

public int insertSystemParameterDetail(String parameterName, String dcCode,
            String value) {
    String insertQuery = "insert into SYSTEM_PARAMETER_DETAIL (PARAMETER_NAME,CODE,VALUE) values(?,?,?)";
    int count = jdbcTemplate.update(insertQuery, new Object[] {
                parameterName, code, value });
        return count;



Also we can use SimpleJdbcInsert class from spring for inserting the data in to table.SimpleJdbcInsert is having definitely
better capability then JdbcTemplate class for inserting data in to tables.

public int insertSystemParameterDetail(String parameterName, String dcCode,
String value) {
Map<String, Object> dcMasterMap = new HashMap<>();

dcMasterMap.put("PARAMETER_NAME", parameterName);
dcMasterMap.put("CODE", code);
dcMasterMap.put("VALUE", value);

SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate)
int count = 0;

count = simpleJdbcInsert.execute(dcMasterMap);

return count;

Although making a  Java Data Access Object class for every table is a good practice in standard Java based enterprise application,There may be some situation in which you want to insert data in lot of tables and you may like to do less coding.In these situations, using of SimpleJdbcInsert can be a very good option.


public class GenericOperationDAOImpl implements GenericOperationDAO {

    private final Logger logger = Logger
    private JdbcTemplate jdbcTemplate;

    public int genericInsert(Map<String, Object> genericMap, String tableName) {
        if (logger.isInfoEnabled()) {
  "Inserting Data into table : " + tableName);
        SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate)
        int count = 0;

        count = simpleJdbcInsert.execute(genericMap);

        return count;


    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;




public class GenericDBOperationalAPIImpl implements GenericDBOperationalAPI {

private final Logger logger = Logger.getLogger(GenericDBOperationalAPIImpl.class);

GenericOperationDAO genericOperationDAO;

public int genericInsert(Map<String, Object> genericMap,String tableName) {

if (logger.isInfoEnabled()) {"Starting Insertion of Data into table : " + tableName);

genericOperationDAO.genericInsert(GenericMapUtils.getCarrierMap("1", "WL-ST"), "CARRIER");
genericOperationDAO.genericInsert(GenericMapUtils.getMasterMap("ABC", "UTC", "1", "Unregistered"),"MASTER");
genericOperationDAO.genericInsert(GenericMapUtils.getAttributeMap("DDS", "ABC"),"ATTRIBUTE");
genericOperationDAO.genericInsert(GenericMapUtils.getDeliveryAreaCodeMap("DAC"), "DELIVERY_AREA_CODE");


public void setGenericOperationDAO(GenericOperationDAO genericOperationDAO) {
                 this.genericOperationDAO = genericOperationDAO;




public class GenericMapUtils {

    public static final Map<String, Object> getCarrierMap(String proNumber,
            String carrierId) {
        Map<String, Object> carrierMap = new HashMap<>();

        carrierMap.put("PRO_NUMBER", proNumber);
        carrierMap.put("CARRIER_ID", carrierId);
        return carrierMap;

    public static final Map<String, Object> getMasterMap(String code,
            String timeZone, String defaultSystemFlag, String description) {
        Map<String, Object> masterMap = new HashMap<>();

        masterMap.put("TIMEZONE", timeZone);
        masterMap.put("DEFAULT_SYSTEM_FLAG", defaultSystemFlag);
        masterMap.put("DESCRIPTION", description);

        return masterMap;


    public static final Map<String, Object> getAttributeMap(
            String attributeName, String code) {

        Map<String, Object> attributeMap = new HashMap<>();

        attributeMap.put("ATTRIBUTE_NAME", attributeName);
        attributeMap.put("CODE", code);
        attributeMap.put("ATTRIBUTE_TYPE", "attT");
        attributeMap.put("ATTRIBUTE_ID", 32);
        return attributeMap;

    public static final Map<String, Object> getDeliveryAreaCodeMap(
            String deliveryArea) {

        Map<String, Object> deliveryAreaCodeMap = new HashMap<>();

        deliveryAreaCodeMap.put("DELIVERY_AREA_CODE", deliveryAreaCode);

        return deliveryAreaCodeMap;


and spring xml configuration is:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:context=""
xmlns:oxm="" xsi:schemaLocation="">

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />

<bean id="genericDBOperationalAPI" class="com.svg.move.api.GenericDBOperationalAPIImpl">
<property name="genericOperationDAO" ref="genericOperationDAO" />

<bean id="genericOperationDAO" class="com.svg.move.dao.GenericOperationDAOImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />

<bean id="dataSource"
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:Oracle" />
<property name="username" value="abc" />
<property name="password" value="abc" />


That is it,So by using SimpleJdbcInsert we can insert in to multiple tables with less and clean coding.This approach is very useful as well in writing junits which involves multiple tables insertion.