Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.commons.dbutils.handlers;
 
 import java.util.Map;
 

ResultSetHandler implementation that returns a Map of Maps. ResultSet rows are converted into Maps which are then stored in a Map under the given key.

If you had a Person table with a primary key column called ID, you could retrieve rows from the table like this:

 ResultSetHandler h = new KeyedHandler("id");
 Map found = (Map) queryRunner.query("select id, name, age from person", h);
 Map jane = (Map) found.get(new Long(1)); // jane's id is 1
 String janesName = (String) jane.get("name");
 Integer janesAge = (Integer) jane.get("age");
 
Note that the "id" passed to KeyedHandler and "name" and "age" passed to the returned Map's get() method can be in any case. The data types returned for name and age are dependent upon how your JDBC driver converts SQL column types from the Person table into Java types.

This class is thread safe.

Parameters:
<K> The type of the key
Since:
DbUtils 1.1
See also:
org.apache.commons.dbutils.ResultSetHandler
 
 public class KeyedHandler<K> extends AbstractKeyedHandler<K, Map<StringObject>> {

    
The RowProcessor implementation to use when converting rows into Objects.
 
     protected final RowProcessor convert;

    
The column index to retrieve key values from. Defaults to 1.
 
     protected final int columnIndex;

    
The column name to retrieve key values from. Either columnName or columnIndex will be used but never both.
 
     protected final String columnName;

    
Creates a new instance of KeyedHandler. The value of the first column of each row will be a key in the Map.
 
     public KeyedHandler() {
         this(., 1, null);
     }

    
Creates a new instance of KeyedHandler. The value of the first column of each row will be a key in the Map.

Parameters:
convert The RowProcessor implementation to use when converting rows into Maps
 
     public KeyedHandler(RowProcessor convert) {
         this(convert, 1, null);
     }

    
Creates a new instance of KeyedHandler.

Parameters:
columnIndex The values to use as keys in the Map are retrieved from the column at this index.
 
     public KeyedHandler(int columnIndex) {
         this(.columnIndexnull);
     }

    
Creates a new instance of KeyedHandler.

Parameters:
columnName The values to use as keys in the Map are retrieved from the column with this name.
    public KeyedHandler(String columnName) {
        this(., 1, columnName);
    }

    
Private Helper

Parameters:
convert The RowProcessor implementation to use when converting rows into Maps
columnIndex The values to use as keys in the Map are retrieved from the column at this index.
columnName The values to use as keys in the Map are retrieved from the column with this name.
    private KeyedHandler(RowProcessor convertint columnIndex,
            String columnName) {
        super();
        this. = convert;
        this. = columnIndex;
        this. = columnName;
    }
    
This factory method is called by handle() to retrieve the key value from the current ResultSet row. This implementation returns ResultSet.getObject() for the configured key column name or index.

Parameters:
rs ResultSet to create a key from
Returns:
Object from the configured key column name/index
Throws:
java.sql.SQLException if a database access error occurs
java.lang.ClassCastException if the class datatype does not match the column type
    // We assume that the user has picked the correct type to match the column
    // so getObject will return the appropriate type and the cast will succeed.
    @SuppressWarnings("unchecked")
    @Override
    protected K createKey(ResultSet rsthrows SQLException {
        return ( == null) ?
               (K) rs.getObject() :
               (K) rs.getObject();
    }

    
This factory method is called by handle() to store the current ResultSet row in some object. This implementation returns a Map with case insensitive column names as keys. Calls to map.get("COL") and map.get("col") return the same value.

Parameters:
rs ResultSet to create a row from
Returns:
Object typed Map containing column names to values
Throws:
java.sql.SQLException if a database access error occurs
    @Override
    protected Map<StringObjectcreateRow(ResultSet rsthrows SQLException {
        return this..toMap(rs);
    }
New to GrepCode? Check out our FAQ X