加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

如何用java反射技术将sql操作与面向对象编程关联起来

发布时间:2023-04-21 15:28:06 所属栏目:教程 来源:
导读:实例代码:

public class sqlUtil extends BaseApplogic {
public List excuteQuery(String sql, Object[] paras, Object voo)
throws AppException {
DBPersistenceManager dbpm = this
实例代码:

public class sqlUtil extends BaseApplogic {
   public List excuteQuery(String sql, Object[] paras, Object voo)
           throws AppException {
       DBPersistenceManager dbpm = this.getFnmsDBPM();
       List list=new ArrayList();
       try {
           DataSet ds = (DataSet) dbpm.executeQuery(sql, paras);
           
           DataSetMetaData dsmd = ds.getDataSetMetaData();
           
           Field[] fd = voo.getClass().getDeclaredFields();
           String className = voo.getClass().getName();
           int size = fd.length;
           Method md[]=new Method[size];
           //构造method[]
           for (int i = 0; i < size; i++) {
               Attribute attr=dsmd.getAttribute(fd[i].getName().toupperCase());
               if (null != attr) {
                   Field f = voo.getClass().getDeclaredField(fd[i].getName());
                   String type = f.getType().getName();
                   Class[] types=getTypes(type);  
                   String methodName=getSetterName(fd[i].getName());
                   md[i] = voo.getClass().getmethod(
                           methodName,types);
               }
           }
           
           while(ds.next()){
               Object o = Class.forName(className).newInstance();
               for (int i = 0; i < size; i++) {
                   if(null!=md[i]){
                       //调用
                       Attribute attr=dsmd.getAttribute(fd[i].getName().toupperCase());
                       if (null==attr) continue;
                       Object[] pa=new Object[]{ds.getString(attr.getAttrName())};
                       md[i].invoke(o,pa);
                   }
               }
               list.add(o);
           }
       } catch (DrmException drme) {
           this.handleException(drme);
       } catch (Exception e) {
           this.handleException(e);// 新增加的异常处理
       } finally {
           if (dbpm != null) {
               dbpm.close();
           }
       }
       return list;

   }

   //由属性调用set方法
   public static String getSetterName(String propName) {
       return "set" + propName.substring(0, 1).toupperCase()
               + propName.substring(1, propName.length());

   }

   // 取类型
   public static Class[] getTypes(String type) {
       if (type.equals("java.lang.String")) {
           return new Class[] { String.class };
       } else if (type.equals("int")) {
           return new Class[] { Integer.TYPE };
       } else if (type.equals("long")) {
           return new Class[] { Long.TYPE };
       } else if (type.equals("float")) {
           return new Class[] { Float.TYPE };
       } else {
           System.out.println("no such type!");
           return null;
       }

   }
}

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章