afunms

          My Software,My Dream—Forge a more perfect NMS product.

          new framework(6)--extract data from ResultSet via reflect

          今天試了一下用反射從ResultSet 提取數據,然后調用相應的dto的方法。
          這樣就不要每次都針對一個新表來寫一次extractData方法了,挺爽的。
          缺點就是數據表中的字段與dto的方法必須一一對應。

             /**
              * extract data from ResultSet to dto
              
          */

             
          protected DtoInterface extractData(ResultSet rs) throws Exception
             
          {
                 
          if(dtoClass == null

                    
          throw new NullPointerException("dtoClass is not setted!"
          ); 
                 
                 DtoInterface dto 
          =
           dtoClass.newInstance();
                 ResultSetMetaData rsm 
          =
           rs.getMetaData(); 
                 
          for(int i=1;i<=rsm.getColumnCount();i++

                 

                     String methodName 
          = "set" + rsm.getColumnName(i).replaceAll("_"""
          ); 
                     SysLogger.debug(
          "[" + rsm.getColumnName(i) + "]=" + rsm.getColumnType(i) + ",method=" +
           methodName);
                     Method method 
          =
           lookupMethod(dtoClass.getMethods(), methodName);
                     
          if(method==null

                     

                         SysLogger.error(
          "set" + rsm.getColumnName(i) + " does not exist"
          ); 
                         
          continue
          ;
                     }
           
                     
          if(rsm.getColumnType(i)==
          Types.INTEGER) 
                        method.invoke(dto,rs.getInt(i)); 
                     
          else if(rsm.getColumnType(i)==
          Types.VARCHAR) 
                        method.invoke(dto,rs.getString(i)); 
                 }
           
                 
          return
           dto; 
             }

             
             
          protected Method lookupMethod(Method[] methods,String methodName) 
             

                 Method result 
          = null

                 
          for
          (Method method:methods) 
                 

                     
          if
          (method.getName().equalsIgnoreCase(methodName)) 
                     

                         result 
          =
           method; 
                         
          break

                     }
           
                 }

                 
          return result; 
              }
           

          posted on 2007-05-06 22:33 afunms 閱讀(127) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 太仆寺旗| 拜泉县| 和田县| 东台市| 宿迁市| 芷江| 宁强县| 名山县| 玛曲县| 浦北县| 奉化市| 萍乡市| 华容县| 来凤县| 梁山县| 什邡市| 称多县| 宜兰县| 哈密市| 调兵山市| 资阳市| 日照市| 沁源县| 柞水县| 饶平县| 白城市| 墨玉县| 姜堰市| 达尔| 枞阳县| 塔城市| 彭泽县| 澄迈县| 辽宁省| 湟中县| 卓资县| 梅河口市| 阜南县| 湄潭县| 康定县| 宕昌县|