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 {
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 private ScalarHandler(int columnIndex, String columnName){
073 this.columnIndex = columnIndex;
074 this.columnName = columnName;
075 }
076
077 /**
078 * Returns one <code>ResultSet</code> column as an object via the
079 * <code>ResultSet.getObject()</code> method that performs type
080 * conversions.
081 *
082 * @return The column or <code>null</code> if there are no rows in
083 * the <code>ResultSet</code>.
084 *
085 * @throws SQLException if a database access error occurs
086 *
087 * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
088 */
089 public Object handle(ResultSet rs) throws SQLException {
090
091 if (rs.next()) {
092 if (this.columnName == null) {
093 return rs.getObject(this.columnIndex);
094 } else {
095 return rs.getObject(this.columnName);
096 }
097
098 } else {
099 return null;
100 }
101 }
102 }