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 DbUtils 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 /** 063 * Creates a new instance of ColumnListHandler. 064 * 065 * @param columnName The name of the column to retrieve from the 066 * {@code ResultSet}. 067 */ 068 public ColumnListHandler(final String columnName) { 069 this(1, columnName); 070 } 071 072 /** Private Helper 073 * @param columnIndex The index of the column to retrieve from the 074 * {@code ResultSet}. 075 * @param columnName The name of the column to retrieve from the 076 * {@code ResultSet}. 077 */ 078 private ColumnListHandler(final int columnIndex, final String columnName) { 079 super(); 080 this.columnIndex = columnIndex; 081 this.columnName = columnName; 082 } 083 084 /** 085 * Returns one {@code ResultSet} column value as {@code Object}. 086 * @param rs {@code ResultSet} to process. 087 * @return {@code Object}, never {@code null}. 088 * 089 * @throws SQLException if a database access error occurs 090 * @throws ClassCastException if the class datatype does not match the column type 091 * 092 * @see org.apache.commons.dbutils.handlers.AbstractListHandler#handle(ResultSet) 093 */ 094 // We assume that the user has picked the correct type to match the column 095 // so getObject will return the appropriate type and the cast will succeed. 096 @SuppressWarnings("unchecked") 097 @Override 098 protected T handleRow(final ResultSet rs) throws SQLException { 099 if (this.columnName == null) { 100 return (T) rs.getObject(this.columnIndex); 101 } 102 return (T) rs.getObject(this.columnName); 103 } 104 105}