001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jdbc.support.rowset;
018:
019: import java.math.BigDecimal;
020: import java.sql.Date;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023: import java.sql.Time;
024: import java.sql.Timestamp;
025: import java.util.Calendar;
026: import java.util.Map;
027:
028: import org.springframework.jdbc.InvalidResultSetAccessException;
029:
030: /**
031: * Default implementation of Spring's SqlRowSet interface.
032: *
033: * <p>This implementation wraps a <code>javax.sql.ResultSet</code>,
034: * catching any SQLExceptions and translating them to the
035: * appropriate Spring DataAccessException.
036: *
037: * <p>The passed-in ResultSets should already be disconnected if the
038: * SqlRowSet is supposed to be usable in a disconnected fashion.
039: * This means that you will usually pass in a
040: * <code>javax.sql.rowset.CachedRowSet</code>,
041: * which implements the ResultSet interface.
042: *
043: * <p>Note: This class implements the <code>java.io.Serializable</code>
044: * marker interface through the SqlRowSet interface, but is only actually
045: * serializable if the disconnected ResultSet/RowSet contained in it is
046: * serializable. Most CachedRowSet implementations are serializable.
047: *
048: * @author Thomas Risberg
049: * @author Juergen Hoeller
050: * @since 1.2
051: * @see java.sql.ResultSet
052: * @see javax.sql.rowset.CachedRowSet
053: */
054: public class ResultSetWrappingSqlRowSet implements SqlRowSet {
055:
056: private final ResultSet resultSet;
057:
058: private final SqlRowSetMetaData rowSetMetaData;
059:
060: /**
061: * Create a new ResultSetWrappingSqlRowSet for the given ResultSet.
062: * @param resultSet a disconnected ResultSet to wrap
063: * (usually a <code>javax.sql.rowset.CachedRowSet</code>)
064: * @throws InvalidResultSetAccessException if extracting
065: * the ResultSetMetaData failed
066: * @see javax.sql.rowset.CachedRowSet
067: * @see java.sql.ResultSet#getMetaData
068: * @see ResultSetWrappingSqlRowSetMetaData
069: */
070: public ResultSetWrappingSqlRowSet(ResultSet resultSet)
071: throws InvalidResultSetAccessException {
072: this .resultSet = resultSet;
073: try {
074: this .rowSetMetaData = new ResultSetWrappingSqlRowSetMetaData(
075: resultSet.getMetaData());
076: } catch (SQLException se) {
077: throw new InvalidResultSetAccessException(se);
078: }
079: }
080:
081: /**
082: * Return the underlying ResultSet
083: * (usually a <code>javax.sql.rowset.CachedRowSet</code>).
084: * @see javax.sql.rowset.CachedRowSet
085: */
086: public ResultSet getResultSet() {
087: return this .resultSet;
088: }
089:
090: /**
091: * @see java.sql.ResultSetMetaData#getCatalogName(int)
092: */
093: public SqlRowSetMetaData getMetaData() {
094: return this .rowSetMetaData;
095: }
096:
097: /**
098: * @see java.sql.ResultSet#findColumn(String)
099: */
100: public int findColumn(String columnName)
101: throws InvalidResultSetAccessException {
102: try {
103: return this .resultSet.findColumn(columnName);
104: } catch (SQLException se) {
105: throw new InvalidResultSetAccessException(se);
106: }
107: }
108:
109: // RowSet methods for extracting data values
110:
111: /**
112: * @see java.sql.ResultSet#getBigDecimal(int)
113: */
114: public BigDecimal getBigDecimal(int columnIndex)
115: throws InvalidResultSetAccessException {
116: try {
117: return this .resultSet.getBigDecimal(columnIndex);
118: } catch (SQLException se) {
119: throw new InvalidResultSetAccessException(se);
120: }
121: }
122:
123: /**
124: * @see java.sql.ResultSet#getBigDecimal(String)
125: */
126: public BigDecimal getBigDecimal(String columnName)
127: throws InvalidResultSetAccessException {
128: try {
129: return this .resultSet.getBigDecimal(columnName);
130: } catch (SQLException se) {
131: throw new InvalidResultSetAccessException(se);
132: }
133: }
134:
135: /**
136: * @see java.sql.ResultSet#getBoolean(int)
137: */
138: public boolean getBoolean(int columnIndex)
139: throws InvalidResultSetAccessException {
140: try {
141: return this .resultSet.getBoolean(columnIndex);
142: } catch (SQLException se) {
143: throw new InvalidResultSetAccessException(se);
144: }
145: }
146:
147: /**
148: * @see java.sql.ResultSet#getBoolean(String)
149: */
150: public boolean getBoolean(String columnName)
151: throws InvalidResultSetAccessException {
152: try {
153: return this .resultSet.getBoolean(columnName);
154: } catch (SQLException se) {
155: throw new InvalidResultSetAccessException(se);
156: }
157: }
158:
159: /**
160: * @see java.sql.ResultSet#getByte(int)
161: */
162: public byte getByte(int columnIndex)
163: throws InvalidResultSetAccessException {
164: try {
165: return this .resultSet.getByte(columnIndex);
166: } catch (SQLException se) {
167: throw new InvalidResultSetAccessException(se);
168: }
169: }
170:
171: /**
172: * @see java.sql.ResultSet#getByte(String)
173: */
174: public byte getByte(String columnName)
175: throws InvalidResultSetAccessException {
176: try {
177: return this .resultSet.getByte(columnName);
178: } catch (SQLException se) {
179: throw new InvalidResultSetAccessException(se);
180: }
181: }
182:
183: /**
184: * @see java.sql.ResultSet#getDate(int, java.util.Calendar)
185: */
186: public Date getDate(int columnIndex, Calendar cal)
187: throws InvalidResultSetAccessException {
188: try {
189: return this .resultSet.getDate(columnIndex, cal);
190: } catch (SQLException se) {
191: throw new InvalidResultSetAccessException(se);
192: }
193: }
194:
195: /**
196: * @see java.sql.ResultSet#getDate(int)
197: */
198: public Date getDate(int columnIndex)
199: throws InvalidResultSetAccessException {
200: try {
201: return this .resultSet.getDate(columnIndex);
202: } catch (SQLException se) {
203: throw new InvalidResultSetAccessException(se);
204: }
205: }
206:
207: /**
208: * @see java.sql.ResultSet#getDate(String, java.util.Calendar)
209: */
210: public Date getDate(String columnName, Calendar cal)
211: throws InvalidResultSetAccessException {
212: try {
213: return this .resultSet.getDate(columnName, cal);
214: } catch (SQLException se) {
215: throw new InvalidResultSetAccessException(se);
216: }
217: }
218:
219: /**
220: * @see java.sql.ResultSet#getDate(String)
221: */
222: public Date getDate(String columnName)
223: throws InvalidResultSetAccessException {
224: try {
225: return this .resultSet.getDate(columnName);
226: } catch (SQLException se) {
227: throw new InvalidResultSetAccessException(se);
228: }
229: }
230:
231: /**
232: * @see java.sql.ResultSet#getDouble(int)
233: */
234: public double getDouble(int columnIndex)
235: throws InvalidResultSetAccessException {
236: try {
237: return this .resultSet.getDouble(columnIndex);
238: } catch (SQLException se) {
239: throw new InvalidResultSetAccessException(se);
240: }
241: }
242:
243: /**
244: * @see java.sql.ResultSet#getDouble(String)
245: */
246: public double getDouble(String columnName)
247: throws InvalidResultSetAccessException {
248: try {
249: return this .resultSet.getDouble(columnName);
250: } catch (SQLException se) {
251: throw new InvalidResultSetAccessException(se);
252: }
253: }
254:
255: /**
256: * @see java.sql.ResultSet#getFloat(int)
257: */
258: public float getFloat(int columnIndex)
259: throws InvalidResultSetAccessException {
260: try {
261: return this .resultSet.getFloat(columnIndex);
262: } catch (SQLException se) {
263: throw new InvalidResultSetAccessException(se);
264: }
265: }
266:
267: /**
268: * @see java.sql.ResultSet#getFloat(String)
269: */
270: public float getFloat(String columnName)
271: throws InvalidResultSetAccessException {
272: try {
273: return this .resultSet.getFloat(columnName);
274: } catch (SQLException se) {
275: throw new InvalidResultSetAccessException(se);
276: }
277: }
278:
279: /**
280: * @see java.sql.ResultSet#getInt(int)
281: */
282: public int getInt(int columnIndex)
283: throws InvalidResultSetAccessException {
284: try {
285: return this .resultSet.getInt(columnIndex);
286: } catch (SQLException se) {
287: throw new InvalidResultSetAccessException(se);
288: }
289: }
290:
291: /**
292: * @see java.sql.ResultSet#getInt(String)
293: */
294: public int getInt(String columnName)
295: throws InvalidResultSetAccessException {
296: try {
297: return this .resultSet.getInt(columnName);
298: } catch (SQLException se) {
299: throw new InvalidResultSetAccessException(se);
300: }
301: }
302:
303: /**
304: * @see java.sql.ResultSet#getLong(int)
305: */
306: public long getLong(int columnIndex)
307: throws InvalidResultSetAccessException {
308: try {
309: return this .resultSet.getLong(columnIndex);
310: } catch (SQLException se) {
311: throw new InvalidResultSetAccessException(se);
312: }
313: }
314:
315: /**
316: * @see java.sql.ResultSet#getLong(String)
317: */
318: public long getLong(String columnName)
319: throws InvalidResultSetAccessException {
320: try {
321: return this .resultSet.getLong(columnName);
322: } catch (SQLException se) {
323: throw new InvalidResultSetAccessException(se);
324: }
325: }
326:
327: /**
328: * @see java.sql.ResultSet#getObject(int, java.util.Map)
329: */
330: public Object getObject(int i, Map map)
331: throws InvalidResultSetAccessException {
332: try {
333: return this .resultSet.getObject(i, map);
334: } catch (SQLException se) {
335: throw new InvalidResultSetAccessException(se);
336: }
337: }
338:
339: /**
340: * @see java.sql.ResultSet#getObject(int)
341: */
342: public Object getObject(int columnIndex)
343: throws InvalidResultSetAccessException {
344: try {
345: return this .resultSet.getObject(columnIndex);
346: } catch (SQLException se) {
347: throw new InvalidResultSetAccessException(se);
348: }
349: }
350:
351: /**
352: * @see java.sql.ResultSet#getObject(String, java.util.Map)
353: */
354: public Object getObject(String columnName, Map map)
355: throws InvalidResultSetAccessException {
356: try {
357: return this .resultSet.getObject(columnName, map);
358: } catch (SQLException se) {
359: throw new InvalidResultSetAccessException(se);
360: }
361: }
362:
363: /**
364: * @see java.sql.ResultSet#getObject(String)
365: */
366: public Object getObject(String columnName)
367: throws InvalidResultSetAccessException {
368: try {
369: return this .resultSet.getObject(columnName);
370: } catch (SQLException se) {
371: throw new InvalidResultSetAccessException(se);
372: }
373: }
374:
375: /**
376: * @see java.sql.ResultSet#getShort(int)
377: */
378: public short getShort(int columnIndex)
379: throws InvalidResultSetAccessException {
380: try {
381: return this .resultSet.getShort(columnIndex);
382: } catch (SQLException se) {
383: throw new InvalidResultSetAccessException(se);
384: }
385: }
386:
387: /**
388: * @see java.sql.ResultSet#getShort(String)
389: */
390: public short getShort(String columnName)
391: throws InvalidResultSetAccessException {
392: try {
393: return this .resultSet.getShort(columnName);
394: } catch (SQLException se) {
395: throw new InvalidResultSetAccessException(se);
396: }
397: }
398:
399: /**
400: * @see java.sql.ResultSet#getString(int)
401: */
402: public String getString(int columnIndex)
403: throws InvalidResultSetAccessException {
404: try {
405: return this .resultSet.getString(columnIndex);
406: } catch (SQLException se) {
407: throw new InvalidResultSetAccessException(se);
408: }
409: }
410:
411: /**
412: * @see java.sql.ResultSet#getString(String)
413: */
414: public String getString(String columnName)
415: throws InvalidResultSetAccessException {
416: try {
417: return this .resultSet.getString(columnName);
418: } catch (SQLException se) {
419: throw new InvalidResultSetAccessException(se);
420: }
421: }
422:
423: /**
424: * @see java.sql.ResultSet#getTime(int, java.util.Calendar)
425: */
426: public Time getTime(int columnIndex, Calendar cal)
427: throws InvalidResultSetAccessException {
428: try {
429: return this .resultSet.getTime(columnIndex, cal);
430: } catch (SQLException se) {
431: throw new InvalidResultSetAccessException(se);
432: }
433: }
434:
435: /**
436: * @see java.sql.ResultSet#getTime(int)
437: */
438: public Time getTime(int columnIndex)
439: throws InvalidResultSetAccessException {
440: try {
441: return this .resultSet.getTime(columnIndex);
442: } catch (SQLException se) {
443: throw new InvalidResultSetAccessException(se);
444: }
445: }
446:
447: /**
448: * @see java.sql.ResultSet#getTime(String, java.util.Calendar)
449: */
450: public Time getTime(String columnName, Calendar cal)
451: throws InvalidResultSetAccessException {
452: try {
453: return this .resultSet.getTime(columnName, cal);
454: } catch (SQLException se) {
455: throw new InvalidResultSetAccessException(se);
456: }
457: }
458:
459: /**
460: * @see java.sql.ResultSet#getTime(String)
461: */
462: public Time getTime(String columnName)
463: throws InvalidResultSetAccessException {
464: try {
465: return this .resultSet.getTime(columnName);
466: } catch (SQLException se) {
467: throw new InvalidResultSetAccessException(se);
468: }
469: }
470:
471: /**
472: * @see java.sql.ResultSet#getTimestamp(int, java.util.Calendar)
473: */
474: public Timestamp getTimestamp(int columnIndex, Calendar cal)
475: throws InvalidResultSetAccessException {
476: try {
477: return this .resultSet.getTimestamp(columnIndex, cal);
478: } catch (SQLException se) {
479: throw new InvalidResultSetAccessException(se);
480: }
481: }
482:
483: /**
484: * @see java.sql.ResultSet#getTimestamp(int)
485: */
486: public Timestamp getTimestamp(int columnIndex)
487: throws InvalidResultSetAccessException {
488: try {
489: return this .resultSet.getTimestamp(columnIndex);
490: } catch (SQLException se) {
491: throw new InvalidResultSetAccessException(se);
492: }
493: }
494:
495: /**
496: * @see java.sql.ResultSet#getTimestamp(String, java.util.Calendar)
497: */
498: public Timestamp getTimestamp(String columnName, Calendar cal)
499: throws InvalidResultSetAccessException {
500: try {
501: return this .resultSet.getTimestamp(columnName, cal);
502: } catch (SQLException se) {
503: throw new InvalidResultSetAccessException(se);
504: }
505: }
506:
507: /**
508: * @see java.sql.ResultSet#getTimestamp(String)
509: */
510: public Timestamp getTimestamp(String columnName)
511: throws InvalidResultSetAccessException {
512: try {
513: return this .resultSet.getTimestamp(columnName);
514: } catch (SQLException se) {
515: throw new InvalidResultSetAccessException(se);
516: }
517: }
518:
519: // RowSet navigation methods
520:
521: /**
522: * @see java.sql.ResultSet#absolute(int)
523: */
524: public boolean absolute(int row)
525: throws InvalidResultSetAccessException {
526: try {
527: return this .resultSet.absolute(row);
528: } catch (SQLException se) {
529: throw new InvalidResultSetAccessException(se);
530: }
531: }
532:
533: /**
534: * @see java.sql.ResultSet#afterLast()
535: */
536: public void afterLast() throws InvalidResultSetAccessException {
537: try {
538: this .resultSet.afterLast();
539: } catch (SQLException se) {
540: throw new InvalidResultSetAccessException(se);
541: }
542: }
543:
544: /**
545: * @see java.sql.ResultSet#beforeFirst()
546: */
547: public void beforeFirst() throws InvalidResultSetAccessException {
548: try {
549: this .resultSet.beforeFirst();
550: } catch (SQLException se) {
551: throw new InvalidResultSetAccessException(se);
552: }
553: }
554:
555: /**
556: * @see java.sql.ResultSet#first()
557: */
558: public boolean first() throws InvalidResultSetAccessException {
559: try {
560: return this .resultSet.first();
561: } catch (SQLException se) {
562: throw new InvalidResultSetAccessException(se);
563: }
564: }
565:
566: /**
567: * @see java.sql.ResultSet#getRow()
568: */
569: public int getRow() throws InvalidResultSetAccessException {
570: try {
571: return this .resultSet.getRow();
572: } catch (SQLException se) {
573: throw new InvalidResultSetAccessException(se);
574: }
575: }
576:
577: /**
578: * @see java.sql.ResultSet#isAfterLast()
579: */
580: public boolean isAfterLast() throws InvalidResultSetAccessException {
581: try {
582: return this .resultSet.isAfterLast();
583: } catch (SQLException se) {
584: throw new InvalidResultSetAccessException(se);
585: }
586: }
587:
588: /**
589: * @see java.sql.ResultSet#isBeforeFirst()
590: */
591: public boolean isBeforeFirst()
592: throws InvalidResultSetAccessException {
593: try {
594: return this .resultSet.isBeforeFirst();
595: } catch (SQLException se) {
596: throw new InvalidResultSetAccessException(se);
597: }
598: }
599:
600: /**
601: * @see java.sql.ResultSet#isFirst()
602: */
603: public boolean isFirst() throws InvalidResultSetAccessException {
604: try {
605: return this .resultSet.isFirst();
606: } catch (SQLException se) {
607: throw new InvalidResultSetAccessException(se);
608: }
609: }
610:
611: /**
612: * @see java.sql.ResultSet#isLast()
613: */
614: public boolean isLast() throws InvalidResultSetAccessException {
615: try {
616: return this .resultSet.isLast();
617: } catch (SQLException se) {
618: throw new InvalidResultSetAccessException(se);
619: }
620: }
621:
622: /**
623: * @see java.sql.ResultSet#last()
624: */
625: public boolean last() throws InvalidResultSetAccessException {
626: try {
627: return this .resultSet.last();
628: } catch (SQLException se) {
629: throw new InvalidResultSetAccessException(se);
630: }
631: }
632:
633: /**
634: * @see java.sql.ResultSet#next()
635: */
636: public boolean next() throws InvalidResultSetAccessException {
637: try {
638: return this .resultSet.next();
639: } catch (SQLException se) {
640: throw new InvalidResultSetAccessException(se);
641: }
642: }
643:
644: /**
645: * @see java.sql.ResultSet#previous()
646: */
647: public boolean previous() throws InvalidResultSetAccessException {
648: try {
649: return this .resultSet.previous();
650: } catch (SQLException se) {
651: throw new InvalidResultSetAccessException(se);
652: }
653: }
654:
655: /**
656: * @see java.sql.ResultSet#relative(int)
657: */
658: public boolean relative(int rows)
659: throws InvalidResultSetAccessException {
660: try {
661: return this .resultSet.relative(rows);
662: } catch (SQLException se) {
663: throw new InvalidResultSetAccessException(se);
664: }
665: }
666:
667: /**
668: * @see java.sql.ResultSet#wasNull()
669: */
670: public boolean wasNull() throws InvalidResultSetAccessException {
671: try {
672: return this .resultSet.wasNull();
673: } catch (SQLException se) {
674: throw new InvalidResultSetAccessException(se);
675: }
676: }
677:
678: }
|