RowProcesser接口源码及示例

/*
 * 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{
	
	@Override
	public Object[] toArray(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object toBean(ResultSet rs, Class clazz) throws SQLException {
		// TODO Auto-generated method stub
		Object 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 block
			e.printStackTrace();
		}
		
		return obj;
	}

	@Override
	public List toBeanList(ResultSet arg0, Class arg1) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map toMap(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		Map<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

@Test
	public 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对应的值为key
		
		System.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}}}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值