处理JdbcTemplate放回的结果集
2013-10-20 11:27
246 查看
ResultSet中结果反射
import java.lang.reflect.*; import java.sql.*; import java.util.*; import java.util.Date; public class ResultToBean { /** * 将ResultSet内容映射到实体 须有 ColToProperty注解 * @param rs * @param clazz * @return * @throws SecurityException * @throws NoSuchMethodException * @throws InstantiationException * @throws IllegalAccessException * @throws SQLException * @throws IllegalArgumentException * @throws InvocationTargetException */ public static <T> List<T> getBean(ResultSet rs, Class<T> clazz) throws SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, SQLException, IllegalArgumentException, InvocationTargetException { List<T> l = new ArrayList<T>(); Method method = null; T obj = null; while (rs.next()) { obj = clazz.newInstance(); Field fs[] = clazz.getDeclaredFields(); int count = 1; for (Field f : fs) { ColToProperty colToProperty = f .getAnnotation(ColToProperty.class); if (null == colToProperty) { continue; } String m = f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1); if (f.getType() == java.lang.Boolean.class) { method = clazz.getDeclaredMethod("set" + m, Boolean.class); method.invoke(obj, rs.getBoolean(count)); } else if (f.getType() == boolean.class) { method = clazz.getDeclaredMethod("set" + m, boolean.class); method.invoke(obj, rs.getBoolean(count)); } else if (f.getType() == java.util.Date.class || f.getType() == Date.class) { method = clazz.getDeclaredMethod("set" + m, Date.class); method.invoke(obj, rs.getDate(count)); } else if (f.getType() == java.lang.Double.class) { method = clazz.getDeclaredMethod("set" + m, Double.class); method.invoke(obj, rs.getDouble(count)); } else if (f.getType() == Double.class) { method = clazz.getDeclaredMethod("set" + m, double.class); method.invoke(obj, rs.getDouble(count)); } else if (f.getType() == java.lang.Float.class) { method = clazz.getDeclaredMethod("set" + m, Float.class); method.invoke(obj, rs.getFloat(count)); } else if (f.getType() == Float.class) { method = clazz.getDeclaredMethod("set" + m, float.class); method.invoke(obj, rs.getFloat(count)); } else if (f.getType() == java.lang.Integer.class) { method = clazz.getDeclaredMethod("set" + m, Integer.class); method.invoke(obj, rs.getInt(count)); } else if (f.getType() == int.class) { method = clazz.getDeclaredMethod("set" + m, int.class); method.invoke(obj, rs.getInt(count)); } else if (f.getType() == java.lang.Short.class || f.getType() == short.class) { method = clazz.getDeclaredMethod("set" + m, Short.class); method.invoke(obj, rs.getShort(count)); } else if (f.getType() == java.lang.String.class) { method = clazz.getDeclaredMethod("set" + m, String.class); method.invoke(obj, rs.getString(count)); } else if (f.getType() == java.lang.Long.class || f.getType() == long.class) { method = clazz.getDeclaredMethod("set" + m, Long.class); method.invoke(obj, rs.getLong(count)); } else { throw new RuntimeException("非基础数据类型,无法正确匹配,请检查ColToProperty是否使用正确!"); } count++; } l.add(obj); } return l; } }
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface ColToProperty { }
相关文章推荐
- JdbcTemplate查询数据中两种处理结果集方法的简单比较
- 使用Python的requests库作接口测试——响应结果处理
- php中使用mysql_stmt(预处理语句)来处理select查询结果
- 处理两个分词结果的不一致
- Android中activity处理返回结果的实现方式
- 8.4 stat--stat()函数的结果处理
- java实现网上在线支付--09,10,11,12_分析易宝支付网关的应答协议与处理代码,完成用于处理支付响应的Servlet的初步编写和调试,完成处理支付网关响应结果的Servlet,支付实现
- android——处理返回结果(仿获取电话号码)
- 窗口和线程漫谈之工作线程如何将数据的处理结果显示到窗口
- Play framework处理异步结果
- ActiveRecord做多表查询得出的结果怎么处理?
- struts2支持的结果处理类型
- 显示处理结果
- java new date 结果与操作系统时间相差8小时处理解决方法
- Android使用责任链模式处理二维码扫描结果
- Android Activity间的两种跳转方式、自我关闭方法、关闭后的结果处理
- struts2 处理结果
- struts2处理结果类型
- Rxjava +Retrofit 你需要掌握的几个技巧,Retrofit缓存,RxJava封装,统一对有无网络处理,异常处理, 返回结果问题
- PostgreSQL JDBC:查询和处理结果