KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > support > rowset > ResultSetWrappingSqlRowSet


1 /*
2  * Copyright 2002-2005 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.support.rowset;
18
19 import java.math.BigDecimal JavaDoc;
20 import java.sql.Date JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Time JavaDoc;
24 import java.sql.Timestamp JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.springframework.jdbc.InvalidResultSetAccessException;
29
30 /**
31  * Default implementation of Spring's SqlRowSet interface.
32  *
33  * <p>This implementation wraps a <code>javax.sql.ResultSet</code>,
34  * catching any SQLExceptions and translating them to the
35  * appropriate Spring DataAccessException.
36  *
37  * <p>The passed-in ResultSets should already be disconnected if the
38  * SqlRowSet is supposed to be usable in a disconnected fashion.
39  * This means that you will usually pass in a
40  * <code>javax.sql.rowset.CachedRowSet</code>,
41  * which implements the ResultSet interface.
42  *
43  * <p>Note: This class implements the <code>java.io.Serializable</code>
44  * marker interface through the SqlRowSet interface, but is only actually
45  * serializable if the disconnected ResultSet/RowSet contained in it is
46  * serializable. Most CachedRowSet implementations are serializable.
47  *
48  * @author Thomas Risberg
49  * @author Juergen Hoeller
50  * @since 1.2
51  * @see java.sql.ResultSet
52  * @see javax.sql.rowset.CachedRowSet
53  */

