Home  >  Spring Core

SimpleJdbcTemplate Spring Example

By Arvind Rai, November 04, 2013
SimpleJdbcTemplate has been introduced for JDK 1.5. SimpleJdbcTemplate uses the varargs and autoboxing . SimpleJdbcTemplate can be used in the place of JdbcTemplate. The methods of SimpleJdbcTemplate supports JDK 1.5 where as JdbcTemplate does not completely follow JDK 1.5. In the below example we have used SimpleJdbcTemplate that is complete and running example.

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="farmarDao" class="com.concretepage.dao.FarmarDao">
        <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>
 


FarmarDao.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.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;
import com.concretepage.bean.Farmar;
@Repository
public class FarmarDao {
	private SimpleJdbcTemplate simpleJdbcTemplate;
    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }
    public List<Farmar> getFarmar(){
    	List<Farmar> list = new ArrayList<Farmar>();
    	list = this.simpleJdbcTemplate.query("select name, age from farmar", new RowMapper() {
            public Farmar mapRow(ResultSet rs, int rowNum) throws SQLException {
                Farmar farmar = new Farmar();
                farmar.setName(rs.getString("name"));
                farmar.setAge(rs.getInt("age"));
                return farmar;
            }
        });
    	return list;
    }
}
 


Farmar.java
package com.concretepage.bean;
public class Farmar {
	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;
	}
}
 


SpringTest.java
package com.concretepage;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.concretepage.bean.Farmar;
import com.concretepage.dao.FarmarDao;
public class SpringTest {
	public static void main(String[] args) {
		ApplicationContext  context = new ClassPathXmlApplicationContext("spring.xml");
		FarmarDao soldier = (FarmarDao)context.getBean("farmarDao");
		List<Farmar> list = soldier.getFarmar();
		for(Farmar s : list){
			System.out.println(s.getName() +"  "+ s.getAge());
		}
	}
}   
 


Output
Rajkumar  30
Sanjay  25
Raju  20
 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us