Flyingis

          Talking and thinking freely !
          Flying in the world of GIS !
          隨筆 - 156, 文章 - 16, 評(píng)論 - 589, 引用 - 0
          數(shù)據(jù)加載中……

          用Java實(shí)現(xiàn)自動(dòng)在數(shù)據(jù)庫(kù)表中生成ID號(hào)(改進(jìn)算法)[原創(chuàng)]

              作者:Flyingis

              在前面的一篇日志里面,我設(shè)計(jì)了在數(shù)據(jù)庫(kù)表中自動(dòng)生成ID號(hào)的一種算法(
          點(diǎn)擊這里查看),這個(gè)算法主要應(yīng)用于字典表的修改中。字典表的ID號(hào)是這樣設(shè)計(jì)的:A01、A05、A28等等,即一位字母+兩位數(shù)字。由于每個(gè)字典表的ID號(hào)的第一個(gè)字母對(duì)于一個(gè)字典表來(lái)說(shuō)是固定的,這樣做的目的在于在其它表中查看數(shù)據(jù)的時(shí)候可以很容易分辨字典項(xiàng)的ID號(hào)是屬于哪一個(gè)數(shù)據(jù)字典的,因此這就限制了每個(gè)字典表的數(shù)據(jù)不能超過(guò)99條,當(dāng)數(shù)據(jù)量大于99條的時(shí)候,那個(gè)算法就不再適用。

              因此這里給出了一個(gè)改進(jìn)的算法(其實(shí)只作了一點(diǎn)點(diǎn)改進(jìn)),可以滿(mǎn)足數(shù)據(jù)在1~999條之間的數(shù)據(jù)字典,這對(duì)于絕大多數(shù)應(yīng)用來(lái)說(shuō)已經(jīng)是綽綽有余了。下面就給出具體的方法:

          /* 
           * 功能:增加字典信息時(shí),自動(dòng)生成最小的ID號(hào)碼
           * 參數(shù):String 字典表名稱(chēng) first 字典ID的首字母,代表唯一的字典
           * 返回:String 生成的最小ID號(hào)碼
           */
          public String getId(String table, String first) {

          // 所有除去首字母后的ID號(hào)碼--整型,例如:11
          int[] sid;
          // 所有原始ID號(hào)碼,例如:A011
          String[] rid;
          // 除去首字母后最小的ID號(hào)碼--字符串
          String sid_new = null;
          // 程序返回的最小的原始ID號(hào)碼
          String rid_new = null;

                  // 循環(huán)參數(shù)
          int i = 0;
          int k = 0;

          con = DatabaseConnection.getConnection("jdbc/wutie";
          Statement stm = null;
          ResultSet rst = null;
          RowSet rowRst = null;
          String sql = "SELECT * FROM " + table + " order by id";

          try {
              if (con.isClosed()) {
                  throw new IllegalStateException("error.sql.unexpected";
              }
              stm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
              rst = stm.executeQuery(sql);
              
              while (rst.next()) {
               k++;
               }
              sid = new int[k];
              rid = new String[k];
              rst = stm.executeQuery(sql);
              // 如果不存在結(jié)果集,則直接在first字母后面加001,例如first="A",rid_new=A001
              if (!rst.first()) {
               rid_new = first.concat("001";
               return rid_new;
               }
                      // 如果存在結(jié)果集,則將表中所有ID號(hào)存入數(shù)組中,并轉(zhuǎn)換為整型數(shù)據(jù)
              else {
               /*
              while (rst.next()) {
                  rid[i] = rst.getString("id";
                  sid[i] = Integer.parseInt(rid[i].substring(1));
                  i++;
                  }
                  */
               for (rst.previous(); rst.next(); i++) {
               rid[i] = rst.getString("id";
               sid[i] = Integer.parseInt(rid[i].substring(1));
               }
               // 如果第一條記錄ID號(hào)不為fisrt+001,例如A003、A018、A109等,則返回新增數(shù)據(jù)的ID號(hào)為A001
               if (sid[0] != 1) {
               rid_new = first.concat("001";
               return rid_new;
               }
               // 如果第一條記錄ID號(hào)為first+001,即A001,則執(zhí)行下面語(yǔ)句
              else {
               // 如果總記錄數(shù)只有一條,例如A001,則返回新增數(shù)據(jù)為A002
               if (i == 1) {
               rid_new = first.concat("002";
               return rid_new;
               }
               else {
              for (int j = 1; j < k; j++) {
               // 如果相鄰兩條記錄ID號(hào)的整數(shù)位相差1,則保存新增數(shù)據(jù)ID號(hào)整數(shù)位是前一位ID號(hào)整數(shù)位加1
               if (sid[j] == sid[j-1] + 1) {
               if (sid[j] < 9) {
                 sid_new = String.valueOf(sid[j] + 1);
                 rid_new = first.concat("00".concat(sid_new);
               }
                 else if (sid[j]>9 && sid[j]<100){
                 sid_new = String.valueOf(sid[j] + 1);
                 rid_new = first.concat("0").concat(sid_new);
               }
               else {
                 sid_new = String.valueOf(sid[j] + 1);
                 rid_new = first.concat(sid_new);
               }
               }
          // 如果相鄰兩條記錄ID號(hào)的整數(shù)位相差非1,則返回新增數(shù)據(jù)ID號(hào)整數(shù)位是前一位ID號(hào)整數(shù)位加1
          if (sid[j] != sid[j-1] + 1) {
          if (sid[j-1] < 9) {
              sid_new = String.valueOf(sid[j-1] + 1);
              rid_new = first.concat("00".concat(sid_new);
              return rid_new;
          }
          else if (sid[j]>9 && sid[j]<100){
              sid_new = String.valueOf(sid[j-1] + 1);
              rid_new = first.concat("0").concat(sid_new);
          }
          else {
              sid_new = String.valueOf(sid[j-1] + 1);
              rid_new = first.concat(sid_new);
              return rid_new;
          }
              }
          }
              return rid_new;
               }
               }
               }     
          }
          catch (SQLException e) {
          e.printStackTrace();
          throw new RuntimeException("error.sql.runtime";
          }
          finally {
          try {
          stm.close();
          con.close();
          }
          catch (SQLException e1) {
          e1.printStackTrace();
          throw new RuntimeException("error.sql.runtime";
          }
          }

          }

          注意:之所以生成A01而不是A1,是因?yàn)樵赟QLServer2000中根據(jù)ID號(hào)正確排序的需要,如果按照升序排列,A1后面是A10、A11等,而不是A2。另外,在Hibernate中有多種自動(dòng)生成ID字段的方法,但是這個(gè)項(xiàng)目比較小,我沒(méi)有使用Hibernate中間件,這里提供的只是生成字典ID字段的一種簡(jiǎn)單思路,相比原有算法改進(jìn)了一點(diǎn),可以適用于字典項(xiàng)不多于1000項(xiàng)的情況,一般情況下,字典項(xiàng)是不可能超過(guò)1000項(xiàng)了,在我參與的這個(gè)小項(xiàng)目中已經(jīng)是完全夠用了。還有什么更好的方法和思路還請(qǐng)大家多指教!

          posted on 2005-11-06 17:04 Flyingis 閱讀(2142) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): JavaSE

          主站蜘蛛池模板: 康乐县| 安仁县| 固镇县| 玉门市| 马边| 和龙市| 汽车| 龙海市| 乌鲁木齐市| 罗田县| 怀集县| 武冈市| 白水县| 都昌县| 五指山市| 辰溪县| 峡江县| 固阳县| 东至县| 忻州市| 天祝| 库车县| 石家庄市| 桦甸市| 五常市| 聂拉木县| 万山特区| 古田县| 盈江县| 沾益县| 社旗县| 且末县| 来安县| 福泉市| 伊吾县| 偏关县| 介休市| 瑞昌市| 海宁市| 泌阳县| 城口县|