SimpleJdbcTemplate Spring Example
July 21, 2022
On this page we will learn using Spring SimpleJdbcTemplate
class.
1. The
SimpleJdbcTemplate
is a Java 5 based convenience wrapper for the classic JdbcTemplate
.
2. The
SimpleJdbcTemplate
takes advantage of varargs and autoboxing, and exposing only the most commonly required operations in order to simplify JdbcTemplate
usage.
3. The
SimpleJdbcTemplate
has been deprecated since Spring 3.1 in favor of JdbcTemplate
and NamedParameterJdbcTemplate
. They provide all the functionality of the SimpleJdbcTemplate
.
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>
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; } }
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; } }
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()); } } }
Rajkumar 30 Sanjay 25 Raju 20