Connecting database using Spring JdbcTemplate

Another salient feature of Spring is to connect and work with database at great ease which is what I am going to demonstrate.
This article is the basic to load and configure beans to interact with DB and aims to help people who are trying to user the spring ‘JdbcTemplate’ for the first time.

1) Download Spring from and extract it.
2) Create a normal Java project using ecplise.
3) Database I am using in MySQL so download Mysql driver class

There are several ways in which DB can be access.
JdbcTemplate is the classic Spring JDBC approach and the most popular. This “lowest level” approach and all others use a JdbcTemplate under the covers, and all are updated with Java 5 support such as generics and varargs.
NamedParameterJdbcTemplate wraps a JdbcTemplate to provide named parameters instead of the traditional JDBC “?” placeholders. This approach provides better documentation and ease of use when you have multiple parameters for an SQL statement.
SimpleJdbcTemplate combines the most frequently used operations of JdbcTemplate and NamedParameterJdbcTemplate.
SimpleJdbcInsert and SimpleJdbcCall optimize database metadata to limit the amount of necessary configuration. This approach simplifies coding so that you only need to provide the name of the table or procedure and provide a map of parameters matching the column names. This only works if the database provides adequate metadata. If the database doesn’t provide this metadata, you will have to provide explicit configuration of the parameters.
• RDBMS Objects including MappingSqlQuery, SqlUpdate and StoredProcedure requires you to create reusable and thread-safe objects during initialization of your data access layer. This approach is modeled after JDO Query wherein you define your query string, declare parameters, and compile the query. Once you do that, execute methods can be called multiple times with various parameter values passed in.

Extracted from Spring jdbc docs

The demo is done using JdbcTemplate.

1) Configure the Datasource in the “spring.xml”. One needs to configure the connection properties in the spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="jdbcDao" class="JDBC_Acess">
        <property name="dataSource" ref="dataSource"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/reco_engine"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>



2) Create a Dao class and configure the bean in the “spring.xml”. Here the class is “”. The datasource property created in step 1 is configured for the Dao class.
3) In JDBCDao class the object of JdbcTemplate is created with the constructor argument as Datasource configured in step 1.

public class JDBCDao {

	 private JdbcTemplate jdbcTemplate;

	    private void getUser(){
	    	String sql = "select * from users";
	    	List list = jdbcTemplate.query(sql, new UserMapper());
	    	System.out.println("Total Records "+ list.size());
	    	}catch(DataAccessException e){
	    private void insertUser(){
	    	String sql = "insert into users values ('21','spring,java,perl,oracle','golf,cooking,music,cricket','3','embedded','7','victor','mathews','team lead','NULL')";
	    	 System.out.println("record inserted succesffuly");
	    	}catch(DataAccessException e){
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		JDBCDao dao = (JDBCDao)context.getBean("jdbcDao");
	public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);

class UserMapper implements RowMapper{

	public User mapRow(ResultSet rs, int arg1) throws SQLException {

		User user = new User();
		return user;


5 thoughts on “Connecting database using Spring JdbcTemplate

  1. hello
    nice artical on spring jdbc connection… well can u know how to create jndi DB connection in spring using jboss 4.2 as an applicaiton server… please post on it urgent…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s