Home >Java >javaTutorial >How to use Java reflection technology to connect object-oriented programming and SQL operations?

How to use Java reflection technology to connect object-oriented programming and SQL operations?

PHPz
PHPzforward
2023-04-23 14:19:08982browse

Example code:

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; is the parameter array, and the third one is the object type to be returned.

The return value is a list, and each object in the list is the object type you passed in.

After such a packaging, the sql and the object are naturally encapsulated. There is no need to find out each query, then resultset.next(), then getString(), and then setXxx();

Of course, this is just the tip of the iceberg in the utilization of metadata and java object reflection technology.

The above is the detailed content of How to use Java reflection technology to connect object-oriented programming and SQL operations?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete