Skip to content

Spring JDBCTemplate Example

At my latest client engagement I’ve become the king of batch applications. Small, stand-alone applications run via a scheduler or a set of shell scripts that perform a heavy amount of data-munging.

The problem with being assigned batch applications is that they’re never allocated much time in the project plan; in fact sometimes they’re not found in requirements at all and just seem to crop up mid way through the project.

For these types of applications, Hibernate can be rather bulky and time consuming. But it’s still nice to work in a more object-oriented environment than simple JDBC stuff. Enter Spring’s JDBCTemplate.

Here’s an example of how I’ve used it:

Here is the simple spring config that I used:






	
	

			oracle.jdbc.driver.OracleDriver
		

			jdbc:oracle:thin:@server:1521:mydb
		

			user
		

			password
		
	

	
		
			 
		
	

	

			
		
	

Start with a bean that describes the data you wish to populate:

import java.sql.Date;

public class MyBean {

	String sLibbyFileName, prisonAddress;
	Date sentenceDate;
	int kRoveAccountNumber;

	public int getKRoveAccountNumber() {
		return kRoveAccountNumber;
	}
	public void setKRoveAccountNumber(int roveAccountNumber) {
		kRoveAccountNumber = roveAccountNumber;
	}
	public String getPrisonAddress() {
		return prisonAddress;
	}
	public void setPrisonAddress(String prisonAddress) {
		this.prisonAddress = prisonAddress;
	}
	public Date getSentenceDate() {
		return sentenceDate;
	}
	public void setSentenceDate(Date sentenceDate) {
		this.sentenceDate = sentenceDate;
	}
	public String getSLibbyFileName() {
		return sLibbyFileName;
	}
	public void setSLibbyFileName(String libbyFileName) {
		sLibbyFileName = libbyFileName;
	}

}

Put together a select statement to retrieve the data:

SELECT ACCOUNT_NUMBER, ADDRESS, FILE_NAME, SENTENCE_DATE FROM FBI_SENTENCE_DATA;

And then create your class (I’m skipping the part where you retrieve the myListMaker bean from Spring:

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class MyBeanTemplate {

	private JdbcTemplate jdbcTemplate;

	public static final String MY_QUERY=
		"SELECT ACCOUNT_NUMBER, ADDRESS, FILE_NAME, SENTENCE_DATE FROM FBI_SENTENCE_DATA WHERE FILE_NAME = ? AND ADDRESS = ?";

	public List getListofBeans( String name, String address) {
		List myList = getJdbcTemplate().query(MY_QUERY,
				new Object[] { name, address },
				new int[]{ Types.CHAR, Types.CHAR}, new RowMapper() {
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				MyBean bean = new MyBean();
				bean.setKRoveAccountNumber(rs.getInt("ACCOUNT_NUMBER"));
				bean.setPrisonAddress(rs.getString("ADDRESS"));
				bean.setSentenceDate(rs.getDate("SENTENCE_DATE"));
				bean.setSLibbyFileName(rs.getString("FILE_NAME"));
				return bean;
			}
		});
		return myList;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
}

The coolest part of this is of course that you can call a database and retrieve a list of POJOs in one method call.

More links:

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

5 Comments

  1. lumpynose wrote:

    I prefer iBatis.

    Tuesday, September 11, 2007 at 4:45 pm | Permalink
  2. eceppda wrote:

    Ok, why?

    Tuesday, September 11, 2007 at 5:22 pm | Permalink
  3. lumpynose wrote:

    iBatis isn’t quite so raw as using jdbc in your java code, and it’s not as heavyweight as hibernate. The last paragraph in the blue box here I think says it well.

    http://cwiki.apache.org/WICKET/ibatis.html

    Wednesday, September 12, 2007 at 11:53 pm | Permalink
  4. lincoln wrote:

    With out main class how is going to execute this application? where is the main class……….can any one tell

    Monday, April 21, 2008 at 6:01 am | Permalink
  5. Rachta wrote:

    You need to put in your classpath main void.

    Monday, April 21, 2008 at 12:22 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*