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<Object> { 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 /** Private Helper 072 * @param columnIndex The index of the column to retrieve from the 073 * <code>ResultSet</code>. 074 * @param columnName The name of the column to retrieve from the 075 * <code>ResultSet</code>. 076 */ 077 private ColumnListHandler(int columnIndex, String columnName) { 078 super(); 079 this.columnIndex = columnIndex; 080 this.columnName = columnName; 081 } 082 083 /** 084 * Returns one <code>ResultSet</code> column value as <code>Object</code>. 085 * @param rs <code>ResultSet</code> to process. 086 * @return <code>Object</code>, never <code>null</code>. 087 * 088 * @throws SQLException if a database access error occurs 089 * 090 * @see org.apache.commons.dbutils.handlers.AbstractListHandler#handle(ResultSet) 091 */ 092 @Override 093 protected Object handleRow(ResultSet rs) throws SQLException { 094 if (this.columnName == null) { 095 return rs.getObject(this.columnIndex); 096 } else { 097 return rs.getObject(this.columnName); 098 } 099 } 100 101 }