Example of Spring JdbcTemplate

We are assuming that you have created the following table inside the Oracle10g database.

    create table employee( 
    id number(10), 
    name varchar2(100), 
    salary number(10) 


This class contains 3 properties with constructors and setter and getters.

    package com.edureka; 
    public class Employee { 
    private int id; 
    private String name; 
    private float salary; 
    //no-arg and parameterized constructors 
    //getters and setters 


It contains one property jdbcTemplate and three methods saveEmployee(), updateEmployee and deleteEmployee().

    package com.edureka; 
    import org.springframework.jdbc.core.JdbcTemplate; 
    public class EmployeeDao { 
    private JdbcTemplate jdbcTemplate; 
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 
        this.jdbcTemplate = jdbcTemplate; 
    public int saveEmployee(Employee e){ 
        String query="insert into employee values( 
        return jdbcTemplate.update(query); 
    public int updateEmployee(Employee e){ 
        String query="update employee set  
        name='"+e.getName()+"',salary='"+e.getSalary()+"' where id='"+e.getId()+"' "; 
        return jdbcTemplate.update(query); 
    public int deleteEmployee(Employee e){ 
        String query="delete from employee where id='"+e.getId()+"' "; 
        return jdbcTemplate.update(query); 


The DriverManagerDataSource is used to contain the information about the database such as driver class name, connnection URL, username and password.

There are a property named datasource in the JdbcTemplate class of DriverManagerDataSource type. So, we need to provide the reference of DriverManagerDataSource object in the JdbcTemplate class for the datasource property.

Here, we are using the JdbcTemplate object in the EmployeeDao class, so we are passing it by the setter method but you can use constructor also.

    <?xml version="1.0" encoding="UTF-8"?> 
    <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> 
    <property name="username" value="system" /> 
    <property name="password" value="oracle" /> 
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
    <property name="dataSource" ref="ds"></property> 
    <bean id="edao" class="com.edureka.EmployeeDao"> 
    <property name="jdbcTemplate" ref="jdbcTemplate"></property> 


This class gets the bean from the applicationContext.xml file and calls the saveEmployee() method. You can also call updateEmployee() and deleteEmployee() method by uncommenting the code as well.

    package com.edureka; 
    import org.springframework.context.ApplicationContext; 
    import org.springframework.context.support.ClassPathXmlApplicationContext; 
    public class Test { 
    public static void main(String[] args) { 
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); 
        EmployeeDao dao=(EmployeeDao)ctx.getBean("edao"); 
        int status=dao.saveEmployee(new Employee(102,"Amit",35000)); 
        /*int status=dao.updateEmployee(new Employee(102,"Sonoo",15000));
        /*Employee e=new Employee();
        int status=dao.deleteEmployee(e);