Purpose

Map a Java ResultSet to an Object. Plug it into many frameworks to automatically map ResultSets (eg DBUtils, Spring Framework,...).

The basic problem is that manual Java coding is usually required to convert a JDBC ResultSet (containing named data fields) to an Object (also containing a set of corresponding data fields or 'set' methods).

There are existing simple Java ResultSet row mapping solutions scattered across various frameworks. For example the DBUtils Apache commons project has a BeanProcessor that does a basic job. It is good for a simple mapping, however not many (any?) meet following challenges:

  1. Inheritance - a ResultSet may map to different Objects of the same parent over different rows
  2. Aggregates - a ResultSet may contain the data to build some/all of the aggregate objects

The Spring Framework provides an interface that should be implemented by the developer.

This project is a specific tool providing a 'use anywhere' ResultSet to Object mapper. It solves the inheritance and aggregate problems.

Download it here.

Based on some blog entries from Warren Mayocchi.

Features

Feature
Use Java Reflection to map a ResultSet to the content of a target Object.Yes
Allows configuration of many target object classes - providing support for inheritance.Yes
Allows configuration of aggregate targets - providing support for population of aggregate objects within the target object.Yes
Provides BeanProcessor support for Apache DBUtils.Yes
Provides Spring JDBC support.Yes

An Example

(See the Examples for more details.)

Map the ResultSet to objects of the JellyCompany class.

ResultSet results = stmt.executeQuery("SELECT * FROM jelly_companies");

ResultSetMapper<JellyBean> resultSetMapper = new ReflectionResultSetMapper<JellyBean>(JellyBean.class);

while (results.next()) {
  JellyCompany jellyCompany = resultSetMapper.mapRow(resultSet);
  // Order some jelly beans.
}