11/*
2- * Copyright 2002-2008 the original author or authors.
2+ * Copyright 2002-2010 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1919import java .math .BigDecimal ;
2020import java .sql .Date ;
2121import java .sql .ResultSet ;
22+ import java .sql .ResultSetMetaData ;
2223import java .sql .SQLException ;
2324import java .sql .Time ;
2425import java .sql .Timestamp ;
2526import java .util .Calendar ;
27+ import java .util .HashMap ;
2628import java .util .Map ;
2729
2830import org .springframework .jdbc .InvalidResultSetAccessException ;
3335 * <p>This implementation wraps a <code>javax.sql.ResultSet</code>,
3436 * catching any SQLExceptions and translating them to the
3537 * appropriate Spring {@link InvalidResultSetAccessException}.
38+ *
39+ * <p>Note: Since JDBC 4.0 it has been clarified that any methods using a String to identify the column should
40+ * be using the column label. The column label is assigned using the ALIAS keyword in the SQL query string. When the
41+ * query doesn't use an ALIAS the default label is the column name. Most JDBC ResultSet implementations follow this
42+ * new pattern but there are some exceptions such as the com.sun.rowset.CachedRowSetImpl which only uses the column
43+ * name ignoring any column labels. Since Spring 3.0.5 this class will translate column labels to the correct column
44+ * index to provide better support for the com.sun.rowset.CachedRowSetImpl which is the default implementation used by
45+ * the JdbcTemplate when working with RowSets.
3646 *
3747 * <p>The passed-in ResultSets should already be disconnected if the SqlRowSet
3848 * is supposed to be usable in a disconnected fashion. This means that
@@ -59,6 +69,8 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
5969 private final ResultSet resultSet ;
6070
6171 private final SqlRowSetMetaData rowSetMetaData ;
72+
73+ private final Map <String , Integer > columnLabelMap ;
6274
6375
6476 /**
@@ -79,6 +91,22 @@ public ResultSetWrappingSqlRowSet(ResultSet resultSet) throws InvalidResultSetAc
7991 catch (SQLException se ) {
8092 throw new InvalidResultSetAccessException (se );
8193 }
94+ try {
95+ ResultSetMetaData rsmd = resultSet .getMetaData ();
96+ if (rsmd != null ) {
97+ int columnCount = rsmd .getColumnCount ();
98+ this .columnLabelMap = new HashMap <String , Integer >(columnCount );
99+ for (int i = 1 ; i <= columnCount ; i ++) {
100+ columnLabelMap .put (rsmd .getColumnLabel (i ), Integer .valueOf (i ));
101+ }
102+ }
103+ else {
104+ this .columnLabelMap = new HashMap <String , Integer >(0 );
105+ }
106+ } catch (SQLException se ) {
107+ throw new InvalidResultSetAccessException (se );
108+ }
109+
82110 }
83111
84112
@@ -101,13 +129,17 @@ public final SqlRowSetMetaData getMetaData() {
101129 /**
102130 * @see java.sql.ResultSet#findColumn(String)
103131 */
104- public int findColumn (String columnName ) throws InvalidResultSetAccessException {
132+ public int findColumn (String columnLabel ) throws InvalidResultSetAccessException {
133+ Integer columnIndex = columnLabelMap .get (columnLabel );
105134 try {
106- return this .resultSet .findColumn (columnName );
135+ if (columnIndex == null ) {
136+ columnIndex = this .resultSet .findColumn (columnLabel );
137+ }
107138 }
108139 catch (SQLException se ) {
109140 throw new InvalidResultSetAccessException (se );
110141 }
142+ return columnIndex .intValue ();
111143 }
112144
113145
@@ -128,13 +160,8 @@ public BigDecimal getBigDecimal(int columnIndex) throws InvalidResultSetAccessEx
128160 /**
129161 * @see java.sql.ResultSet#getBigDecimal(String)
130162 */
131- public BigDecimal getBigDecimal (String columnName ) throws InvalidResultSetAccessException {
132- try {
133- return this .resultSet .getBigDecimal (columnName );
134- }
135- catch (SQLException se ) {
136- throw new InvalidResultSetAccessException (se );
137- }
163+ public BigDecimal getBigDecimal (String columnLabel ) throws InvalidResultSetAccessException {
164+ return getBigDecimal (findColumn (columnLabel ));
138165 }
139166
140167 /**
@@ -152,13 +179,8 @@ public boolean getBoolean(int columnIndex) throws InvalidResultSetAccessExceptio
152179 /**
153180 * @see java.sql.ResultSet#getBoolean(String)
154181 */
155- public boolean getBoolean (String columnName ) throws InvalidResultSetAccessException {
156- try {
157- return this .resultSet .getBoolean (columnName );
158- }
159- catch (SQLException se ) {
160- throw new InvalidResultSetAccessException (se );
161- }
182+ public boolean getBoolean (String columnLabel ) throws InvalidResultSetAccessException {
183+ return getBoolean (findColumn (columnLabel ));
162184 }
163185
164186 /**
@@ -176,13 +198,8 @@ public byte getByte(int columnIndex) throws InvalidResultSetAccessException {
176198 /**
177199 * @see java.sql.ResultSet#getByte(String)
178200 */
179- public byte getByte (String columnName ) throws InvalidResultSetAccessException {
180- try {
181- return this .resultSet .getByte (columnName );
182- }
183- catch (SQLException se ) {
184- throw new InvalidResultSetAccessException (se );
185- }
201+ public byte getByte (String columnLabel ) throws InvalidResultSetAccessException {
202+ return getByte (findColumn (columnLabel ));
186203 }
187204
188205 /**
@@ -211,25 +228,15 @@ public Date getDate(int columnIndex) throws InvalidResultSetAccessException {
211228 /**
212229 * @see java.sql.ResultSet#getDate(String, java.util.Calendar)
213230 */
214- public Date getDate (String columnName , Calendar cal ) throws InvalidResultSetAccessException {
215- try {
216- return this .resultSet .getDate (columnName , cal );
217- }
218- catch (SQLException se ) {
219- throw new InvalidResultSetAccessException (se );
220- }
231+ public Date getDate (String columnLabel , Calendar cal ) throws InvalidResultSetAccessException {
232+ return getDate (findColumn (columnLabel ), cal );
221233 }
222234
223235 /**
224236 * @see java.sql.ResultSet#getDate(String)
225237 */
226- public Date getDate (String columnName ) throws InvalidResultSetAccessException {
227- try {
228- return this .resultSet .getDate (columnName );
229- }
230- catch (SQLException se ) {
231- throw new InvalidResultSetAccessException (se );
232- }
238+ public Date getDate (String columnLabel ) throws InvalidResultSetAccessException {
239+ return getDate (findColumn (columnLabel ));
233240 }
234241
235242 /**
@@ -247,13 +254,8 @@ public double getDouble(int columnIndex) throws InvalidResultSetAccessException
247254 /**
248255 * @see java.sql.ResultSet#getDouble(String)
249256 */
250- public double getDouble (String columnName ) throws InvalidResultSetAccessException {
251- try {
252- return this .resultSet .getDouble (columnName );
253- }
254- catch (SQLException se ) {
255- throw new InvalidResultSetAccessException (se );
256- }
257+ public double getDouble (String columnLabel ) throws InvalidResultSetAccessException {
258+ return getDouble (findColumn (columnLabel ));
257259 }
258260
259261 /**
@@ -271,13 +273,8 @@ public float getFloat(int columnIndex) throws InvalidResultSetAccessException {
271273 /**
272274 * @see java.sql.ResultSet#getFloat(String)
273275 */
274- public float getFloat (String columnName ) throws InvalidResultSetAccessException {
275- try {
276- return this .resultSet .getFloat (columnName );
277- }
278- catch (SQLException se ) {
279- throw new InvalidResultSetAccessException (se );
280- }
276+ public float getFloat (String columnLabel ) throws InvalidResultSetAccessException {
277+ return getFloat (findColumn (columnLabel ));
281278 }
282279 /**
283280 * @see java.sql.ResultSet#getInt(int)
@@ -294,13 +291,8 @@ public int getInt(int columnIndex) throws InvalidResultSetAccessException {
294291 /**
295292 * @see java.sql.ResultSet#getInt(String)
296293 */
297- public int getInt (String columnName ) throws InvalidResultSetAccessException {
298- try {
299- return this .resultSet .getInt (columnName );
300- }
301- catch (SQLException se ) {
302- throw new InvalidResultSetAccessException (se );
303- }
294+ public int getInt (String columnLabel ) throws InvalidResultSetAccessException {
295+ return getInt (findColumn (columnLabel ));
304296 }
305297
306298 /**
@@ -318,19 +310,14 @@ public long getLong(int columnIndex) throws InvalidResultSetAccessException {
318310 /**
319311 * @see java.sql.ResultSet#getLong(String)
320312 */
321- public long getLong (String columnName ) throws InvalidResultSetAccessException {
322- try {
323- return this .resultSet .getLong (columnName );
324- }
325- catch (SQLException se ) {
326- throw new InvalidResultSetAccessException (se );
327- }
313+ public long getLong (String columnLabel ) throws InvalidResultSetAccessException {
314+ return getLong (findColumn (columnLabel ));
328315 }
329316
330317 /**
331318 * @see java.sql.ResultSet#getObject(int, java.util.Map)
332319 */
333- public Object getObject (int i , Map map ) throws InvalidResultSetAccessException {
320+ public Object getObject (int i , Map < String , Class <?>> map ) throws InvalidResultSetAccessException {
334321 try {
335322 return this .resultSet .getObject (i , map );
336323 }
@@ -354,25 +341,15 @@ public Object getObject(int columnIndex) throws InvalidResultSetAccessException
354341 /**
355342 * @see java.sql.ResultSet#getObject(String, java.util.Map)
356343 */
357- public Object getObject (String columnName , Map map ) throws InvalidResultSetAccessException {
358- try {
359- return this .resultSet .getObject (columnName , map );
360- }
361- catch (SQLException se ) {
362- throw new InvalidResultSetAccessException (se );
363- }
344+ public Object getObject (String columnLabel , Map <String , Class <?>> map ) throws InvalidResultSetAccessException {
345+ return getObject (findColumn (columnLabel ), map );
364346 }
365347
366348 /**
367349 * @see java.sql.ResultSet#getObject(String)
368350 */
369- public Object getObject (String columnName ) throws InvalidResultSetAccessException {
370- try {
371- return this .resultSet .getObject (columnName );
372- }
373- catch (SQLException se ) {
374- throw new InvalidResultSetAccessException (se );
375- }
351+ public Object getObject (String columnLabel ) throws InvalidResultSetAccessException {
352+ return getObject (findColumn (columnLabel ));
376353 }
377354
378355 /**
@@ -390,13 +367,8 @@ public short getShort(int columnIndex) throws InvalidResultSetAccessException {
390367 /**
391368 * @see java.sql.ResultSet#getShort(String)
392369 */
393- public short getShort (String columnName ) throws InvalidResultSetAccessException {
394- try {
395- return this .resultSet .getShort (columnName );
396- }
397- catch (SQLException se ) {
398- throw new InvalidResultSetAccessException (se );
399- }
370+ public short getShort (String columnLabel ) throws InvalidResultSetAccessException {
371+ return getShort (findColumn (columnLabel ));
400372 }
401373
402374 /**
@@ -414,13 +386,8 @@ public String getString(int columnIndex) throws InvalidResultSetAccessException
414386 /**
415387 * @see java.sql.ResultSet#getString(String)
416388 */
417- public String getString (String columnName ) throws InvalidResultSetAccessException {
418- try {
419- return this .resultSet .getString (columnName );
420- }
421- catch (SQLException se ) {
422- throw new InvalidResultSetAccessException (se );
423- }
389+ public String getString (String columnLabel ) throws InvalidResultSetAccessException {
390+ return getString (findColumn (columnLabel ));
424391 }
425392
426393 /**
@@ -450,25 +417,15 @@ public Time getTime(int columnIndex) throws InvalidResultSetAccessException {
450417 /**
451418 * @see java.sql.ResultSet#getTime(String, java.util.Calendar)
452419 */
453- public Time getTime (String columnName , Calendar cal ) throws InvalidResultSetAccessException {
454- try {
455- return this .resultSet .getTime (columnName , cal );
456- }
457- catch (SQLException se ) {
458- throw new InvalidResultSetAccessException (se );
459- }
420+ public Time getTime (String columnLabel , Calendar cal ) throws InvalidResultSetAccessException {
421+ return getTime (findColumn (columnLabel ), cal );
460422 }
461423
462424 /**
463425 * @see java.sql.ResultSet#getTime(String)
464426 */
465- public Time getTime (String columnName ) throws InvalidResultSetAccessException {
466- try {
467- return this .resultSet .getTime (columnName );
468- }
469- catch (SQLException se ) {
470- throw new InvalidResultSetAccessException (se );
471- }
427+ public Time getTime (String columnLabel ) throws InvalidResultSetAccessException {
428+ return getTime (findColumn (columnLabel ));
472429 }
473430
474431 /**
@@ -498,25 +455,15 @@ public Timestamp getTimestamp(int columnIndex) throws InvalidResultSetAccessExce
498455 /**
499456 * @see java.sql.ResultSet#getTimestamp(String, java.util.Calendar)
500457 */
501- public Timestamp getTimestamp (String columnName , Calendar cal ) throws InvalidResultSetAccessException {
502- try {
503- return this .resultSet .getTimestamp (columnName , cal );
504- }
505- catch (SQLException se ) {
506- throw new InvalidResultSetAccessException (se );
507- }
458+ public Timestamp getTimestamp (String columnLabel , Calendar cal ) throws InvalidResultSetAccessException {
459+ return getTimestamp (findColumn (columnLabel ), cal );
508460 }
509461
510462 /**
511463 * @see java.sql.ResultSet#getTimestamp(String)
512464 */
513- public Timestamp getTimestamp (String columnName ) throws InvalidResultSetAccessException {
514- try {
515- return this .resultSet .getTimestamp (columnName );
516- }
517- catch (SQLException se ) {
518- throw new InvalidResultSetAccessException (se );
519- }
465+ public Timestamp getTimestamp (String columnLabel ) throws InvalidResultSetAccessException {
466+ return getTimestamp (findColumn (columnLabel ));
520467 }
521468
522469
0 commit comments