01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20: import java.sql.SQLException;
21:
22: import javax.sql.rowset.CachedRowSet;
23:
24: import com.sun.rowset.CachedRowSetImpl;
25:
26: import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet;
27: import org.springframework.jdbc.support.rowset.SqlRowSet;
28:
29: /**
30: * ResultSetExtractor implementation that returns a Spring SqlRowSet
31: * representation for each given ResultSet.
32: *
33: * <p>The default implementation uses a standard JDBC CachedRowSet underneath.
34: * This means that JDBC RowSet support needs to be available at runtime:
35: * by default, Sun's <code>com.sun.rowset.CachedRowSetImpl</code> class is
36: * used, which is part of JDK 1.5+ and also available separately as part of
37: * Sun's JDBC RowSet Implementations download (rowset.jar).
38: *
39: * @author Juergen Hoeller
40: * @since 1.2
41: * @see #newCachedRowSet
42: * @see org.springframework.jdbc.support.rowset.SqlRowSet
43: * @see JdbcTemplate#queryForRowSet(String)
44: * @see javax.sql.rowset.CachedRowSet
45: */
46: public class SqlRowSetResultSetExtractor implements ResultSetExtractor {
47:
48: public Object extractData(ResultSet rs) throws SQLException {
49: return createSqlRowSet(rs);
50: }
51:
52: /**
53: * Create a SqlRowSet that wraps the given ResultSet,
54: * representing its data in a disconnected fashion.
55: * <p>This implementation creates a Spring ResultSetWrappingSqlRowSet
56: * instance that wraps a standard JDBC CachedRowSet instance.
57: * Can be overridden to use a different implementation.
58: * @param rs the original ResultSet (connected)
59: * @return the disconnected SqlRowSet
60: * @throws SQLException if thrown by JDBC methods
61: * @see #newCachedRowSet
62: * @see org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet
63: */
64: protected SqlRowSet createSqlRowSet(ResultSet rs)
65: throws SQLException {
66: CachedRowSet rowSet = newCachedRowSet();
67: rowSet.populate(rs);
68: return new ResultSetWrappingSqlRowSet(rowSet);
69: }
70:
71: /**
72: * Create a new CachedRowSet instance, to be populated by
73: * the <code>createSqlRowSet</code> implementation.
74: * <p>The default implementation creates a new instance of
75: * Sun's <code>com.sun.rowset.CachedRowSetImpl</code> class,
76: * which is part of JDK 1.5+ and also available separately
77: * as part of Sun's JDBC RowSet Implementations download.
78: * @return a new CachedRowSet instance
79: * @throws SQLException if thrown by JDBC methods
80: * @see #createSqlRowSet
81: * @see com.sun.rowset.CachedRowSetImpl
82: */
83: protected CachedRowSet newCachedRowSet() throws SQLException {
84: return new CachedRowSetImpl();
85: }
86:
87: }
|