KickJava   Java API By Example, From Geeks To Geeks.

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


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.ResultSetMetaData JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 /**
24  * Implementation of RowCallbackHandler. Convenient superclass for callback handlers.
25  * An instance can only be used once.
26  *
27  * <p>We can either use this on its own (for example, in a test case, to ensure
28  * that our result sets have valid dimensions), or use it as a superclass
29  * for callback handlers that actually do something, and will benefit
30  * from the dimension information it provides.
31  *
32  * <p>A usage example with JdbcTemplate:
33  *
34  * <pre class="code">JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); // reusable object
35  *
36  * RowCountCallbackHandler countCallback = new RowCountCallbackHandler(); // not reusable
37  * jdbcTemplate.query("select * from user", countCallback);
38  * int rowCount = countCallback.getRowCount();</pre>
39  *
40  * @author Rod Johnson
41  * @since May 3, 2001
42  */

43 public class RowCountCallbackHandler implements RowCallbackHandler {
44
45     /** Rows we've seen so far */
46     private int rowCount;
47
48     /** Columns we've seen so far */
49     private int columnCount;
50
51     /**
52      * Indexed from 0. Type (as in java.sql.Types) for the columns
53      * as returned by ResultSetMetaData object.
54      */

55     private int[] columnTypes;
56     
57     /**
58      * Indexed from 0. Column name as returned by ResultSetMetaData object.
59      */

60     private String JavaDoc[] columnNames;
61
62
63     /**
64      * Implementation of ResultSetCallbackHandler.
65      * Work out column size if this is the first row, otherwise just count rows.
66      * <p>Subclasses can perform custom extraction or processing
67      * by overriding the <code>processRow(ResultSet, int)</code> method.
68      * @see #processRow(java.sql.ResultSet, int)
69      */

70     public final void processRow(ResultSet JavaDoc rs) throws SQLException JavaDoc {
71         if (this.rowCount == 0) {
72             ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
73             this.columnCount = rsmd.getColumnCount();
74             this.columnTypes = new int[this.columnCount];
75             this.columnNames = new String JavaDoc[this.columnCount];
76             for (int i = 0; i < this.columnCount; i++) {
77                 this.columnTypes[i] = rsmd.getColumnType(i + 1);
78                 this.columnNames[i] = rsmd.getColumnName(i + 1);
79             }
80             // could also get column names
81
}
82         processRow(rs, this.rowCount++);
83     }
84
85     /**
86      * Subclasses may override this to perform custom extraction
87      * or processing. This class's implementation does nothing.
88      * @param rs ResultSet to extract data from. This method is
89      * invoked for each row
90      * @param rowNum number of the current row (starting from 0)
91      */

92     protected void processRow(ResultSet JavaDoc rs, int rowNum) throws SQLException JavaDoc {
93     }
94
95
96     /**
97      * Return the types of the columns as java.sql.Types constants
98      * Valid after processRow is invoked the first time.
99      * @return the types of the columns as java.sql.Types constants.
100      * <b>Indexed from 0 to n-1.</b>
101      */

102     public final int[] getColumnTypes() {
103         return columnTypes;
104     }
105     
106     /**
107      * Return the names of the columns.
108      * Valid after processRow is invoked the first time.
109      * @return the names of the columns.
110      * <b>Indexed from 0 to n-1.</b>
111      */

112     public final String JavaDoc[] getColumnNames() {
113         return columnNames;
114     }
115
116     /**
117      * Return the row count of this ResultSet
118      * Only valid after processing is complete
119      * @return the number of rows in this ResultSet
120      */

121     public final int getRowCount() {
122         return rowCount;
123     }
124
125     /**
126      * Return the number of columns in this result set.
127      * Valid once we've seen the first row,
128      * so subclasses can use it during processing
129      * @return the number of columns in this result set
130      */

131     public final int getColumnCount() {
132         return columnCount;
133     }
134
135 }
136
Popular Tags