KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > ColumnMapRowMapper


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.core;
18
19 import java.sql.ResultSet JavaDoc;
20 import java.sql.ResultSetMetaData JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.springframework.core.CollectionFactory;
25 import org.springframework.jdbc.support.JdbcUtils;
26
27 /**
28  * {@link RowMapper} implementation that creates a <code>java.util.Map</code>
29  * for each row, representing all columns as key-value pairs: one
30  * entry for each column, with the column name as key.
31  *
32  * <p>The Map implementation to use and the key to use for each column
33  * in the column Map can be customized through overriding
34  * {@link #createColumnMap} and {@link #getColumnKey}, respectively.
35  *
36  * <p><b>Note:</b> By default, ColumnMapRowMapper will try to build a linked Map
37  * with case-insensitive keys, to preserve column order as well as allow any
38  * casing to be used for column names. This requires Commons Collections on the
39  * classpath (which will be autodetected). Else, the fallback is a standard linked
40  * HashMap, which will still preserve column order but requires the application
41  * to specify the column names in the same casing as exposed by the driver.
42  *
43  * @author Juergen Hoeller
44  * @since 1.2
45  * @see JdbcTemplate#queryForList(String)
46  * @see JdbcTemplate#queryForMap(String)
47  */

48 public class ColumnMapRowMapper implements RowMapper {
49
50     public Object JavaDoc mapRow(ResultSet JavaDoc rs, int rowNum) throws SQLException JavaDoc {
51         ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
52         int columnCount = rsmd.getColumnCount();
53         Map JavaDoc mapOfColValues = createColumnMap(columnCount);
54         for (int i = 1; i <= columnCount; i++) {
55             String JavaDoc key = getColumnKey(rsmd.getColumnName(i));
56             Object JavaDoc obj = getColumnValue(rs, i);
57             mapOfColValues.put(key, obj);
58         }
59         return mapOfColValues;
60     }
61
62     /**
63      * Create a Map instance to be used as column map.
64      * <p>By default, a linked case-insensitive Map will be created if possible,
65      * else a plain HashMap (see Spring's CollectionFactory).
66      * @param columnCount the column count, to be used as initial
67      * capacity for the Map
68      * @return the new Map instance
69      * @see org.springframework.core.CollectionFactory#createLinkedCaseInsensitiveMapIfPossible
70      */

71     protected Map JavaDoc createColumnMap(int columnCount) {
72         return CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(columnCount);
73     }
74
75     /**
76      * Determine the key to use for the given column in the column Map.
77      * @param columnName the column name as returned by the ResultSet
78      * @return the column key to use
79      * @see java.sql.ResultSetMetaData#getColumnName
80      */

81     protected String JavaDoc getColumnKey(String JavaDoc columnName) {
82         return columnName;
83     }
84
85     /**
86      * Retrieve a JDBC object value for the specified column.
87      * <p>The default implementation uses the <code>getObject</code> method.
88      * Additionally, this implementation includes a "hack" to get around Oracle
89      * returning a non standard object for their TIMESTAMP datatype.
90      * @param rs is the ResultSet holding the data
91      * @param index is the column index
92      * @return the Object returned
93      * @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue
94      */

95     protected Object JavaDoc getColumnValue(ResultSet JavaDoc rs, int index) throws SQLException JavaDoc {
96         return JdbcUtils.getResultSetValue(rs, index);
97     }
98
99 }
100
Popular Tags