<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on"><b>SimpleJdbcTemplate </b>combines the most frequently used operations of <b><a href="https://dineshonjava.com/2012/12/using-jdbctemplate-in-spring-chapter-33.html">JdbcTemplate</a></b> and <b><a href="https://dineshonjava.com/2012/12/using-namedparameterjdbctemplate-in.html">NamedParameterJdbcTemplate</a></b>.<br />
 ; <br />
<b>Popular Spring Tutorials</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html">Spring Tutorial</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html">Spring MVC Web Tutorial</a></b></li>
<li><b><a href="https://dineshonjava.com/2016/06/introduction-to-spring-boot-a-spring-boot-complete-guide.html">Spring Boot Tutorial</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/using-spring-jdbc-framework-chapter-32.html">Spring JDBC Tutorial</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/introduction-to-aop-in-spring.html">Spring AOP Tutorial</a></b></li>
<li><b><a href="https://dineshonjava.com/2013/02/spring-security-take-baby-step-to-secure.html">Spring Security Tutorial</a></b></li>
</ol>
<p>The <b>SimpleJdbcTemplate </b>has all the features of old <a href="https://dineshonjava.com/2012/12/using-jdbctemplate-in-spring-chapter-33.html">JdbcTemplate</a> and also support some features of Java 5 i.e<b> varargs and autoboxing</b>. It best suited when you need not to access all the feature of <a href="https://dineshonjava.com/2012/12/using-jdbctemplate-in-spring-chapter-33.html">JdbcTemplate</a>. It has a simpler API and basically construct to support java 5.Thats why it has more method to exploit varargs.</p>
<p> The <i><b>getJdbcOperations()</b></i> method is used to access those methods which are defined in <a href="https://dineshonjava.com/2012/12/using-jdbctemplate-in-spring-chapter-33.html">JdbcTemplate</a>. You have to call these method on SimpleJdbcTemplate . <b>The main drawback is that you need to cast these methods as the methods on JdbcOperations interface are not generic</b>.</div>
<p>
Here are few examples to show how to use <b>SimpleJdbcTemplate </b>query() methods to query or extract data from database. <b>In <a href="https://dineshonjava.com/2012/12/using-jdbctemplate-in-spring-chapter-33.html">JdbcTemplate</a> query(), you need to manually cast the returned result to desire object type, and pass an Object array as parameters. In SimpleJdbcTemplate, it is more user friendly and simple.</b></p>
<div align='center' id="ads-id"></div>
<p><b> ;Note: </b><b>The SimpleJdbcTemplate isn’t a replacement for JdbcTemplate, it’s just a java5-friendly supplement to it. </b></div>
<p>
<b>JdbcTemplate Style:-</b></p>
<pre class="highlight" name="code">// classic JdbcTemplate-style...
private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
 this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public Actor findActor(String specialty, int age) {

 String sql = "select id, first_name, last_name from T_ACTOR" + 
 " where specialty = ? and age = ?";
 
 RowMapper mapper = new RowMapper() {
 public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
 Actor actor = new Actor();
 actor.setId(rs.getLong("id"));
 actor.setFirstName(rs.getString("first_name"));
 actor.setLastName(rs.getString("last_name"));
 return actor;
 }
 };

 
 // notice the wrapping up of the argumenta in an array
 return (Actor) jdbcTemplate.queryForObject(sql, new Object[] {specialty, age}, mapper);
}
</pre>
<p>
<b>Same thing in the form of SimplrJdbcTemplate style:-</b></p>
<pre class="highlight" name="code">// SimpleJdbcTemplate-style...
private SimpleJdbcTemplate simpleJdbcTemplate;

public void setDataSource(DataSource dataSource) {
 this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}

public Actor findActor(String specialty, int age) {

 String sql = "select id, first_name, last_name from T_ACTOR" + 
 " where specialty = ? and age = ?";
 RowMapper mapper = new RowMapper() { 
 public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
 Actor actor = new Actor();
 actor.setId(rs.getLong("id"));
 actor.setFirstName(rs.getString("first_name"));
 actor.setLastName(rs.getString("last_name"));
 return actor;
 }
 };

 // notice the use of varargs since the parameter values now come 
 // after the RowMapper parameter
 return this.simpleJdbcTemplate.queryForObject(sql, mapper, specialty, age);
}
</pre>
<p>
<b>Note:</b><br />
The <b>SimpleJdbcTemplate </b>class only offers a subset of the methods exposed on the <a href="https://dineshonjava.com/2012/12/using-jdbctemplate-in-spring-chapter-33.html"><b>JdbcTemplate </b></a>class. If you need to use a method from the <a href="https://dineshonjava.com/2012/12/using-jdbctemplate-in-spring-chapter-33.html">JdbcTemplate </a>that is not defined on the SimpleJdbcTemplate, you can always access the underlying <a href="https://dineshonjava.com/2012/12/using-jdbctemplate-in-spring-chapter-33.html">JdbcTemplate </a>by calling the <i>getJdbcOperations()</i> method on the SimpleJdbcTemplate, which then allows you to invoke the method that you want. The only downside is that the methods on the JdbcOperations interface are not generic, so you are back to casting and so on.</p>
<div style="text-align: center;"><b><;<;</b><b><a href="https://dineshonjava.com/2012/12/using-jdbctemplate-in-spring-chapter-33.html">Using JdbcTemplate</a></b><b> |<a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html">index</a>| </b><b><a href="https://dineshonjava.com/2012/12/dao-support-classes-in-spring-chapter-35.html">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/using-namedparameterjdbctemplate-in/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-simplejdbcinsert-example/">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: '632'});
});
</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…