當柳上原的風吹向天際的時候...

          真正的快樂來源于創造

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          編碼調試過程中,常有Sql語句的調試任務,這種任務比較麻煩的一點在于需要手工將?替換成參數,如果參數有十來個就夠讓人頭疼的.
          為了減輕這種無謂的勞動,本人設計了一個類來代替完成這種累而又容易讓人出錯的活.
          下面是代碼:
          package com.heyang;

          import java.text.MessageFormat;
          import java.util.regex.Matcher;
          import java.util.regex.Pattern;

          /**
           * 將SQL語句中的問號替換成參數
           * 此類用于調試SQL時減輕手工加入參數的勞動量
           * 
          @author heyang@gmail.com
           *
           
          */
          public class SqlCompletion{
              
          // 輸入的SQL語句
              private String sql;
              
              
          // 輸入的參數
              private String[] arr;
              
              
          // 構造函數
              public SqlCompletion(String sql,Object[] params){
                  
          this.sql=sql;
                  
                  arr
          =new String[params.length];
                  
                  
          for(int i=0;i<arr.length;i++){
                      arr[i]
          ="'"+params[i].toString()+"'";
                  }
              }
              
              
          /**
               * 取得將問號替換成參數的補全SQL
               * 
          @return
               
          */
              
          public String getCompletedSql(){
                  Pattern p 
          = Pattern.compile("\\?",Pattern.CASE_INSENSITIVE);

                  Matcher m 
          = p.matcher(sql);
                  StringBuffer sb 
          = new StringBuffer();

                  
          boolean result = m.find();
                  
          int index=0;

                  
          while (result) {
                      m.appendReplacement(sb, 
          "{"+index+"}");
                      index
          ++;
                      result 
          = m.find();
                  }
                  m.appendTail(sb);
                  String repSql
          =sb.toString();

                  
                  
          return MessageFormat.format(repSql,arr);
              }
              
              
          // 測試
              public static void main(String[] args){
                  Object[] params
          ={"c1","c2","c3","c4"};
                  SqlCompletion s
          =new SqlCompletion("select * from t where t.f1=? and t.f2=? and t.f3=? and t.f4=? ",params);
                  System.out.println(s.getCompletedSql());        
              }
          }

          輸出結果是:
          select * from t where t.f1='c1' and t.f2='c2' and t.f3='c3' and t.f4='c4'

          你可以把此類拷貝到你的項目中,只要標明我是原作者就行.
          posted on 2012-12-17 22:55 何楊 閱讀(558) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 毕节市| 林口县| 梁山县| 枣强县| 陆河县| 驻马店市| 林口县| 手机| 周至县| 兴城市| 抚州市| 北碚区| 洪江市| 鹰潭市| 鹿邑县| 容城县| 河西区| 克什克腾旗| 通山县| 自贡市| 青川县| 甘肃省| 天镇县| 绥江县| 昭通市| 汨罗市| 莱州市| 灌阳县| 利津县| 论坛| 龙南县| 杭锦旗| 南昌市| 健康| 湖口县| 武陟县| 文化| 吉首市| 冕宁县| 汉寿县| 襄垣县|