Execute a query for a result list, given static SQL. Uses a JDBC Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForList method with null as argument array. The results will be mapped to a List (one entry. May 26, 2018 Refer Select query using JdbcTemplate in Spring framework to see how to read data from DB using Select Query. Note that JdbcTemplate needs a DataSource in order to perform its management of fixed part like getting a DB connection, cleaning up resources. In this post Apache DBCP is used for providing pooled datasource and MYSQL is used as the.
![]()
Spring » on Feb 11, 2012 { 9 Comments } By Sivateja
Let us see how to use spring JdbcTemplate selectquery
![]() Files Required
Directory StructureSpringJdbcSelect.javaOurLogic.javaspconfig.xmlOutput:Explanation![]()
See the xml file.. actually our beam is id3, which needs JdbcTemplate to use the methods so i have given <ref bean=”id2″/>, and JdbcTemplate(id2) class need DriverManagerDataSource help so i have given <ref bean=”id1“/>, hope you got it.
You Might Also Like
In Spring JDBC development, you can use
JdbcTemplate and JdbcDaoSupport classes to simplify the overall database operation processes.
In this tutorial, we will reuse the last Spring + JDBC example, to see the different between a before (No JdbcTemplate support) and after (With JdbcTemplate support) example.
1. Example Without JdbcTemplate
Witout JdbcTemplate, you have to create many redundant codes (create connection , close connection , handle exception) in all the DAO database operation methods – insert, update and delete. It just not efficient, ugly, error prone and tedious.
2. Example With JdbcTemplate
With JdbcTemplate, you save a lot of typing on the redundant codes, becuase JdbcTemplate will handle it automatically.
See the different?
3. Example With JdbcDaoSupport
By extended the JdbcDaoSupport, set the datasource and JdbcTemplate in your class is no longer required, you just need to inject the correct datasource into JdbcCustomerDAO. And you can get the JdbcTemplate by using a getJdbcTemplate() method.
Note
In Spring JDBC development, it’s always recommended to use JdbcTemplate and JdbcDaoSupport , instead of coding JDBC code yourself.
Download Source CodeAbout the AuthormkyongFounder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.Comments![]() Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |