首页>代码>java开发医疗信息管理系统源代码下载>/医疗/HospitalSystem/HospitalSystem/src/com/tsinghuait/st0717/hospitalsystem/common/ResultSetToBean.java
package com.tsinghuait.st0717.hospitalsystem.common; import java.lang.reflect.Method; import java.sql.Date; import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; public class ResultSetToBean { /** * 将结果集中的数据拷贝到DTO对象中去 * * @param rs * 数据库结果集对象 * @param dto * dto对象 */ public static Collection convertResultSet2DTOCollection(ResultSet rs, Class cls,int rsBeginIndex,int size) { Logger log = Logger.getLogger(ResultSetToBean.class.getName()); Collection col = null; // 存放DTO对象的集合 Method[] methods = cls.getMethods(); // DTO对象中所有的方法 String currentMethodName = null; Method currentMethod = null; String currentColumnName = null; try { // 用于从数据库中取数据的列名对应要调用的set方法 Map<String, Method> dataMap = new HashMap<String, Method>(); // 获取数据的列名对应要调用的set方法的map对象 for (int i = 0; i < methods.length; i++) { currentMethodName = methods[i].getName(); if ("set".equals(currentMethodName.subSequence(0, 3))) { dataMap.put(currentMethodName.substring(3), methods[i]); } } Set<String> columns = dataMap.keySet(); if (rs != null && rs.absolute(rsBeginIndex)) { col = new ArrayList(); // 实例化集合对象,用于存放DTO int count = 0; do { Object obj = cls.newInstance(); // 实例化dto对象 Iterator<String> colIterator = columns.iterator(); while (colIterator.hasNext()) { currentColumnName = colIterator.next(); currentMethod = dataMap.get(currentColumnName); if (currentMethod.getParameterTypes()[0] == String.class) { currentMethod.invoke(obj, rs .getString(currentColumnName)); } else if (currentMethod.getParameterTypes()[0] == Integer.class) { currentMethod.invoke(obj, rs .getInt(currentColumnName)); } else if (currentMethod.getParameterTypes()[0] == Date.class) { currentMethod.invoke(obj, rs .getDate(currentColumnName)); } else { log.log(Level.WARNING, "有部分数据转换失败"); //throw new Exception("数据转换失败!"); } } count++; col.add(obj); } while (rs.next() && count<size); } } catch (Exception e) { e.printStackTrace(); } return col; } }
最近下载更多
王举洪 LV1
1月25日
sl0018 LV13
2023年12月19日
lfcwan520 LV2
2023年11月8日
syhsyhzxhzxh LV3
2023年10月23日
不止是六位数 LV7
2023年3月20日
微信网友_6332243429347328 LV1
2023年2月2日
快乐的程序员 LV25
2023年1月21日
Laurae LV1
2022年11月26日
yuuuuuuuuu LV1
2022年11月25日
hosinvm LV7
2022年11月18日
最近浏览更多
微信网友_6914427966033920 LV4
3月25日
wangxiaolaugh LV2
3月23日
谢谢谢谢谢谢你 LV6
2月28日
yxzzxy LV3
2月15日
程健飞 LV2
2月1日
王举洪 LV1
1月25日
微信网友_6827593093173248
1月18日
暂无贡献等级
Mirage无衣
1月17日
暂无贡献等级
247698755 LV5
1月16日
微信网友_6818845589049344
1月15日
暂无贡献等级