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 */ 017package org.apache.commons.dbutils.handlers; 018 019import java.sql.ResultSet; 020import java.sql.SQLException; 021 022/** 023 * {@code ResultSetHandler} implementation that converts one 024 * {@code ResultSet} column into a {@code List} of 025 * {@code Object}s. This class is thread safe. 026 * 027 * @param <T> The type of the column. 028 * @see org.apache.commons.dbutils.ResultSetHandler 029 * @since 1.1 030 */ 031public class ColumnListHandler<T> extends AbstractListHandler<T> { 032 033 /** 034 * The column number to retrieve. 035 */ 036 private final int columnIndex; 037 038 /** 039 * The column name to retrieve. Either columnName or columnIndex 040 * will be used but never both. 041 */ 042 private final String columnName; 043 044 /** 045 * Creates a new instance of ColumnListHandler. The first column of each 046 * row will be returned from {@code handle()}. 047 */ 048 public ColumnListHandler() { 049 this(1, null); 050 } 051 052 /** 053 * Creates a new instance of ColumnListHandler. 054 * 055 * @param columnIndex The index of the column to retrieve from the 056 * {@code ResultSet}. 057 */ 058 public ColumnListHandler(final int columnIndex) { 059 this(columnIndex, null); 060 } 061 062 /** Private Helper 063 * @param columnIndex The index of the column to retrieve from the 064 * {@code ResultSet}. 065 * @param columnName The name of the column to retrieve from the 066 * {@code ResultSet}. 067 */ 068 private ColumnListHandler(final int columnIndex, final String columnName) { 069 this.columnIndex = columnIndex; 070 this.columnName = columnName; 071 } 072 073 /** 074 * Creates a new instance of ColumnListHandler. 075 * 076 * @param columnName The name of the column to retrieve from the 077 * {@code ResultSet}. 078 */ 079 public ColumnListHandler(final String columnName) { 080 this(1, columnName); 081 } 082 083 /** 084 * Returns one {@code ResultSet} column value as {@code Object}. 085 * @param resultSet {@code ResultSet} to process. 086 * @return {@code Object}, never {@code null}. 087 * 088 * @throws SQLException if a database access error occurs 089 * @throws ClassCastException if the class datatype does not match the column type 090 * 091 * @see org.apache.commons.dbutils.handlers.AbstractListHandler#handle(ResultSet) 092 */ 093 // We assume that the user has picked the correct type to match the column 094 // so getObject will return the appropriate type and the cast will succeed. 095 @SuppressWarnings("unchecked") 096 @Override 097 protected T handleRow(final ResultSet resultSet) throws SQLException { 098 if (this.columnName == null) { 099 return (T) resultSet.getObject(this.columnIndex); 100 } 101 return (T) resultSet.getObject(this.columnName); 102 } 103 104}