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 import org.apache.commons.dbutils.ResultSetHandler;
023
024 /**
025 * <code>ResultSetHandler</code> implementation that converts one
026 * <code>ResultSet</code> column into an Object. This class is thread safe.
027 *
028 * @see org.apache.commons.dbutils.ResultSetHandler
029 */
030 public class ScalarHandler implements ResultSetHandler<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 ScalarHandler. The first column will
045 * be returned from <code>handle()</code>.
046 */
047 public ScalarHandler() {
048 this(1, null);
049 }
050
051 /**
052 * Creates a new instance of ScalarHandler.
053 *
054 * @param columnIndex The index of the column to retrieve from the
055 * <code>ResultSet</code>.
056 */
057 public ScalarHandler(int columnIndex) {
058 this(columnIndex, null);
059 }
060
061 /**
062 * Creates a new instance of ScalarHandler.
063 *
064 * @param columnName The name of the column to retrieve from the
065 * <code>ResultSet</code>.
066 */
067 public ScalarHandler(String columnName) {
068 this(1, columnName);
069 }
070
071 /** Helper constructor
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 ScalarHandler(int columnIndex, String columnName) {
078 this.columnIndex = columnIndex;
079 this.columnName = columnName;
080 }
081
082 /**
083 * Returns one <code>ResultSet</code> column as an object via the
084 * <code>ResultSet.getObject()</code> method that performs type
085 * conversions.
086 * @param rs <code>ResultSet</code> to process.
087 * @return The column or <code>null</code> if there are no rows in
088 * the <code>ResultSet</code>.
089 *
090 * @throws SQLException if a database access error occurs
091 *
092 * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
093 */
094 public Object handle(ResultSet rs) throws SQLException {
095
096 if (rs.next()) {
097 if (this.columnName == null) {
098 return rs.getObject(this.columnIndex);
099 } else {
100 return rs.getObject(this.columnName);
101 }
102
103 } else {
104 return null;
105 }
106 }
107 }