Spring JDBC


  1. JdbcTemplate  
  2. NamedParameterJdbcTemplate
  3. SimpleJdbcTemplate

1) In the Spring bean configuration file you need to first configure a datasource and then inject it to the DAO class.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
<bean id="forumDAO" class="com.vaannila.dao.ForumDAOImpl">
<property name="dataSource" ref="dataSource"/> 

2) In the Spring bean configuration file you need to first configure a datasource and then inject it to the DAO class.

Initialise DataSourse in the DAOImpl and Call a SP (if req)
public void setDataSource(DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        this.procReadActor =
                new SimpleJdbcCall(jdbcTemplate)
                      new SqlParameter("in_id", Types.NUMERIC),
                      new SqlOutParameter("out_first_name", Types.VARCHAR),
                      new SqlOutParameter("out_last_name", Types.VARCHAR),
                      new SqlOutParameter("out_birth_date", Types.DATE)
 To GET using JdbcTemplate
String query = "SELECT * FROM FORUMS WHERE FORUM_ID=?";                                   
return (Forum) jdbcTemplate.queryForObject(query, new Object[] { Integer.valueOf(forumId) },
new RowMapper() {                                                                          
public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {                
return new Forum(resultSet.getInt("FORUM_ID"), resultSet.getString("FORUM_NAME"),          
  • JdbcTemplate is the classic Spring JDBC approach and the most popular. This "lowest level" approach and all others use a JdbcTemplate under the covers, and all are updated with Java 5 support such as generics and varargs.
  • NamedParameterJdbcTemplate wraps a JdbcTemplate to provide named parameters instead of the traditional JDBC "?" placeholders. This approach provides better documentation and ease of use when you have multiple parameters for an SQL statement.
  • SimpleJdbcTemplate combines the most frequently used operations of JdbcTemplate and NamedParameterJdbcTemplate.
  • SimpleJdbcInsert and SimpleJdbcCall optimize database metadata to limit the amount of necessary configuration. This approach simplifies coding so that you only need to provide the name of the table or procedure and provide a map of parameters matching the column names. This only works if the database provides adequate metadata. If the database doesn't provide this metadata, you will have to provide explicit configuration of the parameters.
  • RDBMS Objects including MappingSqlQuery, SqlUpdate and StoredProcedure requires you to create reusable and thread-safe objects during initialization of your data access layer. This approach is modeled after JDO Query wherein you define your query string, declare parameters, and compile the query. Once you do that, execute methods can be called multiple times with various parameter values passed in.