Spring SimpleJdbcTemplate example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on"><b>SimpleJdbcTemplate <&sol;b>combines the most frequently used operations of <b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-jdbctemplate-in-spring-chapter-33&period;html">JdbcTemplate<&sol;a><&sol;b> and <b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-namedparameterjdbctemplate-in&period;html">NamedParameterJdbcTemplate<&sol;a><&sol;b>&period;<br &sol;>&NewLine;&nbsp&semi; <br &sol;>&NewLine;<b>Popular Spring Tutorials<&sol;b><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;06&sol;spring-30-baby-step-to-learn&period;html">Spring Tutorial<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-web-mvc-framework-chapter-38&period;html">Spring MVC Web Tutorial<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2016&sol;06&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&period;html">Spring Boot Tutorial<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-spring-jdbc-framework-chapter-32&period;html">Spring JDBC Tutorial<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;07&sol;introduction-to-aop-in-spring&period;html">Spring AOP Tutorial<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;02&sol;spring-security-take-baby-step-to-secure&period;html">Spring Security Tutorial<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<p>The <b>SimpleJdbcTemplate <&sol;b>has all the features of old <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-jdbctemplate-in-spring-chapter-33&period;html">JdbcTemplate<&sol;a> and also support some features of Java 5 i&period;e<b> varargs and autoboxing<&sol;b>&period; It best suited when you need not to access all the feature of <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-jdbctemplate-in-spring-chapter-33&period;html">JdbcTemplate<&sol;a>&period; It has a simpler API and basically construct to support java 5&period;Thats why it has more method to exploit varargs&period;<&sol;p>&NewLine;<p> The <i><b>getJdbcOperations&lpar;&rpar;<&sol;b><&sol;i> method is used to access those methods which are defined in <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-jdbctemplate-in-spring-chapter-33&period;html">JdbcTemplate<&sol;a>&period; You have to call these method on SimpleJdbcTemplate &period; <b>The main drawback is that you need to cast these methods as the methods on JdbcOperations interface are not generic<&sol;b>&period;<&sol;div>&NewLine;<p>&NewLine;Here are few examples to show how to use <b>SimpleJdbcTemplate <&sol;b>query&lpar;&rpar; methods to query or extract data from database&period; <b>In <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-jdbctemplate-in-spring-chapter-33&period;html">JdbcTemplate<&sol;a> query&lpar;&rpar;&comma; you need to manually cast the returned result to desire object type&comma; and pass an Object array as parameters&period; In SimpleJdbcTemplate&comma; it is more user friendly and simple&period;<&sol;b><&sol;p>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p><b>&nbsp&semi;Note&colon; <&sol;b><b>The SimpleJdbcTemplate isn’t a replacement for JdbcTemplate&comma; it’s just a java5-friendly supplement to it&period; <&sol;b><&sol;div>&NewLine;<p>&NewLine;<b>JdbcTemplate Style&colon;-<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&sol;&sol; classic JdbcTemplate-style&period;&period;&period;&NewLine;private JdbcTemplate jdbcTemplate&semi;&NewLine;&NewLine;public void setDataSource&lpar;DataSource dataSource&rpar; &lbrace;&NewLine; this&period;jdbcTemplate &equals; new JdbcTemplate&lpar;dataSource&rpar;&semi;&NewLine;&rcub;&NewLine;&NewLine;public Actor findActor&lpar;String specialty&comma; int age&rpar; &lbrace;&NewLine;&NewLine; String sql &equals; "select id&comma; first&lowbar;name&comma; last&lowbar;name from T&lowbar;ACTOR" &plus; &NewLine; " where specialty &equals; &quest; and age &equals; &quest;"&semi;&NewLine; &NewLine; RowMapper mapper &equals; new RowMapper&lpar;&rpar; &lbrace;&NewLine; public Actor mapRow&lpar;ResultSet rs&comma; int rowNum&rpar; throws SQLException &lbrace;&NewLine; Actor actor &equals; new Actor&lpar;&rpar;&semi;&NewLine; actor&period;setId&lpar;rs&period;getLong&lpar;"id"&rpar;&rpar;&semi;&NewLine; actor&period;setFirstName&lpar;rs&period;getString&lpar;"first&lowbar;name"&rpar;&rpar;&semi;&NewLine; actor&period;setLastName&lpar;rs&period;getString&lpar;"last&lowbar;name"&rpar;&rpar;&semi;&NewLine; return actor&semi;&NewLine; &rcub;&NewLine; &rcub;&semi;&NewLine;&NewLine; &NewLine; &sol;&sol; notice the wrapping up of the argumenta in an array&NewLine; return &lpar;Actor&rpar; jdbcTemplate&period;queryForObject&lpar;sql&comma; new Object&lbrack;&rsqb; &lbrace;specialty&comma; age&rcub;&comma; mapper&rpar;&semi;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Same thing in the form of SimplrJdbcTemplate style&colon;-<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&sol;&sol; SimpleJdbcTemplate-style&period;&period;&period;&NewLine;private SimpleJdbcTemplate simpleJdbcTemplate&semi;&NewLine;&NewLine;public void setDataSource&lpar;DataSource dataSource&rpar; &lbrace;&NewLine; this&period;simpleJdbcTemplate &equals; new SimpleJdbcTemplate&lpar;dataSource&rpar;&semi;&NewLine;&rcub;&NewLine;&NewLine;public Actor findActor&lpar;String specialty&comma; int age&rpar; &lbrace;&NewLine;&NewLine; String sql &equals; "select id&comma; first&lowbar;name&comma; last&lowbar;name from T&lowbar;ACTOR" &plus; &NewLine; " where specialty &equals; &quest; and age &equals; &quest;"&semi;&NewLine; RowMapper mapper &equals; new RowMapper&lpar;&rpar; &lbrace; &NewLine; public Actor mapRow&lpar;ResultSet rs&comma; int rowNum&rpar; throws SQLException &lbrace;&NewLine; Actor actor &equals; new Actor&lpar;&rpar;&semi;&NewLine; actor&period;setId&lpar;rs&period;getLong&lpar;"id"&rpar;&rpar;&semi;&NewLine; actor&period;setFirstName&lpar;rs&period;getString&lpar;"first&lowbar;name"&rpar;&rpar;&semi;&NewLine; actor&period;setLastName&lpar;rs&period;getString&lpar;"last&lowbar;name"&rpar;&rpar;&semi;&NewLine; return actor&semi;&NewLine; &rcub;&NewLine; &rcub;&semi;&NewLine;&NewLine; &sol;&sol; notice the use of varargs since the parameter values now come &NewLine; &sol;&sol; after the RowMapper parameter&NewLine; return this&period;simpleJdbcTemplate&period;queryForObject&lpar;sql&comma; mapper&comma; specialty&comma; age&rpar;&semi;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Note&colon;<&sol;b><br &sol;>&NewLine;The <b>SimpleJdbcTemplate <&sol;b>class only offers a subset of the methods exposed on the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-jdbctemplate-in-spring-chapter-33&period;html"><b>JdbcTemplate <&sol;b><&sol;a>class&period; If you need to use a method from the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-jdbctemplate-in-spring-chapter-33&period;html">JdbcTemplate <&sol;a>that is not defined on the SimpleJdbcTemplate&comma; you can always access the underlying <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-jdbctemplate-in-spring-chapter-33&period;html">JdbcTemplate <&sol;a>by calling the <i>getJdbcOperations&lpar;&rpar;<&sol;i> method on the SimpleJdbcTemplate&comma; which then allows you to invoke the method that you want&period; The only downside is that the methods on the JdbcOperations interface are not generic&comma; so you are back to casting and so on&period;<&sol;p>&NewLine;<div style&equals;"text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<&sol;b><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-jdbctemplate-in-spring-chapter-33&period;html">Using JdbcTemplate<&sol;a><&sol;b><b> &vert;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;06&sol;spring-30-baby-step-to-learn&period;html">index<&sol;a>&vert; <&sol;b><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;dao-support-classes-in-spring-chapter-35&period;html">DAO Support Classes<&sol;a><&sol;b><b>&gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-namedparameterjdbctemplate-in&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-simplejdbcinsert-example&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '632'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago