Home  >  Spring Core

Spring JDBC Integration Example| JdbcTemplate in Spring

By Arvind Rai, October 27, 2013
Spring connects with database via JDBC. Spring has a class named as JdbcTemplate that is created by passing the argument of DataSource. DataSource is created by configuring the database authentication parameter. JdbcTemplate has methods like query, execute and update etc.
In our example we have taken a dao named as SoldierDao and a bean Soldier. SoldierDao has a method to return the list of soldiers from the database. Find the complete example below.

Database Configuration for JdbcTemplate

spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <bean id="soldierDao" class="com.concretepage.dao.SoldierDao">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <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/test"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
</beans>
 

Dao and Bean used in Spring JDBC Integration

SoldierDao.java
package com.concretepage.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.concretepage.bean.Soldier;
@Repository
public class SoldierDao {
	private JdbcTemplate jdbcTemplate;
    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
    public List<Soldier> getSoldier(){
    	List<Soldier> list = new ArrayList<Soldier>();
    	list = this.jdbcTemplate.query("select name, age from soldier", new RowMapper() {
            public Soldier mapRow(ResultSet rs, int rowNum) throws SQLException {
                Soldier soldier = new Soldier();
                soldier.setName(rs.getString("name"));
                soldier.setAge(rs.getInt("age"));
                return soldier;
            }
        });
    	return list;
    }
}
 


Soldier.java
package com.concretepage.bean;
public class Soldier {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}
 

Run the Spring JDBC Integration Example

SpringTest.java
package com.concretepage;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.concretepage.bean.Soldier;
import com.concretepage.dao.SoldierDao;
public class SpringTest {
	public static void main(String[] args) {
		ApplicationContext  context = new ClassPathXmlApplicationContext("spring.xml");
		SoldierDao soldier = (SoldierDao)context.getBean("soldierDao");
		List<Soldier> list = soldier.getSoldier();
		for(Soldier s : list){
			System.out.println(s.getName() +"  "+ s.getAge());
		}
	}
}   
 
Output
Indrajeet  25
Samarjeet  28

Eclipse Configuration and Database Rows

Find the eclipse configuration. The below images is showing all the packages and classes used to run the example.

Spring JDBC Integration Example| JdbcTemplate in Spring

Find the database rows used to run example.

Spring JDBC Integration Example| JdbcTemplate in Spring

Download Source Code
dbc-template-spring-jdbc-integration-example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us