Spring Data - SimpleJdbcTemplate

Spring Data -SimpleJdbcTemplate

Spring 3 JDBC supports the java 5 feature var-args (variable argument) and autoboxing by the help of SimpleJdbcTemplate class. SimpleJdbcTemplate class wraps the JdbcTemplate class and provides the update method where we can pass arbitrary number of arguments

int update(String sql,Object... parameters)

here We should pass the parameter values in the update method in the order they are defined in the parameterized query

//File : StudentDao.java
public class StudentDao {
	private SimpleJdbcTemplate jdbcTemplate;
	public StudentDao(SimpleJdbcTemplate jdbcTemplate) { 
 this.jdbcTemplate = jdbcTemplate;
	}

	public int updateStudent(Student e) {
 String query = "update student set name=? where sno=?";
 return jdbcTemplate.update(query, e.getName(), e.getSno());
	}
}
//SpringConfig.xml	
<bean id="jtemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
	<constructor-arg ref="ds"></constructor-arg>
</bean>
//File: JdbcTestApplication.java
public class JdbcTestApplication {
	public static void main(String[] args) {
 Resource res = new ClassPathResource("SpringConfig.xml");
 BeanFactory factory = new XmlBeanFactory(res);

 StudentDao dao = (StudentDao) factory.getBean("dao");
 Student s = new Student(103, "RAM", "HYDERABAD");
 int i =dao.updateStudent(s);
 System.out.println(i);
	}
}

We have JPA & Hibernate integations in this topic. We will cover these things in Spring Integration with Other frameworks topic