View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.dbutils.handlers;
18  
19  import java.sql.ResultSet;
20  import java.sql.SQLException;
21  
22  /**
23   * {@code ResultSetHandler} implementation that converts one
24   * {@code ResultSet} column into a {@code List} of
25   * {@code Object}s. This class is thread safe.
26   *
27   * @param <T> The type of the column.
28   * @see org.apache.commons.dbutils.ResultSetHandler
29   * @since 1.1
30   */
31  public class ColumnListHandler<T> extends AbstractListHandler<T> {
32  
33      /**
34       * The column number to retrieve.
35       */
36      private final int columnIndex;
37  
38      /**
39       * The column name to retrieve.  Either columnName or columnIndex
40       * will be used but never both.
41       */
42      private final String columnName;
43  
44      /**
45       * Creates a new instance of ColumnListHandler.  The first column of each
46       * row will be returned from {@code handle()}.
47       */
48      public ColumnListHandler() {
49          this(1, null);
50      }
51  
52      /**
53       * Creates a new instance of ColumnListHandler.
54       *
55       * @param columnIndex The index of the column to retrieve from the
56       * {@code ResultSet}.
57       */
58      public ColumnListHandler(final int columnIndex) {
59          this(columnIndex, null);
60      }
61  
62      /** Private Helper
63       * @param columnIndex The index of the column to retrieve from the
64       * {@code ResultSet}.
65       * @param columnName The name of the column to retrieve from the
66       * {@code ResultSet}.
67       */
68      private ColumnListHandler(final int columnIndex, final String columnName) {
69          this.columnIndex = columnIndex;
70          this.columnName = columnName;
71      }
72  
73      /**
74       * Creates a new instance of ColumnListHandler.
75       *
76       * @param columnName The name of the column to retrieve from the
77       * {@code ResultSet}.
78       */
79      public ColumnListHandler(final String columnName) {
80          this(1, columnName);
81      }
82  
83      /**
84       * Returns one {@code ResultSet} column value as {@code Object}.
85       * @param resultSet {@code ResultSet} to process.
86       * @return {@code Object}, never {@code null}.
87       *
88       * @throws SQLException if a database access error occurs
89       * @throws ClassCastException if the class datatype does not match the column type
90       *
91       * @see org.apache.commons.dbutils.handlers.AbstractListHandler#handle(ResultSet)
92       */
93      // We assume that the user has picked the correct type to match the column
94      // so getObject will return the appropriate type and the cast will succeed.
95      @SuppressWarnings("unchecked")
96      @Override
97      protected T handleRow(final ResultSet resultSet) throws SQLException {
98          if (this.columnName == null) {
99              return (T) resultSet.getObject(this.columnIndex);
100         }
101         return (T) resultSet.getObject(this.columnName);
102    }
103 
104 }