1 package com.gu.utils; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.Method; 5 6 /** 7 * jdbc反射而常用的操作 8 * @author 谷 9 * 10 */ 11 public class reflectUtils { 12 13 //由属性名获得属性值 14 public static Object invokeGet( String feildName,Object obj){ 15 Method m=null; 16 Object end=null; 17 try { 18 Class c=obj.getClass(); 19 m=c.getMethod("Get"+StringUtils.firstChar2uppercas(feildName), null); 20 try { 21 end=m.invoke(obj, null); 22 } catch (IllegalAccessException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } catch (IllegalArgumentException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } catch (InvocationTargetException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 } catch (NoSuchMethodException e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 } catch (SecurityException e) { 36 // TODO Auto-generated catch block 37 e.printStackTrace(); 38 } 39 return end; 40 } 41 public static void invokeSet( String columnName,Object columnValue,Object RowObj){ 42 //调用set方法将属性值设进去 43 Method m=null; 44 try { 45 m = RowObj.getClass().getDeclaredMethod("Set"+StringUtils.firstChar2uppercase(columnName),columnValue.getClass() ); 46 } catch (NoSuchMethodException e1) { 47 // TODO Auto-generated catch block 48 e1.printStackTrace(); 49 } catch (SecurityException e1) { 50 // TODO Auto-generated catch block 51 e1.printStackTrace(); 52 } 53 try { 54 m.invoke(RowObj, columnValue); 55 } catch (IllegalAccessException e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } catch (IllegalArgumentException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } catch (InvocationTargetException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 } 66 67 }