Spring JdbcDaoSupport, DBCP, MySQL demonstration

Following up from the Spring MySQL JdbcDaoSupport demonstration example, we augment this to add DBCP support in our application.

All we have to do is first Add the DBCP dependency in the POM file:

    <dependency>
    	<groupId>commons-dbcp</groupId>
    	<artifactId>commons-dbcp</artifactId>
    	<version>1.4</version>
    </dependency>

and then add the DBCP related datasource in the datasource bean config file:

<bean id="dataSourceDBCP" 
         class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="${jdbc.driverClassName}" />
	<property name="url" value="${jdbc.url}" />
	<property name="username" value="${jdbc.username}" />
	<property name="password" value="${jdbc.password}" />
	<property name="initialSize" value="${dbcp.initialSize}" />
	<property name="maxActive" value="${dbcp.maxActive}" />
</bean>

In this bean we setup our database connection pool to have an initial size of 5 db connection that can be increased up to 10 connections.

Then, thanks to the dependency injection magic, all we have to do is change the name of the dataSource bean to the DAO bean config file:

   <bean id="personSupportDao" class="com.dimitrisli.springMySQL.dao.PersonDaoSupportImpl" >
   		<property name="dataSource" ref="dataSourceDBCP"></property>
   </bean>

The source code can be found <a href="https://github.com/dimitrisli/SpringJdbcSupportMySQL">in this Github repository</a>.

Spring JdbcDaoSupport, MySQL demonstration

This is a variation of the Spring MySQL Jdbc demonstration¬†article that we will alter to use Spring’s JdbcDaoSupport abstract¬†Class.

Our DaoSupport Class bean will read now:

package com.dimitrisli.springMySQL.dao;

import java.util.List;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.dimitrisli.springMySQL.model.Person;


public class PersonDaoSupportImpl extends JdbcDaoSupport implements PersonDao{

	public void createPerson(String name, String surname) {
		getJdbcTemplate().update("INSERT INTO PERSON_RECORD (NAME,SURNAME) VALUES (?,?)",
				new Object[]{name,surname});
	}

	public List<Person> selectPerson(String name, String surname) {
		return getJdbcTemplate().query("SELECT NAME,SURNAME FROM PERSON_RECORD WHERE NAME=? AND SURNAME=?",
				new Object[] {name,surname},
				new PersonRowMapper());
	}

	public List<Person> selectAll() {
		return getJdbcTemplate().query("SELECT NAME,SURNAME FROM PERSON_RECORD",
				new PersonRowMapper());
	}

	public void deletePerson(String name, String surname) {
		getJdbcTemplate().update("DELETE FROM PERSON_RECORD WHERE NAME=? AND SURNAME=?",
				new Object[]{name,surname});
	}

	public void deleteAll() {
		getJdbcTemplate().update("DELETE FROM PERSON_RECORD");
	}

	
}

while the bean declaration of this Class is similar in that it gets the dataSource injected:


   <bean id="personSupportDao" class="com.dimitrisli.springMySQL.dao.PersonDaoSupportImpl" >
   		<property name="dataSource" ref="dataSource"></property>
   </bean>

The difference is that we don’t need in our class a setter method since super class JdbcDaoSupport is taking care of that.

The code can be found in this Github repository.