用Java實現自動在數據庫表中生成ID號(改進算法)[原創]
作者:Flyingis
在前面的一篇日志里面,我設計了在數據庫表中自動生成ID號的一種算法(點擊這里查看),這個算法主要應用于字典表的修改中。字典表的ID號是這樣設計的:A01、A05、A28等等,即一位字母+兩位數字。由于每個字典表的ID號的第一個字母對于一個字典表來說是固定的,這樣做的目的在于在其它表中查看數據的時候可以很容易分辨字典項的ID號是屬于哪一個數據字典的,因此這就限制了每個字典表的數據不能超過99條,當數據量大于99條的時候,那個算法就不再適用。
因此這里給出了一個改進的算法(其實只作了一點點改進),可以滿足數據在1~999條之間的數據字典,這對于絕大多數應用來說已經是綽綽有余了。下面就給出具體的方法:
/*
* 功能:增加字典信息時,自動生成最小的ID號碼
* 參數:String 字典表名稱 first 字典ID的首字母,代表唯一的字典
* 返回:String 生成的最小ID號碼
*/
public String getId(String table, String first) {
// 所有除去首字母后的ID號碼--整型,例如:11
int[] sid;
// 所有原始ID號碼,例如:A011
String[] rid;
// 除去首字母后最小的ID號碼--字符串
String sid_new = null;
// 程序返回的最小的原始ID號碼
String rid_new = null;
// 循環參數
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);
// 如果不存在結果集,則直接在first字母后面加001,例如first="A",rid_new=A001
if (!rst.first()) {
rid_new = first.concat("001";
return rid_new;
}
// 如果存在結果集,則將表中所有ID號存入數組中,并轉換為整型數據
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號不為fisrt+001,例如A003、A018、A109等,則返回新增數據的ID號為A001
if (sid[0] != 1) {
rid_new = first.concat("001";
return rid_new;
}
// 如果第一條記錄ID號為first+001,即A001,則執行下面語句
else {
// 如果總記錄數只有一條,例如A001,則返回新增數據為A002
if (i == 1) {
rid_new = first.concat("002";
return rid_new;
}
else {
for (int j = 1; j < k; j++) {
// 如果相鄰兩條記錄ID號的整數位相差1,則保存新增數據ID號整數位是前一位ID號整數位加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號的整數位相差非1,則返回新增數據ID號整數位是前一位ID號整數位加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,是因為在SQLServer2000中根據ID號正確排序的需要,如果按照升序排列,A1后面是A10、A11等,而不是A2。另外,在Hibernate中有多種自動生成ID字段的方法,但是這個項目比較小,我沒有使用Hibernate中間件,這里提供的只是生成字典ID字段的一種簡單思路,相比原有算法改進了一點,可以適用于字典項不多于1000項的情況,一般情況下,字典項是不可能超過1000項了,在我參與的這個小項目中已經是完全夠用了。還有什么更好的方法和思路還請大家多指教!
在前面的一篇日志里面,我設計了在數據庫表中自動生成ID號的一種算法(點擊這里查看),這個算法主要應用于字典表的修改中。字典表的ID號是這樣設計的:A01、A05、A28等等,即一位字母+兩位數字。由于每個字典表的ID號的第一個字母對于一個字典表來說是固定的,這樣做的目的在于在其它表中查看數據的時候可以很容易分辨字典項的ID號是屬于哪一個數據字典的,因此這就限制了每個字典表的數據不能超過99條,當數據量大于99條的時候,那個算法就不再適用。
因此這里給出了一個改進的算法(其實只作了一點點改進),可以滿足數據在1~999條之間的數據字典,這對于絕大多數應用來說已經是綽綽有余了。下面就給出具體的方法:
/*
* 功能:增加字典信息時,自動生成最小的ID號碼
* 參數:String 字典表名稱 first 字典ID的首字母,代表唯一的字典
* 返回:String 生成的最小ID號碼
*/
public String getId(String table, String first) {
// 所有除去首字母后的ID號碼--整型,例如:11
int[] sid;
// 所有原始ID號碼,例如:A011
String[] rid;
// 除去首字母后最小的ID號碼--字符串
String sid_new = null;
// 程序返回的最小的原始ID號碼
String rid_new = null;
// 循環參數
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);
// 如果不存在結果集,則直接在first字母后面加001,例如first="A",rid_new=A001
if (!rst.first()) {
rid_new = first.concat("001";
return rid_new;
}
// 如果存在結果集,則將表中所有ID號存入數組中,并轉換為整型數據
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號不為fisrt+001,例如A003、A018、A109等,則返回新增數據的ID號為A001
if (sid[0] != 1) {
rid_new = first.concat("001";
return rid_new;
}
// 如果第一條記錄ID號為first+001,即A001,則執行下面語句
else {
// 如果總記錄數只有一條,例如A001,則返回新增數據為A002
if (i == 1) {
rid_new = first.concat("002";
return rid_new;
}
else {
for (int j = 1; j < k; j++) {
// 如果相鄰兩條記錄ID號的整數位相差1,則保存新增數據ID號整數位是前一位ID號整數位加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號的整數位相差非1,則返回新增數據ID號整數位是前一位ID號整數位加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,是因為在SQLServer2000中根據ID號正確排序的需要,如果按照升序排列,A1后面是A10、A11等,而不是A2。另外,在Hibernate中有多種自動生成ID字段的方法,但是這個項目比較小,我沒有使用Hibernate中間件,這里提供的只是生成字典ID字段的一種簡單思路,相比原有算法改進了一點,可以適用于字典項不多于1000項的情況,一般情況下,字典項是不可能超過1000項了,在我參與的這個小項目中已經是完全夠用了。還有什么更好的方法和思路還請大家多指教!
posted on 2005-11-06 17:04 Flyingis 閱讀(2142) 評論(0) 編輯 收藏 所屬分類: JavaSE