当前位置 : 主页 > 编程语言 > c++ >

java工具类连接数据库修改数据

来源:互联网 收集:自由互联 发布时间:2021-07-03
gistfile1.txt /**修改对象*/public static int updateObject(T t){int i=0;Class c=t.getClass();List columns=new ArrayList (); List values=new ArrayList (); StringBuffer sb=new StringBuffer(); for (Field fi : c.getDeclaredFields()) { if (is
gistfile1.txt
/**修改对象*/
	public static 
 
   int updateObject(T t){
		int i=0;
		Class c=t.getClass();
		List
  
    columns=new ArrayList
   
    (); List
     values=new ArrayList(); StringBuffer sb=new StringBuffer(); for (Field fi : c.getDeclaredFields()) { if (isBasicType(fi.getType())) columns.add(fi.getName()); } sb.append("update "); sb.append(c.getSimpleName()); sb.append(" set "); for (String string : columns) { sb.append(string); sb.append("=?,"); } sb.delete(sb.length()-1, sb.length()); sb.append("where id=?"); for (String s:columns) { for (Method mt: c.getMethods()) { if (mt.getName().equalsIgnoreCase("get"+s)) { try { System.out.println(mt.getName()); values.add(mt.invoke(t, null)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } try { //最后添加ID values.add(c.getMethod("getId", null).invoke(t, null)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } i=executeUpdate(sb.toString(), values); return i; } 
   
  
 
网友评论