From 60706c2043f3761abce43b2cdc335af0da0e55f0 Mon Sep 17 00:00:00 2001 From: RenKaiGe-Office Date: Fri, 20 Jul 2018 10:49:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=9F=E5=A7=8B=E6=97=A5=E5=BF=97=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=97=B6,=E4=B8=8D=E4=BB=8EObject...=20obj=E4=B8=AD?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=9C=80=E8=A6=81date=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AE=B5=E4=BA=86,=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E5=AE=B9=E6=98=93=E6=BC=8F=E5=86=99?= =?UTF-8?q?,=E6=94=B9=E4=B8=BA=E5=8F=8D=E5=B0=84=E8=8E=B7=E5=8F=96date?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E7=9A=84=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/nis/util/HiveJDBC.java | 39 +++++++++++++++++++----- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/nis/util/HiveJDBC.java b/src/main/java/com/nis/util/HiveJDBC.java index b611268..a000f25 100644 --- a/src/main/java/com/nis/util/HiveJDBC.java +++ b/src/main/java/com/nis/util/HiveJDBC.java @@ -135,13 +135,15 @@ public class HiveJDBC { Map filedAndColumnMap = getColumn2FiledMap(entityClass); List listString = new ArrayList(); List listObject = new ArrayList(); - List columnList = null; - if (null != obj && obj.length > 0) { - columnList = new ArrayList(); - for (int i = 0; i < obj.length; i++) { - columnList.add(obj[i].toString().toLowerCase()); - } - } + //不从Object... obj中获取需要date类型的字段了,调用的时候容易漏写,改为反射获取date类型的字段 + List columnList =getDateColumn(entityClass); +// List columnList =null; +// if (null != obj && obj.length > 0) { +// columnList = new ArrayList(); +// for (int i = 0; i < obj.length; i++) { +// columnList.add(obj[i].toString().toLowerCase()); +// } +// } // ResultSet rs = HiveJDBC.query(sql.toString()); ResultSetMetaData metaData = rs.getMetaData(); while (rs.next()) { @@ -186,7 +188,7 @@ public class HiveJDBC { } else { mapList.put("str", listString.subList(startNum, listString.size())); } - + } else { mapList.put("str", new ArrayList()); } @@ -203,6 +205,27 @@ public class HiveJDBC { return mapList; } + /** + * 反射获取类中date类型的字段名称 + * @param type + * @return + * @throws Exception + */ + public static List getDateColumn(Class type) throws Exception { + List columnList = new ArrayList(); + BeanInfo beanInfo = Introspector.getBeanInfo(type); + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + for (int i = 0; i < propertyDescriptors.length; i++) { + PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; + String name = propertyDescriptor.getName(); + String fieldTypeName = propertyDescriptor.getPropertyType().getName(); + if (fieldTypeName.equals("java.util.Date")) { + columnList.add(name.toLowerCase()); + } + } + return columnList; + } + public static Object map2Obj(Map map, Class type) throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(type); Object obj = type.newInstance();