001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.dbutils.handlers;
018
019 import java.sql.ResultSet;
020 import java.sql.SQLException;
021
022 /**
023 * <code>ResultSetHandler</code> implementation that converts one
024 * <code>ResultSet</code> column into a <code>List</code> of
025 * <code>Object</code>s. This class is thread safe.
026 *
027 * @see org.apache.commons.dbutils.ResultSetHandler
028 * @since DbUtils 1.1
029 */
030 public class ColumnListHandler extends AbstractListHandler {
031
032 /**
033 * The column number to retrieve.
034 */
035 private final int columnIndex;
036
037 /**
038 * The column name to retrieve. Either columnName or columnIndex
039 * will be used but never both.
040 */
041 private final String columnName;
042
043 /**
044 * Creates a new instance of ColumnListHandler. The first column of each
045 * row will be returned from <code>handle()</code>.
046 */
047 public ColumnListHandler() {
048 this(1, null);
049 }
050
051 /**
052 * Creates a new instance of ColumnListHandler.
053 *
054 * @param columnIndex The index of the column to retrieve from the
055 * <code>ResultSet</code>.
056 */
057 public ColumnListHandler(int columnIndex) {
058 this(columnIndex, null);
059 }
060
061 /**
062 * Creates a new instance of ColumnListHandler.
063 *
064 * @param columnName The name of the column to retrieve from the
065 * <code>ResultSet</code>.
066 */
067 public ColumnListHandler(String columnName) {
068 this(1, columnName);
069 }
070
071 // Helper
072 private ColumnListHandler(int columnIndex, String columnName) {
073 super();
074 this.columnIndex = columnIndex;
075 this.columnName = columnName;
076 }
077
078 /**
079 * Returns one <code>ResultSet</code> column value as <code>Object</code>.
080 *
081 * @return <code>Object</code>, never <code>null</code>.
082 *
083 * @throws SQLException if a database access error occurs
084 *
085 * @see org.apache.commons.dbutils.handlers.AbstractListHandler#handle(ResultSet)
086 */
087 protected Object handleRow(ResultSet rs) throws SQLException {
088 if (this.columnName == null) {
089 return rs.getObject(this.columnIndex);
090 } else {
091 return rs.getObject(this.columnName);
092 }
093 }
094
095 }