<p>In this tutorial is about inserting data into database table using Spring SimpleJdbcInsert. Let&#8217;s start by looking at the<b> Spring SimpleJdbcInsert class with the minimal amount of configuration options</b>. You should instantiate the SimpleJdbcInsert in the data access layer&#8217;s initialization method.</p>
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;"> <img class="aligncenter wp-image-3248 size-full" src="https://dineshonjava.com/wp-content/uploads/2012/12/simple-jdbc-with-spring.jpg" width="663" height="472" /></div>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p><b>This approach minimize the database metadata</b>. This optimization is to bound the amount of necessary configuration. In this approach you need to provide <u>name of table and a map of parameters matching the column name</u>. This works only if database provide sufficient metadata. In case it is not provided adequately, then you have to provide explicit configuration of the parameters.</p>
<p>Using database metadata, SimpleJdbcInsert provide a easy configuration. The database metadata can be fetched via JDBC driver. It means SimpleJdbcInsert provide minimum configuration options. <i>The SimpleJdbcInsert must be initialize inside <b>setDataSource</b> method and table name can be set using &#8220;<b>withTableName</b>&#8221; method</i>.</p>
<h2><b>1. Inserting data using Spring SimpleJdbcInsert:-</b></h2>
<p>Let&#8217;s start by looking at the SimpleJdbcInsert class with the minimal amount of configuration options. You should instantiate the SimpleJdbcInsert in the data access layer&#8217;s initialization method. For this example, the initializing method is the setDataSource method. You do not need to subclass the SimpleJdbcInsert class; simply create a new instance and set the table name using the withTableName method. Configuration methods for this class follow the &#8220;fluid&#8221; style that returns the instance of the SimpleJdbcInsert, which allows you to chain all configuration methods. This example uses only one configuration method; you will see examples of multiple ones later.</p>
</div>
</div>
<div id="ads-id" align="center"></div>
<pre class="highlight">public class EmployeeDaoImpl implements EmpDao { 
 private SimpleJdbcTemplate simpleJdbcTemplate; 
 private SimpleJdbcInsert insertEmp; 
 
 public void setDataSource(DataSource dataSource) { 
 this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); 
 this.insertEmp = 
 new SimpleJdbcInsert(dataSource).withTableName("employee"); 
 } 
 
 public void create(String name, Integer age, Long salary) { 
 Map parameters = new HashMap(); 
 parameters.put("name", name); 
 parameters.put("age", age); 
 parameters.put("salary", salary); 
 insertEmp.execute(parameters); 
 } 
 
 // ... additional methods 
} 
</pre>
<p><b>The execute method used here takes a plain java.utils.Map as its only parameter. The important thing to note here is that the keys used for the Map must match the column names of the table as defined in the database. This is because we read the metadata in order to construct the actual insert statement.</b><br />
<b></b></p>
<h2><b>2. Retrieving auto-generated keys using Spring SimpleJdbcInsert:-</b></h2>
<p>When you create the <b>SimpleJdbcInsert</b>, in addition to specifying the table name, you specify the name of the generated key column with the <i><b>usingGeneratedKeyColumns</b></i> method.</p>
</div>
<p> ;</p>
<pre class="highlight">public class EmployeeDaoImpl implements EmpDao { 
 private SimpleJdbcTemplate simpleJdbcTemplate; 
 private SimpleJdbcInsert insertEmp; 
 
 public void setDataSource(DataSource dataSource) { 
 this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); 
 this.insertEmp = 
 new SimpleJdbcInsert(dataSource) 
 .withTableName("employee"); 
 .usingGeneratedKeyColumns("empid"); 
 } 
 
 public void create(String name, Integer age, Long salary) { 
 Map parameters = new HashMap(); 
 parameters.put("name", name); 
 parameters.put("age", age); 
 parameters.put("salary", salary); 
 Number empid = insertEmp.executeAndReturnKey(parameters); 
 } 
 
 // ... additional methods 
} 
</pre>
<h2><b>3. Specifying columns for a SimpleJdbcInsert:-</b></h2>
<p>You can limit the columns for an insert by specifying a list of column names with the <i><b>usingColumns</b></i> method:</p>
</div>
<pre class="highlight">public class EmployeeDaoImpl implements EmpDao { 
 private SimpleJdbcTemplate simpleJdbcTemplate; 
 private SimpleJdbcInsert insertEmp; 
 
 public void setDataSource(DataSource dataSource) { 
 this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); 
 this.insertEmp = 
 new SimpleJdbcInsert(dataSource) 
 .withTableName("employee"); 
 .usingColumns("name", "age", "salary") 
 .usingGeneratedKeyColumns("empid"); 
 } 
 
 public void create(String name, Integer age, Long salary) { 
 Map parameters = new HashMap(); 
 parameters.put("name", name); 
 parameters.put("age", age); 
 parameters.put("salary", salary); 
 Number empid = insertEmp.executeAndReturnKey(parameters); 
 } 
 
 // ... additional methods 
} 
</pre>
</div>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p><b>Spring JDBC Framework Tutorial</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/">Spring JDBC Framework</a></b></li>
<li><b><a href="https://dineshonjava.com/using-jdbctemplate-in-spring-chapter-33/">Using JdbcTemplate in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/implementing-rowmapper-chapter-34/">Implementing RowMapper</a></b></li>
<li><b><a href="https://dineshonjava.com/dao-support-classes-in-spring-chapter-35/">DAO Support Classes in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/using-namedparameterjdbctemplate-in/">Using NamedParameterJdbcTemplate in Spring with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/spring-simplejdbctemplate-example/">Spring SimpleJdbcTemplate example</a></b></li>
<li><b><a href="https://dineshonjava.com/spring-simplejdbcinsert-example/">Spring SimpleJdbcInsert example</a></b></li>
<li><b><a href="https://dineshonjava.com/stored-procedure-simplejdbccall-in/">Stored Procedure with SimpleJdbcCall in Spring</a></b></li>
</ol>
</div>
<div style="text-align: center;"><b><;<;</b><b><a href="https://dineshonjava.com/using-jdbctemplate-in-spring-chapter-33/">Using JdbcTemplate</a></b><b> |<a href="https://dineshonjava.com/spring-tutorial/">index</a>| </b><b><a href="https://dineshonjava.com/dao-support-classes-in-spring-chapter-35/">DAO Support Classes</a></b><b>>;>;</b></div>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-simplejdbctemplate-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/stored-procedure-simplejdbccall-in/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '631'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…