54 public class ResultSetWrappingSqlRowSet implements SqlRowSet {
55
56     private final ResultSet JavaDoc resultSet;
57
58     private final SqlRowSetMetaData rowSetMetaData;
59
60
61     /**
62      * Create a new ResultSetWrappingSqlRowSet for the given ResultSet.
63      * @param resultSet a disconnected ResultSet to wrap
64      * (usually a <code>javax.sql.rowset.CachedRowSet</code>)
65      * @throws InvalidResultSetAccessException if extracting
66      * the ResultSetMetaData failed
67      * @see javax.sql.rowset.CachedRowSet
68      * @see java.sql.ResultSet#getMetaData
69      * @see ResultSetWrappingSqlRowSetMetaData
70      */

71     public ResultSetWrappingSqlRowSet(ResultSet JavaDoc resultSet) throws InvalidResultSetAccessException {
72         this.resultSet = resultSet;
73         try {
74             this.rowSetMetaData = new ResultSetWrappingSqlRowSetMetaData(resultSet.getMetaData());
75         }
76         catch (SQLException JavaDoc se) {
77             throw new InvalidResultSetAccessException(se);
78         }
79     }
80     
81     /**
82      * Return the underlying ResultSet
83      * (usually a <code>javax.sql.rowset.CachedRowSet</code>).
84      * @see javax.sql.rowset.CachedRowSet
85      */

86     public ResultSet JavaDoc getResultSet() {
87         return this.resultSet;
88     }
89
90     /**
91      * @see java.sql.ResultSetMetaData#getCatalogName(int)
92      */

93     public SqlRowSetMetaData getMetaData() {
94         return this.rowSetMetaData;
95     }
96     
97     /**
98      * @see java.sql.ResultSet#findColumn(String)
99      */

100     public int findColumn(String JavaDoc columnName) throws InvalidResultSetAccessException {
101         try {
102             return this.resultSet.findColumn(columnName);
103         }
104         catch (SQLException JavaDoc se) {
105             throw new InvalidResultSetAccessException(se);
106         }
107     }
108
109
110     // RowSet methods for extracting data values
111

112     /**
113      * @see java.sql.ResultSet#getBigDecimal(int)
114      */

115     public BigDecimal JavaDoc getBigDecimal(int columnIndex) throws InvalidResultSetAccessException {
116         try {
117             return this.resultSet.getBigDecimal(columnIndex);
118         }
119         catch (SQLException JavaDoc se) {
120             throw new InvalidResultSetAccessException(se);
121         }
122     }
123     
124     /**
125      * @see java.sql.ResultSet#getBigDecimal(String)
126      */

127     public BigDecimal JavaDoc getBigDecimal(String JavaDoc columnName) throws InvalidResultSetAccessException {
128         try {
129             return this.resultSet.getBigDecimal(columnName);
130         }
131         catch (SQLException JavaDoc se) {
132             throw new InvalidResultSetAccessException(se);
133         }
134     }
135
136     /**
137      * @see java.sql.ResultSet#getBoolean(int)
138      */

139     public boolean getBoolean(int columnIndex) throws InvalidResultSetAccessException {
140         try {
141             return this.resultSet.getBoolean(columnIndex);
142         }
143         catch (SQLException JavaDoc se) {
144             throw new InvalidResultSetAccessException(se);
145         }
146     }
147
148     /**
149      * @see java.sql.ResultSet#getBoolean(String)
150      */

151     public boolean getBoolean(String JavaDoc columnName) throws InvalidResultSetAccessException {
152         try {
153             return this.resultSet.getBoolean(columnName);
154         }
155         catch (SQLException JavaDoc se) {
156             throw new InvalidResultSetAccessException(se);
157         }
158     }
159     
160     /**
161      * @see java.sql.ResultSet#getByte(int)
162      */

163     public byte getByte(int columnIndex) throws InvalidResultSetAccessException {
164         try {
165             return this.resultSet.getByte(columnIndex);
166         }
167         catch (SQLException JavaDoc se) {
168             throw new InvalidResultSetAccessException(se);
169         }
170     }
171     
172     /**
173      * @see java.sql.ResultSet#getByte(String)
174      */

175     public byte getByte(String JavaDoc columnName) throws InvalidResultSetAccessException {
176         try {
177             return this.resultSet.getByte(columnName);
178         }
179         catch (SQLException JavaDoc se) {
180             throw new InvalidResultSetAccessException(se);
181         }
182     }
183     
184     /**
185      * @see java.sql.ResultSet#getDate(int, java.util.Calendar)
186      */

187     public Date JavaDoc getDate(int columnIndex, Calendar JavaDoc cal) throws InvalidResultSetAccessException {
188         try {
189             return this.resultSet.getDate(columnIndex, cal);
190         }
191         catch (SQLException JavaDoc se) {
192             throw new InvalidResultSetAccessException(se);
193         }
194     }
195     
196     /**
197      * @see java.sql.ResultSet#getDate(int)
198      */

199     public Date JavaDoc getDate(int columnIndex) throws InvalidResultSetAccessException {
200         try {
201             return this.resultSet.getDate(columnIndex);
202         }
203         catch (SQLException JavaDoc se) {
204             throw new InvalidResultSetAccessException(se);
205         }
206     }
207     /**
208      * @see java.sql.ResultSet#getDate(String, java.util.Calendar)
209      */

210     public Date JavaDoc getDate(String JavaDoc columnName, Calendar JavaDoc cal) throws InvalidResultSetAccessException {
211         try {
212             return this.resultSet.getDate(columnName, cal);
213         }
214         catch (SQLException JavaDoc se) {
215             throw new InvalidResultSetAccessException(se);
216         }
217     }
218     
219     /**
220      * @see java.sql.ResultSet#getDate(String)
221      */

222     public Date JavaDoc getDate(String JavaDoc columnName) throws InvalidResultSetAccessException {
223         try {
224             return this.resultSet.getDate(columnName);
225         }
226         catch (SQLException JavaDoc se) {
227             throw new InvalidResultSetAccessException(se);
228         }
229     }
230     
231     /**
232      * @see java.sql.ResultSet#getDouble(int)
233      */

234     public double getDouble(int columnIndex) throws InvalidResultSetAccessException {
235         try {
236             return this.resultSet.getDouble(columnIndex);
237         }
238         catch (SQLException JavaDoc se) {
239             throw new InvalidResultSetAccessException(se);
240         }
241     }
242     
243     /**
244      * @see java.sql.ResultSet#getDouble(String)
245      */

246     public double getDouble(String JavaDoc columnName) throws InvalidResultSetAccessException {
247         try {
248             return this.resultSet.getDouble(columnName);
249         }
250         catch (SQLException JavaDoc se) {
251             throw new InvalidResultSetAccessException(se);
252         }
253     }
254     
255     /**
256      * @see java.sql.ResultSet#getFloat(int)
257      */

258     public float getFloat(int columnIndex) throws InvalidResultSetAccessException {
259         try {
260             return this.resultSet.getFloat(columnIndex);
261         }
262         catch (SQLException JavaDoc se) {
263             throw new InvalidResultSetAccessException(se);
264         }
265     }
266
267     /**
268      * @see java.sql.ResultSet#getFloat(String)
269      */

270     public float getFloat(String JavaDoc columnName) throws InvalidResultSetAccessException {
271         try {
272             return this.resultSet.getFloat(columnName);
273         }
274         catch (SQLException JavaDoc se) {
275             throw new InvalidResultSetAccessException(se);
276         }
277     }
278     /**
279      * @see java.sql.ResultSet#getInt(int)
280      */

281     public int getInt(int columnIndex) throws InvalidResultSetAccessException {
282         try {
283             return this.resultSet.getInt(columnIndex);
284         }
285         catch (SQLException JavaDoc se) {
286             throw new InvalidResultSetAccessException(se);
287         }
288     }
289
290     /**
291      * @see java.sql.ResultSet#getInt(String)
292      */

293     public int getInt(String JavaDoc columnName) throws InvalidResultSetAccessException {
294         try {
295             return this.resultSet.getInt(columnName);
296         }
297         catch (SQLException JavaDoc se) {
298             throw new InvalidResultSetAccessException(se);
299         }
300     }
301     
302     /**
303      * @see java.sql.ResultSet#getLong(int)
304      */

305     public long getLong(int columnIndex) throws InvalidResultSetAccessException {
306         try {
307             return this.resultSet.getLong(columnIndex);
308         }
309         catch (SQLException JavaDoc se) {
310             throw new InvalidResultSetAccessException(se);
311         }
312     }
313     
314     /**
315      * @see java.sql.ResultSet#getLong(String)
316      */

317     public long getLong(String JavaDoc columnName) throws InvalidResultSetAccessException {
318         try {
319             return this.resultSet.getLong(columnName);
320         }
321         catch (SQLException JavaDoc se) {
322             throw new InvalidResultSetAccessException(se);
323         }
324     }
325     
326     /**
327      * @see java.sql.ResultSet#getObject(int, java.util.Map)
328      */

329     public Object JavaDoc getObject(int i, Map JavaDoc map) throws InvalidResultSetAccessException {
330         try {
331             return this.resultSet.getObject(i, map);
332         }
333         catch (SQLException JavaDoc se) {
334             throw new InvalidResultSetAccessException(se);
335         }
336     }
337     
338     /**
339      * @see java.sql.ResultSet#getObject(int)
340      */

341     public Object JavaDoc getObject(int columnIndex) throws InvalidResultSetAccessException {
342         try {
343             return this.resultSet.getObject(columnIndex);
344         }
345         catch (SQLException JavaDoc se) {
346             throw new InvalidResultSetAccessException(se);
347         }
348     }
349     
350     /**
351      * @see java.sql.ResultSet#getObject(String, java.util.Map)
352      */

353     public Object JavaDoc getObject(String JavaDoc columnName, Map JavaDoc map) throws InvalidResultSetAccessException {
354         try {
355             return this.resultSet.getObject(columnName, map);
356         }
357         catch (SQLException JavaDoc se) {
358             throw new InvalidResultSetAccessException(se);
359         }
360     }
361     
362     /**
363      * @see java.sql.ResultSet#getObject(String)
364      */

365     public Object JavaDoc getObject(String JavaDoc columnName) throws InvalidResultSetAccessException {
366         try {
367             return this.resultSet.getObject(columnName);
368         }
369         catch (SQLException JavaDoc se) {
370             throw new InvalidResultSetAccessException(se);
371         }
372     }
373     
374     /**
375      * @see java.sql.ResultSet#getShort(int)
376      */

377     public short getShort(int columnIndex) throws InvalidResultSetAccessException {
378         try {
379             return this.resultSet.getShort(columnIndex);
380         }
381         catch (SQLException JavaDoc se) {
382             throw new InvalidResultSetAccessException(se);
383         }
384     }
385     
386     /**
387      * @see java.sql.ResultSet#getShort(String)
388      */

389     public short getShort(String JavaDoc columnName) throws InvalidResultSetAccessException {
390         try {
391             return this.resultSet.getShort(columnName);
392         }
393         catch (SQLException JavaDoc se) {
394             throw new InvalidResultSetAccessException(se);
395         }
396     }
397     
398     /**
399      * @see java.sql.ResultSet#getString(int)
400      */

401     public String JavaDoc getString(int columnIndex) throws InvalidResultSetAccessException {
402         try {
403             return this.resultSet.getString(columnIndex);
404         }
405         catch (SQLException JavaDoc se) {
406             throw new InvalidResultSetAccessException(se);
407         }
408     }
409     
410     /**
411      * @see java.sql.ResultSet#getString(String)
412      */

413     public String JavaDoc getString(String JavaDoc columnName) throws InvalidResultSetAccessException {
414         try {
415             return this.resultSet.getString(columnName);
416         }
417         catch (SQLException JavaDoc se) {
418             throw new InvalidResultSetAccessException(se);
419         }
420     }
421     
422     /**
423      * @see java.sql.ResultSet#getTime(int, java.util.Calendar)
424      */

425     public Time JavaDoc getTime(int columnIndex, Calendar JavaDoc cal) throws InvalidResultSetAccessException {
426         try {
427             return this.resultSet.getTime(columnIndex, cal);
428         }
429         catch (SQLException JavaDoc se) {
430             throw new InvalidResultSetAccessException(se);
431         }
432     }
433     
434     /**
435      * @see java.sql.ResultSet#getTime(int)
436      */

437     public Time JavaDoc getTime(int columnIndex) throws InvalidResultSetAccessException {
438         try {
439             return this.resultSet.getTime(columnIndex);
440         }
441         catch (SQLException JavaDoc se) {
442             throw new InvalidResultSetAccessException(se);
443         }
444     }
445
446     /**
447      * @see java.sql.ResultSet#getTime(String, java.util.Calendar)
448      */

449     public Time JavaDoc getTime(String JavaDoc columnName, Calendar JavaDoc cal) throws InvalidResultSetAccessException {
450         try {
451             return this.resultSet.getTime(columnName, cal);
452         }
453         catch (SQLException JavaDoc se) {
454             throw new InvalidResultSetAccessException(se);
455         }
456     }
457     
458     /**
459      * @see java.sql.ResultSet#getTime(String)
460      */

461     public Time JavaDoc getTime(String JavaDoc columnName) throws InvalidResultSetAccessException {
462         try {
463             return this.resultSet.getTime(columnName);
464         }
465         catch (SQLException JavaDoc se) {
466             throw new InvalidResultSetAccessException(se);
467         }
468     }
469     
470     /**
471      * @see java.sql.ResultSet#getTimestamp(int, java.util.Calendar)
472      */

473     public Timestamp JavaDoc getTimestamp(int columnIndex, Calendar JavaDoc cal)
474             throws InvalidResultSetAccessException {
475         try {
476             return this.resultSet.getTimestamp(columnIndex, cal);
477         }
478         catch (SQLException JavaDoc se) {
479             throw new InvalidResultSetAccessException(se);
480         }
481     }
482     
483     /**
484      * @see java.sql.ResultSet#getTimestamp(int)
485      */

486     public Timestamp JavaDoc getTimestamp(int columnIndex) throws InvalidResultSetAccessException {
487         try {
488             return this.resultSet.getTimestamp(columnIndex);
489         }
490         catch (SQLException JavaDoc se) {
491             throw new InvalidResultSetAccessException(se);
492         }
493     }
494
495     /**
496      * @see java.sql.ResultSet#getTimestamp(String, java.util.Calendar)
497      */

498     public Timestamp JavaDoc getTimestamp(String JavaDoc columnName, Calendar JavaDoc cal)
499             throws InvalidResultSetAccessException {
500         try {
501             return this.resultSet.getTimestamp(columnName, cal);
502         }
503         catch (SQLException JavaDoc se) {
504             throw new InvalidResultSetAccessException(se);
505         }
506     }
507
508     /**
509      * @see java.sql.ResultSet#getTimestamp(String)
510      */

511     public Timestamp JavaDoc getTimestamp(String JavaDoc columnName) throws InvalidResultSetAccessException {
512         try {
513             return this.resultSet.getTimestamp(columnName);
514         }
515         catch (SQLException JavaDoc se) {
516             throw new InvalidResultSetAccessException(se);
517         }
518     }
519
520
521     // RowSet navigation methods
522

523     /**
524      * @see java.sql.ResultSet#absolute(int)
525      */

526     public boolean absolute(int row) throws InvalidResultSetAccessException {
527         try {
528             return this.resultSet.absolute(row);
529         }
530         catch (SQLException JavaDoc se) {
531             throw new InvalidResultSetAccessException(se);
532         }
533     }
534
535     /**
536      * @see java.sql.ResultSet#afterLast()
537      */

538     public void afterLast() throws InvalidResultSetAccessException {
539         try {
540             this.resultSet.afterLast();
541         }
542         catch (SQLException JavaDoc se) {
543             throw new InvalidResultSetAccessException(se);
544         }
545     }
546     
547     /**
548      * @see java.sql.ResultSet#beforeFirst()
549      */

550     public void beforeFirst() throws InvalidResultSetAccessException {
551         try {
552             this.resultSet.beforeFirst();
553         }
554         catch (SQLException JavaDoc se) {
555             throw new InvalidResultSetAccessException(se);
556         }
557     }
558     
559     /**
560      * @see java.sql.ResultSet#first()
561      */

562     public boolean first() throws InvalidResultSetAccessException {
563         try {
564             return this.resultSet.first();
565         }
566         catch (SQLException JavaDoc se) {
567             throw new InvalidResultSetAccessException(se);
568         }
569     }
570
571     /**
572      * @see java.sql.ResultSet#getRow()
573      */

574     public int getRow() throws InvalidResultSetAccessException {
575         try {
576             return this.resultSet.getRow();
577         }
578         catch (SQLException JavaDoc se) {
579             throw new InvalidResultSetAccessException(se);
580         }
581     }
582
583     /**
584      * @see java.sql.ResultSet#isAfterLast()
585      */

586     public boolean isAfterLast() throws InvalidResultSetAccessException {
587         try {
588             return this.resultSet.isAfterLast();
589         }
590         catch (SQLException JavaDoc se) {
591             throw new InvalidResultSetAccessException(se);
592         }
593     }
594
595     /**
596      * @see java.sql.ResultSet#isBeforeFirst()
597      */

598     public boolean isBeforeFirst() throws InvalidResultSetAccessException {
599         try {
600             return this.resultSet.isBeforeFirst();
601         }
602         catch (SQLException JavaDoc se) {
603             throw new InvalidResultSetAccessException(se);
604         }
605     }
606     
607     /**
608      * @see java.sql.ResultSet#isFirst()
609      */

610     public boolean isFirst() throws InvalidResultSetAccessException {
611         try {
612             return this.resultSet.isFirst();
613         }
614         catch (SQLException JavaDoc se) {
615             throw new InvalidResultSetAccessException(se);
616         }
617     }
618     
619     /**
620      * @see java.sql.ResultSet#isLast()
621      */

622     public boolean isLast() throws InvalidResultSetAccessException {
623         try {
624             return this.resultSet.isLast();
625         }
626         catch (SQLException JavaDoc se) {
627             throw new InvalidResultSetAccessException(se);
628         }
629     }
630     
631     /**
632      * @see java.sql.ResultSet#last()
633      */

634     public boolean last() throws InvalidResultSetAccessException {
635         try {
636             return this.resultSet.last();
637         }
638         catch (SQLException JavaDoc se) {
639             throw new InvalidResultSetAccessException(se);
640         }
641     }
642     
643     /**
644      * @see java.sql.ResultSet#next()
645      */

646     public boolean next() throws InvalidResultSetAccessException {
647         try {
648             return this.resultSet.next();
649         }
650         catch (SQLException JavaDoc se) {
651             throw new InvalidResultSetAccessException(se);
652         }
653     }
654     
655     /**
656      * @see java.sql.ResultSet#previous()
657      */

658     public boolean previous() throws InvalidResultSetAccessException {
659         try {
660             return this.resultSet.previous();
661         }
662         catch (SQLException JavaDoc se) {
663             throw new InvalidResultSetAccessException(se);
664         }
665     }
666     
667     /**
668      * @see java.sql.ResultSet#relative(int)
669      */

670     public boolean relative(int rows) throws InvalidResultSetAccessException {
671         try {
672             return this.resultSet.relative(rows);
673         }
674         catch (SQLException JavaDoc se) {
675             throw new InvalidResultSetAccessException(se);
676         }
677     }
678     
679     /**
680      * @see java.sql.ResultSet#wasNull()
681      */

682     public boolean wasNull() throws InvalidResultSetAccessException {
683         try {
684             return this.resultSet.wasNull();
685         }
686         catch (SQLException JavaDoc se) {
687             throw new InvalidResultSetAccessException(se);
688         }
689     }
690
691 }
692
Popular Tags