How to query using org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport

First of all you will need a pojo and a dao:

package net.istya.vrsbrazil.model;


public class Data implements Serializable{

private Long id;
 private String name;

public Long getId() {
 return id;
 public void setId(Long id) { = id;
 public String getName() {
 return name;
 public void setName(String name) { = name;


package net.istya.vrsbrazil.dao;

import net.istya.vrsbrazil.model.Data;

public interface DataDao {

public Data getFromId(Long id);


Now write a class that extends SimpleJdbcDaoSupport and implements your Dao:

package net.istya.vrsbrazil.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import net.istya.vrsbrazil.model.Data;
import net.istya.vrsbrazil.dao.DataDao;

import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class JdbcDataDao extends SimpleJdbcDaoSupport implements DataDao{

private Logger logger = Logger.getLogger(JdbcDataDao.class.getName());

 public Place getFromId(Long id) {"getFromId: " + id);
 String query = "select * from PLACE where ID= :id";

 Map<String, Long> args = new HashMap<String, Long>();

 args.put("id", id);

 SimpleJdbcTemplate template = getSimpleJdbcTemplate();

Data data= template.queryForObject(query, Data.class, args);

 return data;


What you need now is a context file, which in my case is WEB-INF/classes/jdbc/context.xml


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""

<bean id="dataDao" class="net.istya.vrsbrazil.jdbc.JdbcDataDao">

<property name="dataSource" ref="dataSource" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="${jdbc.driverClassName}"/>
 <property name="url" value="${jdbc.url}"/>
 <property name="username" value="${jdbc.username}"/>
 <property name="password" value="${jdbc.password}"/>
 <bean id="propertyConfigurer"
 <property name="locations">

 <bean id="transactionManager"
 <property name="dataSource" ref="dataSource" />



Now call the config from a main:


import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import  net.istya.vrsbrazil.jdbc.JdbcDataDao;

import  net.istya.vrsbrazil.dao.DataDao;
<pre>import net.istya.vrsbrazil.model.Data;</pre>

ApplicationContext context = new ClassPathXmlApplicationContext("jdbc/context.xml");
<pre>DataDao dao = (JdbcDataDao) context.getBean("dataDao");</pre>
Data data = dao.getFromId(0L);


Thank you,


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s