View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.dbutils.handlers;
18  
19  import java.sql.ResultSet;
20  import java.sql.SQLException;
21  
22  import org.apache.commons.dbutils.ResultSetHandler;
23  
24  /**
25   * <code>ResultSetHandler</code> implementation that converts one
26   * <code>ResultSet</code> column into an Object. This class is thread safe.
27   * 
28   * @see org.apache.commons.dbutils.ResultSetHandler
29   */
30  public class ScalarHandler implements ResultSetHandler<Object> {
31  
32      /**
33       * The column number to retrieve.
34       */
35      private final int columnIndex;
36  
37      /**
38       * The column name to retrieve.  Either columnName or columnIndex
39       * will be used but never both.
40       */
41      private final String columnName;
42  
43      /** 
44       * Creates a new instance of ScalarHandler.  The first column will
45       * be returned from <code>handle()</code>.
46       */
47      public ScalarHandler() {
48          this(1, null);
49      }
50  
51      /** 
52       * Creates a new instance of ScalarHandler.
53       * 
54       * @param columnIndex The index of the column to retrieve from the 
55       * <code>ResultSet</code>.
56       */
57      public ScalarHandler(int columnIndex) {
58          this(columnIndex, null);
59      }
60  
61      /** 
62       * Creates a new instance of ScalarHandler.
63       * 
64       * @param columnName The name of the column to retrieve from the 
65       * <code>ResultSet</code>.
66       */
67      public ScalarHandler(String columnName) {
68          this(1, columnName);
69      }
70  
71      /** Helper constructor
72       * @param columnIndex The index of the column to retrieve from the 
73       * <code>ResultSet</code>.
74       * @param columnName The name of the column to retrieve from the 
75       * <code>ResultSet</code>.
76       */
77      private ScalarHandler(int columnIndex, String columnName){
78          this.columnIndex = columnIndex;
79          this.columnName = columnName;        
80      }
81  
82      /**
83       * Returns one <code>ResultSet</code> column as an object via the
84       * <code>ResultSet.getObject()</code> method that performs type 
85       * conversions.
86       * @param rs <code>ResultSet</code> to process.
87       * @return The column or <code>null</code> if there are no rows in
88       * the <code>ResultSet</code>.
89       * 
90       * @throws SQLException if a database access error occurs
91       * 
92       * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
93       */
94      public Object handle(ResultSet rs) throws SQLException {
95  
96          if (rs.next()) {
97              if (this.columnName == null) {
98                  return rs.getObject(this.columnIndex);
99              } else {
100                 return rs.getObject(this.columnName);
101             }
102 
103         } else {
104             return null;
105         }
106     }
107 }