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.BasicRowProcessor; 023 import org.apache.commons.dbutils.ResultSetHandler; 024 import org.apache.commons.dbutils.RowProcessor; 025 026 /** 027 * <code>ResultSetHandler</code> implementation that converts a 028 * <code>ResultSet</code> into an <code>Object[]</code>. This class is 029 * thread safe. 030 * 031 * @see org.apache.commons.dbutils.ResultSetHandler 032 */ 033 public class ArrayHandler implements ResultSetHandler<Object[]> { 034 035 /** 036 * Singleton processor instance that handlers share to save memory. Notice 037 * the default scoping to allow only classes in this package to use this 038 * instance. 039 */ 040 static final RowProcessor ROW_PROCESSOR = new BasicRowProcessor(); 041 042 /** 043 * The RowProcessor implementation to use when converting rows 044 * into arrays. 045 */ 046 private final RowProcessor convert; 047 048 /** 049 * Creates a new instance of ArrayHandler using a 050 * <code>BasicRowProcessor</code> for conversion. 051 */ 052 public ArrayHandler() { 053 this(ROW_PROCESSOR); 054 } 055 056 /** 057 * Creates a new instance of ArrayHandler. 058 * 059 * @param convert The <code>RowProcessor</code> implementation 060 * to use when converting rows into arrays. 061 */ 062 public ArrayHandler(RowProcessor convert) { 063 super(); 064 this.convert = convert; 065 } 066 067 /** 068 * Places the column values from the first row in an <code>Object[]</code>. 069 * @param rs <code>ResultSet</code> to process. 070 * @return An Object[] or <code>null</code> if there are no rows in the 071 * <code>ResultSet</code>. 072 * 073 * @throws SQLException if a database access error occurs 074 * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) 075 */ 076 public Object[] handle(ResultSet rs) throws SQLException { 077 return rs.next() ? this.convert.toArray(rs) : null; 078 } 079 080 }