用Java實現(xiàn)自動在數(shù)據(jù)庫表中生成ID號[原創(chuàng)]
作者:Flyingis
前段時間用Struts開發(fā)了一個B/S結(jié)構(gòu)的信息管理系統(tǒng),其中有一個功能是要求管理員能夠?qū)?shù)據(jù)字典進行修改,數(shù)據(jù)字典的表結(jié)構(gòu)基本上都是table(id, name),id為數(shù)據(jù)庫其它表中所存儲的內(nèi)容,表示方式為A01、A02、A08、B10、B25、C12等等,一個字典就分配一個字母作為其ID號的標(biāo)識,其實就是為了調(diào)試時方便,在其它的表中判斷該字典的名稱。因此對于一個特定的字典表來說,其ID號排序應(yīng)該是A01、A02、A03、A04……
在對字典內(nèi)容進行刪除的時候并不需要考慮什么,直接使用DELETE語句就可以了。關(guān)鍵是添加字典信息時,管理員需要在表單中填寫的是table中的name字段,ID號如何生成就需要自己用代碼來實現(xiàn)(包括ID號的01號空缺,中間有斷開等情況)。下面是我設(shè)計的代碼,其中關(guān)鍵的地方都有詳細的注釋:
/*
* 功能:增加字典信息時,自動生成最小的ID號碼
* 參數(shù):String 字典表名稱 first 字典ID的首字母,代表唯一的字典
* 返回:String 生成的最小ID號碼
*/
public String getId(String table, String first) {
// 所有除去首字母后的ID號碼--整型,例如:11
int[] sid;
// 所有原始ID號碼,例如:A11
String[] rid;
// 除去首字母后最小的ID號碼--字符串
String sid_new = null;
// 程序返回的最小的原始ID號碼
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字母后面加01,例如first="A",rid_new=A01
if (!rst.first()) {
rid_new = first.concat("01");
return rid_new;
}
// 如果存在結(jié)果集,則將表中所有ID號存入數(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號不為fisrt+01,例如A03、A05、A18等,則返回新增數(shù)據(jù)的ID號為A01
if (sid[0] != 1) {
rid_new = first.concat("01");
return rid_new;
}
// 如果第一條記錄ID號為first+1,即A1,則執(zhí)行下面語句
else {
// 如果總記錄數(shù)只有一條,例如A1,則返回新增數(shù)據(jù)為A02
if (i == 1) {
rid_new = first.concat("02");
return rid_new;
}
else {
for (int j = 1; j < k; j++) {
// 如果相鄰兩條記錄ID號的整數(shù)位相差1,則保存新增數(shù)據(jù)ID號整數(shù)位是前一位ID號整數(shù)位加1
if (sid[j] == sid[j-1] + 1) {
if (sid[j] < 9) {
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號的整數(shù)位相差非1,則返回新增數(shù)據(jù)ID號整數(shù)位是前一位ID號整數(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("0").concat(sid_new);
return rid_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中根據(jù)ID號正確排序的需要,如果按照升序排列,A1后面是A10、A11等,而不是A2。另外,在Hibernate中有多種自動生成ID字段的方法,但是這個項目比較小,我沒有使用Hibernate中間件,這里提供的只是生成字典ID字段的一種簡單思路,只能用于字典項不多于100項的情況,一般的情況可以滿足了,但如果超過100項只需簡單修改一下代碼,不足之處還請大家多指教!
前段時間用Struts開發(fā)了一個B/S結(jié)構(gòu)的信息管理系統(tǒng),其中有一個功能是要求管理員能夠?qū)?shù)據(jù)字典進行修改,數(shù)據(jù)字典的表結(jié)構(gòu)基本上都是table(id, name),id為數(shù)據(jù)庫其它表中所存儲的內(nèi)容,表示方式為A01、A02、A08、B10、B25、C12等等,一個字典就分配一個字母作為其ID號的標(biāo)識,其實就是為了調(diào)試時方便,在其它的表中判斷該字典的名稱。因此對于一個特定的字典表來說,其ID號排序應(yīng)該是A01、A02、A03、A04……
在對字典內(nèi)容進行刪除的時候并不需要考慮什么,直接使用DELETE語句就可以了。關(guān)鍵是添加字典信息時,管理員需要在表單中填寫的是table中的name字段,ID號如何生成就需要自己用代碼來實現(xiàn)(包括ID號的01號空缺,中間有斷開等情況)。下面是我設(shè)計的代碼,其中關(guān)鍵的地方都有詳細的注釋:
/*
* 功能:增加字典信息時,自動生成最小的ID號碼
* 參數(shù):String 字典表名稱 first 字典ID的首字母,代表唯一的字典
* 返回:String 生成的最小ID號碼
*/
public String getId(String table, String first) {
// 所有除去首字母后的ID號碼--整型,例如:11
int[] sid;
// 所有原始ID號碼,例如:A11
String[] rid;
// 除去首字母后最小的ID號碼--字符串
String sid_new = null;
// 程序返回的最小的原始ID號碼
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字母后面加01,例如first="A",rid_new=A01
if (!rst.first()) {
rid_new = first.concat("01");
return rid_new;
}
// 如果存在結(jié)果集,則將表中所有ID號存入數(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號不為fisrt+01,例如A03、A05、A18等,則返回新增數(shù)據(jù)的ID號為A01
if (sid[0] != 1) {
rid_new = first.concat("01");
return rid_new;
}
// 如果第一條記錄ID號為first+1,即A1,則執(zhí)行下面語句
else {
// 如果總記錄數(shù)只有一條,例如A1,則返回新增數(shù)據(jù)為A02
if (i == 1) {
rid_new = first.concat("02");
return rid_new;
}
else {
for (int j = 1; j < k; j++) {
// 如果相鄰兩條記錄ID號的整數(shù)位相差1,則保存新增數(shù)據(jù)ID號整數(shù)位是前一位ID號整數(shù)位加1
if (sid[j] == sid[j-1] + 1) {
if (sid[j] < 9) {
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號的整數(shù)位相差非1,則返回新增數(shù)據(jù)ID號整數(shù)位是前一位ID號整數(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("0").concat(sid_new);
return rid_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中根據(jù)ID號正確排序的需要,如果按照升序排列,A1后面是A10、A11等,而不是A2。另外,在Hibernate中有多種自動生成ID字段的方法,但是這個項目比較小,我沒有使用Hibernate中間件,這里提供的只是生成字典ID字段的一種簡單思路,只能用于字典項不多于100項的情況,一般的情況可以滿足了,但如果超過100項只需簡單修改一下代碼,不足之處還請大家多指教!
posted on 2005-10-31 11:37 Flyingis 閱讀(1079) 評論(1) 編輯 收藏 所屬分類: JavaSE