org.apache.commons.scaffold.sql
Class ResultSetUtils

java.lang.Object
  extended by org.apache.commons.scaffold.sql.ResultSetUtils

public class ResultSetUtils
extends Object

General purpose utility methods related to ResultSets

Version:
$Revision: 561366 $ $Date: 2007-07-31 16:58:29 +0100 (Tue, 31 Jul 2007) $
Author:
Ted Husted

Constructor Summary
ResultSetUtils()
           
 
Method Summary
static Collection getCollection(Object target, ResultSet resultSet)
          Return a ArrayList of beans populated from a ResultSet.
static boolean getElement(Object target, ResultSet resultSet)
          Populate target bean with the first record from a ResultSet.
static Map getMap(ResultSet resultSet)
          Returns next record of result set as a Map.
static Collection getMaps(ResultSet resultSet)
          Return a Collection of Maps, each representing a row from the ResultSet.
static void putEntry(Map properties, ResultSetMetaData metaData, ResultSet resultSet, int i)
          Map JDBC objects to Java equivalents.
static void setProperties(Object bean, Map properties)
          Deprecated. Use BeanUtils.CopyProperties instead.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResultSetUtils

public ResultSetUtils()
Method Detail

getMap

public static Map getMap(ResultSet resultSet)
                  throws SQLException
Returns next record of result set as a Map. The keys of the map are the column names, as returned by the metadata. The values are the columns as Objects.

Parameters:
resultSet - The ResultSet to process.
Throws:
SQLException - if an error occurs.

getMaps

public static Collection getMaps(ResultSet resultSet)
                          throws SQLException
Return a Collection of Maps, each representing a row from the ResultSet. The keys of the map are the column names, as returned by the metadata. The values are the columns as Objects.

Parameters:
resultSet - The ResultSet to process.
Throws:
SQLException - if an error occurs.

setProperties

public static void setProperties(Object bean,
                                 Map properties)
                          throws IllegalAccessException,
                                 InvocationTargetException
Deprecated. Use BeanUtils.CopyProperties instead.

Populate the JavaBean properties of the specified bean, based on the specified name/value pairs. This method uses Java reflection APIs to identify corresponding "property setter" method names. The type of the value in the Map must match the setter type. The setter must expect a single arguement (the one on the Map).

The particular setter method to be called for each property is determined using the usual JavaBeans introspection mechanisms. Thus, you may identify custom setter methods using a BeanInfo class that is associated with the class of the bean itself. If no such BeanInfo class is available, the standard method name conversion ("set" plus the capitalized name of the property in question) is used.

NOTE: It is contrary to the JavaBeans Specification to have more than one setter method (with different argument signatures) for the same property.

This method adopted from the Apache Commons BeanUtils.populate.

Parameters:
bean - JavaBean whose properties are being populated
properties - Map keyed by property name, with the corresponding value to be set
Throws:
IllegalAccessException - if the caller does not have access to the property accessor method
InvocationTargetException - if the property accessor method throws an exception

putEntry

public static void putEntry(Map properties,
                            ResultSetMetaData metaData,
                            ResultSet resultSet,
                            int i)
                     throws SQLException
Map JDBC objects to Java equivalents. Used by getBean() and getBeans().

Some types not supported. Many not work with all drivers.

Makes binary conversions of BIGINT, DATE, DECIMAL, DOUBLE, FLOAT, INTEGER, REAL, SMALLINT, TIME, TIMESTAMP, TINYINT. Makes Sting conversions of CHAR, CLOB, VARCHAR, LONGVARCHAR, BLOB, LONGVARBINARY, VARBINARY.

DECIMAL, INTEGER, SMALLINT, TIMESTAMP, CHAR, VARCHAR tested with MySQL and Poolman. Others not guaranteed.

Throws:
SQLException

getElement

public static boolean getElement(Object target,
                                 ResultSet resultSet)
                          throws SQLException
Populate target bean with the first record from a ResultSet.

Parameters:
resultSet - The ResultSet whose parameters are to be used to populate bean properties
target - An instance of the bean to populate
Returns:
True if resultSet contained a next element
Throws:
SQLException - if an exception is thrown while setting property values, populating the bean, or accessing the ResultSet

getCollection

public static Collection getCollection(Object target,
                                       ResultSet resultSet)
                                throws SQLException
Return a ArrayList of beans populated from a ResultSet.

Parameters:
resultSet - The ResultSet whose parameters are to be used to populate bean properties
target - An instance of the bean to populate
Throws:
SQLException - if an exception is thrown while setting property values, populating the bean, or accessing the ResultSet


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.