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

          真正的快樂來源于創造

            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
          主站蜘蛛池模板: 五华县| 乌鲁木齐县| 凤城市| 祁门县| 龙里县| 福建省| 巴南区| 博爱县| 无锡市| 元氏县| 东方市| 长阳| 榆树市| 垫江县| 莎车县| 调兵山市| 顺昌县| 汝南县| 吉安市| 海南省| 葫芦岛市| 霍山县| 自治县| 资源县| 黄梅县| 海南省| 东宁县| 嘉义市| 丁青县| 清水县| 安丘市| 和顺县| 邛崃市| 邯郸市| 广安市| 斗六市| 崇文区| 渝北区| 玛多县| 宜良县| 缙云县|