RowProcesser接口源码及示例

来源:互联网 发布:手机会议纪要软件 编辑:程序博客网 时间:2024/05/16 13:38
/* * 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;import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;import java.util.Map;/** * <code>RowProcessor</code> implementations convert * <code>ResultSet</code> rows into various other objects.  Implementations * can extend <code>BasicRowProcessor</code> to protect themselves * from changes to this interface. * * @see BasicRowProcessor */public interface RowProcessor {    /**     * Create an <code>Object[]</code> from the column values in one     * <code>ResultSet</code> row.  The <code>ResultSet</code> should be     * positioned on a valid row before passing it to this method.     * Implementations of this method must not alter the row position of     * the <code>ResultSet</code>.     *     * @param rs ResultSet that supplies the array data     * @throws SQLException if a database access error occurs     * @return the newly created array     */    Object[] toArray(ResultSet rs) throws SQLException;    /**     * Create a JavaBean from the column values in one <code>ResultSet</code>     * row.  The <code>ResultSet</code> should be positioned on a valid row before     * passing it to this method.  Implementations of this method must not     * alter the row position of the <code>ResultSet</code>.     * @param <T> The type of bean to create     * @param rs ResultSet that supplies the bean data     * @param type Class from which to create the bean instance     * @throws SQLException if a database access error occurs     * @return the newly created bean     */    <T> T toBean(ResultSet rs, Class<T> type) throws SQLException;    /**     * Create a <code>List</code> of JavaBeans from the column values in all     * <code>ResultSet</code> rows.  <code>ResultSet.next()</code> should     * <strong>not</strong> be called before passing it to this method.     * @param <T> The type of bean to create     * @param rs ResultSet that supplies the bean data     * @param type Class from which to create the bean instance     * @throws SQLException if a database access error occurs     * @return A <code>List</code> of beans with the given type in the order     * they were returned by the <code>ResultSet</code>.     */    <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException;    /**     * Create a <code>Map</code> from the column values in one     * <code>ResultSet</code> row.  The <code>ResultSet</code> should be     * positioned on a valid row before     * passing it to this method.  Implementations of this method must not     * alter the row position of the <code>ResultSet</code>.     *     * @param rs ResultSet that supplies the map data     * @throws SQLException if a database access error occurs     * @return the newly created Map     */    Map<String, Object> toMap(ResultSet rs) throws SQLException;}


示例:

class myRowProcesser implements RowProcessor{@Overridepublic Object[] toArray(ResultSet rs) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Object toBean(ResultSet rs, Class clazz) throws SQLException {// TODO Auto-generated method stubObject obj = null;try {obj = clazz.newInstance();ResultSetMetaData rsmt = rs.getMetaData();if(rs.next()){int count = rsmt.getColumnCount();while(count>0){Field f = clazz.getDeclaredField(rsmt.getColumnName(count));f.setAccessible(true);f.set(obj, rs.getObject(count));count--;}}}catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return obj;}@Overridepublic List toBeanList(ResultSet arg0, Class arg1) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Map toMap(ResultSet rs) throws SQLException {// TODO Auto-generated method stubMap<Object, Map> map1 = new HashMap<Object, Map>();while(rs.next()){Map<Object, Object> map2 = new HashMap<Object, Object>();ResultSetMetaData rsmt = rs.getMetaData();int count = rsmt.getColumnCount();while(count>0){map2.put(rsmt.getColumnName(count), rs.getObject(count));count--;}map1.put(rs.getObject(1), map2);}for(Entry<Object, Map> entry:map1.entrySet()){System.out.println(entry.getKey()+":"+entry.getValue());Map<Integer, Object> map2 = entry.getValue();for(Entry<Integer, Object> entry2: map2.entrySet()){System.out.println("\t"+entry2.getKey()+":"+entry2.getValue());}}if(map1.size()>0){return map1;}else{return null;}}}

//demo

@Testpublic void keyedHandlerTest() throws SQLException{String sql = "select * from account";QueryRunner qr = new QueryRunner(JdbcC3p0Utils.getDataSource());Object map = qr.query(sql, new KeyedHandler(new myRowProcesser()));//指定第二列的,即name对应的值为keySystem.out.println(map);}
//结果:

{1={5={id=5, name=fff, money=1800.0}, 6={id=6, name=fff, money=1900.0}, 8={id=8, name=111, money=900.0}, 9={id=9, name=222, money=1900.0}, 10={id=10, name=333, money=2900.0}, 11={id=11, name=111, money=900.0}, 12={id=12, name=222, money=1900.0}, 13={id=13, name=333, money=2900.0}}}


0 0