Spring SimpleJdbcInsert Example

By Arvind Rai, August 09, 2022
On this page we will learn using Spring SimpleJdbcInsert class.
1. The SimpleJdbcInsert provides easy insert capabilities. All we need to provide is the name of the table and a Map containing the column names and the column values.
2. The SimpleJdbcInsert is a multithreaded, reusable object.
3. The SimpleJdbcInsert provides meta-data processing to simplify the code needed to construct a basic insert statement.
4. The SimpleJdbcInsert can be instantiated using following constructors.
SimpleJdbcInsert(DataSource dataSource)
SimpleJdbcInsert(JdbcTemplate jdbcTemplate) 
5. Find some methods of SimpleJdbcInsert class.
int execute(Map<String,?> args)
int execute(SqlParameterSource parameterSource)
Number executeAndReturnKey(Map<String,?> args)
KeyHolder executeAndReturnKeyHolder(Map<String,?> args)
int[] executeBatch(Map<String,?>... batch)
SimpleJdbcInsert usingColumns(String... columnNames)
SimpleJdbcInsertOperations withoutTableColumnMetaDataAccess()
SimpleJdbcInsert usingGeneratedKeyColumns(String... columnNames)
SimpleJdbcInsert withTableName(String tableName) 

Example using Spring Boot

PersonDAO.java
package com.concretepage;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

@Repository
public class PersonDAO {
  @Autowired
  private JdbcTemplate jdbcTemplate;
  
  private SimpleJdbcInsert simpleJdbcInsert;
  
  @Autowired
  private void setSimpleJdbcInsert(JdbcTemplate jdbcTemplate) {
	simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
  }
  
  public void addPerson(Person p) {
	Map<String, Object> params = new HashMap<String, Object>();
	params.put("name", p.getName());
	params.put("age", p.getAge());
	
	KeyHolder keyHolder = simpleJdbcInsert
	    .withTableName("person")
	    .usingColumns("name", "age")
	    .usingGeneratedKeyColumns("id")
	    .withoutTableColumnMetaDataAccess()	    
	    .executeAndReturnKeyHolder(params);
	int id = keyHolder.getKey().intValue();
	p.setId(id);
	System.out.println(id);
  }  
} 
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/concretepage
spring.datasource.username=root
spring.datasource.password=cp
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 
Person.java
package com.concretepage;
public class Person {
    private int id;
	private String name;
	private int age;
	public Person(String name, int age) {
	  this.name = name;
	  this.age = age;
	}
	public int getId() {
	  return id;
	}
	public void setId(int id) {
	  this.id = id;
	}
	public String getName() {
	  return name;
	}
	public int getAge() {
	  return age;
	}
	@Override
	public String toString() {
	  return id + " - " + name + " - " + age;
	}
} 
Table: person
CREATE TABLE `person` (
	`id` INT(5) NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(100) NOT NULL,
	`age` INT(3) NOT NULL,
	PRIMARY KEY (`id`)
) 
pom.xml
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>6.0.5</version>
	</dependency>
</dependencies> 
MyApplication.java
package com.concretepage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class MyApplication {
  public static void main(String[] args) {
	ApplicationContext ctx = SpringApplication.run(MyApplication.class, args);
	PersonDAO personDAO = ctx.getBean(PersonDAO.class);

	Person p = new Person("Shiva", 30);
	personDAO.addPerson(p);
	System.out.println(p);
  }
} 

Reference

Class SimpleJdbcInsert

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us