日期:2014-05-16 浏览次数:20440 次
//将ResultSet中的数据转换为相应的对象 @SuppressWarnings("unchecked") private <T> T buildAlarm(T pojo, ResultSet rs) { ResultSetMetaData rsmd; try { rsmd = rs.getMetaData(); int columnsCount = rsmd.getColumnCount(); for (int i = 1; i <= columnsCount; i++) { String fieldName = rsmd.getColumnLabel(i).toLowerCase(); Object value = rs.getObject(fieldName); if(value instanceof java.sql.Date){ value = new java.util.Date(((java.sql.Date) value).getTime()); } if(value instanceof java.sql.Timestamp){ value = new java.util.Date(((java.sql.Timestamp) value).getTime()); } BeanUtils.setProperty(pojo, fieldName, value); } } catch (Exception e1) { } return pojo; } private <T> T buildAlarm(T pojo, ResultSet rs) { ResultSetMetaData rsmd; try { rsmd = rs.getMetaData(); int columnsCount = rsmd.getColumnCount(); for (int i = 1; i <= columnsCount; i++) { String fieldName = rsmd.getColumnLabel(i).toLowerCase(); Object value = rs.getObject(fieldName); if(value instanceof java.sql.Date){ value = new java.util.Date(((java.sql.Date) value).getTime()); } if(value instanceof java.sql.Timestamp){ value = new java.util.Date(((java.sql.Timestamp) value).getTime()); } BeanUtils.setProperty(pojo, fieldName, value); } } catch (Exception e1) { } return pojo; } //将ResultSet中的数据转换为相应的对象 @SuppressWarnings("unchecked") private <T> T buildAlarm_rs(T pojo, ResultSet rs) { Class classType = pojo.getClass(); ResultSetMetaData rsmd; try { rsmd = rs.getMetaData(); int columnsCount = rsmd.getColumnCount(); for (int i = 1; i <= columnsCount; i++) { String fieldName = rsmd.getColumnLabel(i).toLowerCase(); String stringLetter = fieldName.substring(0, 1).toUpperCase(); String setName = "set" + stringLetter + fieldName.substring(1); String getName = "get" + stringLetter + fieldName.substring(1); Method getMethod = classType.getMethod(getName, new Class[] {}); Method setMethod = classType.getMethod(setName, new Class[] { getMethod.getReturnType() }); Class fieldType = setMethod.getParameterTypes()[0]; Object value = null; if (fieldType == Integer.class || "int".equals(fieldType.getName())){ if(rs.getObject(fieldName)!=null) value = rs.getInt(fieldName); } else if (fieldType == Float.class || "float".equals(fieldType.getName())){ if(rs.getObject(fieldName)!=null) value = rs.getFloat(fieldName); } else if (fieldType == Double.class || "double".equals(fieldType.getName())){ if(rs.getObject(fieldName)!=null) value = rs.getDouble(fieldName); } else if (fieldType == Timestamp.class || fieldType == java.util.Date.class){ value = rs.getTimestamp(fieldName); } else value = rs.getString(fieldName); // 赋值操作 setMethod.invoke(pojo, new Object[] { value }); } } catch (Exception e1) { e1.printStackTrace(); } return pojo; }?