---ResultSet数据集

    public static List toList(ResultSet rs, Class cls) {
List list = new ArrayList();
try { BeanInfo beanInfo = Introspector.getBeanInfo(cls); // 获取类属性 // 给 JavaBean 对象的属性赋值
PropertyDescriptor[] propertyDescriptors = beanInfo
.getPropertyDescriptors();
ResultSetMetaData meta = rs.getMetaData(); Object obj = null;
while (rs.next()) { obj = cls.newInstance(); // 创建 JavaBean 对象 for (int j = 1; j <= meta.getColumnCount(); j++) { for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName(); String propertyType = descriptor.getPropertyType()
.getName(); if (meta.getColumnName(j)
.equalsIgnoreCase(propertyName)) { Method method = descriptor.getWriteMethod();
Object value = rs.getObject(j);
if (propertyType.equals("java.lang.String")
&& value == null) {
method.invoke(obj, "");
} else if (propertyType.equals("java.lang.String")) {
method.invoke(obj, value.toString());
} else if (propertyType.equals("java.util.Date")) {
method.invoke(obj, (Date) value);
} else if (propertyType.equals("java.lang.Integer")) {
method.invoke(obj, new Integer((String) value));
} else if (propertyType.equals("float")) {
method.invoke(obj,
Float.parseFloat((String) value));
}else if(propertyType.equals("java.math.BigDecimal"))
{
method.invoke(obj,(BigDecimal)value);
}
else {
method.invoke(obj, value);
}
break;
}
}
}
list.add(obj);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
return list;
}
}
}

---Map数据集

public static Object convertMap(Class type, Map map) {
Object obj = null;
try { BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
obj = type.newInstance(); // 创建 JavaBean 对象 // 给 JavaBean 对象的属性赋值
PropertyDescriptor[] propertyDescriptors = beanInfo
.getPropertyDescriptors();
// 获取key的集合
Set<String> keySet = map.keySet();// 获取mapKEY for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
String strPropertyName = propertyName.toLowerCase();// 大写转小写
String propertyType=descriptor.getPropertyType().getName();
// 遍历key集合,获取value
for (String key : keySet) { String strKey = key.toLowerCase(); if (strPropertyName.equals(strKey)) {// 对比key与属性是否相同
// 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
Object value = map.get(key); /* Object[] args = new Object[1];
args[0] = value;*/ Method method= descriptor.getWriteMethod();
if(propertyType.equals("java.lang.String")){
method.invoke(obj,value.toString());
}
else if(propertyType.equals("java.util.Date")){
method.invoke(obj, (Date)value);
}
else if(propertyType.equals("java.lang.Integer")){
method.invoke(obj, new Integer((String)value));
}
else if(propertyType.equals("float")){
method.invoke(obj, Float.parseFloat((String)value));
}
else{
method.invoke(obj, value);
}
break;
}
}
}
} catch (Exception e) {
// TODO: handle exception
logger.error("map转换对象错误", e);
} return obj;
}

最新文章

  1. 解决 Oracle exp导出表数据时空表不能导出的问题
  2. KnockoutJS 3.X API 第四章 表单绑定(6) click绑定
  3. Django学习(二)
  4. Python 汉字转拼音库 pypinyin
  5. ubuntu 14.04 下FTP服务器的搭建--锁定用户目录,解决vsftpd: refusing to run with writable root inside chroot()
  6. Android Studio Error2
  7. ubuntu下MySQL安装配置及基本操作
  8. 一个简单的网页读字符串 SpeechLib
  9. SPOJ QTREE4 lct
  10. 方案猿身高project联赛,艺术家,相反,养殖场!-------三笔
  11. 使用travis-ci自动部署github上的项目
  12. USACO奶牛赛跑(逆序对)
  13. 【LintCode&#183;入门】斐波那契数列
  14. java查看程序执行时间
  15. ORACLE DB TRIGGER详解
  16. MySQL操作(备份很重要)
  17. Java 问题定位工具 ——jstack
  18. springboot 创建非web项目及数据源简单使用
  19. Qt Md5应用示例
  20. iOS.Debug.Simulator

热门文章

  1. 【测试】解决loadrunner11无法运行负载测试
  2. Bluetooth LE(低功耗蓝牙) - 第六部分(完)
  3. [LeetCode#202] Roman to Integer
  4. unity3d Human skin real time rendering with blood and water drop effect真实模拟人皮实时渲染之血液和水珠掉落效果
  5. eclipse配置j2ee项目
  6. zoj 3757 Alice and Bob and Cue Sports 月赛A 模拟
  7. 让nginx支持文件上传的几种模式
  8. Spring MVC之messageConverters
  9. 【Android - 进阶】之图片三级缓存的原理及实现
  10. 自己在安装centos 系统时, 是使用英文安装 成功,现在系统语言为英语,如何设置为中文?