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
- RowProcesser接口源码及示例
- 数据发送接口源码示例
- andengine源码及示例程序
- SSM框架示例及源码
- ThreadLocal示例及源码浅析
- Fabric源码编译及示例
- 老黄历算命接口实现及代码示例
- 食品新闻接口文档及返回示例
- 源码库编译全过程及示例
- StarlingMVC简介,原理解说及示例源码
- Selenide自动化测试示例及源码
- SQLiteOpenHelper源码梳理及使用示例
- jquery find()解析示例及源码解读
- ByteArrayInputStream的介绍,源码分析及示例
- Android ToolBar使用详解及示例源码
- Android SearchView使用详解及示例源码
- RSA加密 解密 示例及源码
- 接口示例
- MFC DLL——Regular DLL and Extension DLL
- Android中的Menu的简介(翻译自官方文档)——————个人笔记
- Linux驱动开发之七----按键驱动poll实现
- 在eclipse在线安装CheckStyle出错
- CSRF verification failed. Request aborted.
- RowProcesser接口源码及示例
- Factory Methods
- AD RMS个人技术博客记录
- 前缀、中缀、后缀表达式
- 【转】你的企业是否拥有互联网思维?!
- FTP客户端软件FileZilla使用教程
- jquery开发插件
- 使用设计模式改善程序结构(二)
- jquery中,html、val与text三者属性取值的联系与区别