KickJava   Java API By Example, From Geeks To Geeks.

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


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

58 public class RowMapperResultReader implements ResultReader {
59
60     /** List to save results in */
61     private final List JavaDoc results;
62
63     /** The RowMapper implementation that will be used to map rows */
64     private final RowMapper rowMapper;
65
66     /** The counter used to count rows */
67     private int rowNum = 0;
68
69     /**
70      * Create a new RowMapperResultReader.
71      * @param rowMapper the RowMapper which creates an object for each row
72      */

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

83     public RowMapperResultReader(RowMapper rowMapper, int rowsExpected) {
84         // Use the more efficient collection if we know how many rows to expect:
85
// ArrayList in case of a known row count, LinkedList if unknown
86
this.results = (rowsExpected > 0) ? (List JavaDoc) new ArrayList JavaDoc(rowsExpected) : (List JavaDoc) new LinkedList JavaDoc();
87         this.rowMapper = rowMapper;
88     }
89
90     public void processRow(ResultSet JavaDoc rs) throws SQLException JavaDoc {
91         this.results.add(this.rowMapper.mapRow(rs, this.rowNum++));
92     }
93
94     public List JavaDoc getResults() {
95         return this.results;
96     }
97
98 }
99
Popular Tags