Home  >  Spring Core

SimpleJdbcInsert Spring Example

By Arvind Rai, November 07, 2013
SimpleJdbcInsert provides insert operation with lesser configuration. SimpleJdbcInsert should be initialized like JdbcTemplate. SimpleJdbcInsert insert uses prepared statement internally. We need to create a Map to fill data information and SimpleJdbcInsert.execute() takes that Map as parameter. In the below example we have a company bean. We will fill data in company table by SimpleJdbcInsert.

CompanyDao.java
package com.concretepage.dao;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import com.concretepage.bean.Company;
@Repository
public class CompanyDao {
	private SimpleJdbcInsert insertCompany;
    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.insertCompany = new SimpleJdbcInsert(dataSource).withTableName("company");
    }
    public void addCompnay(Company comp){
    	Map<String, Object> params = new HashMap<String, Object>(3);
    	params.put("name", comp.getName());
    	params.put("location",comp.getLocation());
    	params.put("no_of_emp",comp.getNoOfEmp());
    	insertCompany.execute(params);
    }
}
 
Company.java
package com.concretepage.bean;
public class Company {
  private String name;
  private  String location;
  private int noOfEmp;
  public Company(String name, String location, int noOfEmp){
	  this.name = name;
	  this.location = location;
	  this.noOfEmp = noOfEmp;
  }
	public String getName() {
		return name;
	}
	public String getLocation() {
		return location;
	}
	public int getNoOfEmp() {
		return noOfEmp;
	}
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
		
     <bean id="companyDao" class="com.concretepage.dao.CompanyDao">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
</beans>
 
SpringTest.java
package com.concretepage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.concretepage.bean.Company;
import com.concretepage.dao.CompanyDao;
public class SpringTest {
	public static void main(String[] args) {
		ApplicationContext  context = new ClassPathXmlApplicationContext("spring.xml");
		CompanyDao companyDao = (CompanyDao)context.getBean("companyDao");
		Company comp = new Company("Techwox", "Varanasi", 100);
		companyDao.addCompnay(comp);
		System.out.println("Done");
	}
}   
 
Table Schema
CREATE TABLE `company` (
	`name` VARCHAR(50) NULL DEFAULT NULL,
	`location` VARCHAR(50) NULL DEFAULT NULL,
	`no_of_emp` INT(11) NULL DEFAULT NULL
)
Download Source Code
simplejdbcinsert-spring-example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us