KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2006 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.SQLException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.springframework.util.Assert;
25
26 /**
27  * Adapter implementation of the ResultSetExtractor interface that delegates
28  * to a RowMapper which is supposed to create an object for each row.
29  * Each object is added to the results List of this ResultSetExtractor.
30  *
31  * <p>Useful for the typical case of one object per row in the database table.
32  * The number of entries in the results list will match the number of rows.
33  *
34  * <p>Note that a RowMapper object is typically stateless and thus reusable;
35  * just the RowMapperResultSetExtractor adapter is stateful.
36  *
37  * <p>A usage example with JdbcTemplate:
38  *
39  * <pre class="code">JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // reusable object
40  * RowMapper rowMapper = new UserRowMapper(); // reusable object
41  *
42  * List allUsers = (List) jdbcTemplate.query(
43  * "select * from user",
44  * new RowMapperResultSetExtractor(rowMapper, 10));
45  *
46  * User user = (User) jdbcTemplate.queryForObject(
47  * "select * from user where id=?", new Object[] {id},
48  * new RowMapperResultSetExtractor(rowMapper, 1));</pre>
49  *
50  * <p>Alternatively, consider subclassing MappingSqlQuery from the <code>jdbc.object</code>
51  * package: Instead of working with separate JdbcTemplate and RowMapper objects,
52  * you can have executable query objects (containing row-mapping logic) there.
53  *
54  * @author Juergen Hoeller
55  * @since 1.0.2
56  * @see RowMapper
57  * @see JdbcTemplate
58  * @see org.springframework.jdbc.object.MappingSqlQuery
59  */

60 public class RowMapperResultSetExtractor implements ResultSetExtractor {
61
62     private final RowMapper rowMapper;
63
64     private final int rowsExpected;
65
66
67     /**
68      * Create a new RowMapperResultSetExtractor.
69      * @param rowMapper the RowMapper which creates an object for each row
70      */

71     public RowMapperResultSetExtractor(RowMapper rowMapper) {
72         this(rowMapper, 0);
73     }
74
75     /**
76      * Create a new RowMapperResultSetExtractor.
77      * @param rowMapper the RowMapper which creates an object for each row
78      * @param rowsExpected the number of expected rows
79      * (just used for optimized collection handling)
80      */

81     public RowMapperResultSetExtractor(RowMapper rowMapper, int rowsExpected) {
82         Assert.notNull(rowMapper, "RowMapper is required");
83         this.rowMapper = rowMapper;
84         this.rowsExpected = rowsExpected;
85     }
86
87
88     public Object JavaDoc extractData(ResultSet JavaDoc rs) throws SQLException JavaDoc {
89         List JavaDoc results = (this.rowsExpected > 0 ? new ArrayList JavaDoc(this.rowsExpected) : new ArrayList JavaDoc());
90         int rowNum = 0;
91         while (rs.next()) {
92             results.add(this.rowMapper.mapRow(rs, rowNum++));
93         }
94         return results;
95     }
96
97 }
98
Popular Tags