導航:首頁 > 編程語言 > java生成guid

java生成guid

發布時間:2022-07-18 13:17:21

java uuid 和guid 的區別

UUID是一個由4個連字型大小(-)將32個位元組長的字元串分隔後生成的字元串,總共36個位元組長。比如:550e8400-e29b-41d4-a716-446655440000
http://gohands.blogbus.com/logs/147479174.html
GUID 是微軟對UUID這個標準的實現。UUID是由開放軟體基金會(OSF)定義的。UUID還有其它各種實現,不止GUID一種。比如我們這里在Java中用到的。

② 怎樣用java生成GUID與UUID

GUID是一個128位長的數字,一般用16進製表示。演算法的核心思想是結合機器的網卡、當地時間、一個隨即數來生成GUID

③ 如何自動生成Guid碼

在操作過程中,為了測試往往要插入一些Guid碼,可以利用表格生成Guid碼,操作過程如下:新建一個表格,兩個欄位即可,第一個欄位是int
或者其它均可,第二個類型是uniqueidentifier,允許為空,關鍵是修改下面column
properties中的RowGuid,將No改為Yes,在表中添加第一個欄位,Guid碼在第二個欄位中就自動生成了。

④ 怎樣用java生成GUID與UUID

兩種方式生成guid與uuid
需要commlog庫
/**
*@authorAdministrator
*
*
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
importjava.net.InetAddress;
importjava.net.UnknownHostException;
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
importjava.security.SecureRandom;
importjava.util.Random;
{
protectedfinalorg.apache.commons.logging.Loglogger=org.apache.commons.logging.LogFactory
.getLog(getClass());
publicStringvalueBeforeMD5="";
publicStringvalueAfterMD5="";
privatestaticRandommyRand;
;
privatestaticStrings_id;
privatestaticfinalintPAD_BELOW=0x10;
privatestaticfinalintTWO_BYTES=0xFF;
/*
*.
*.Youmight
*
*itwitha"timesincefirstloaded"seedtorecethistime.
*.
*/
static{
mySecureRand=newSecureRandom();
longsecureInitializer=mySecureRand.nextLong();
myRand=newRandom(secureInitializer);
try{
s_id=InetAddress.getLocalHost().toString();
}catch(UnknownHostExceptione){
e.printStackTrace();
}
}

/*
*Defaultconstructor.,
*,highperformance.
*/
publicRandomGUID(){
getRandomGUID(false);
}
/*
*Constructorwithsecurityoption.Settingsecuretrue
*
*strong.
*.
*/
publicRandomGUID(booleansecure){
getRandomGUID(secure);
}
/*
*MethodtogeneratetherandomGUID
*/
privatevoidgetRandomGUID(booleansecure){
MessageDigestmd5=null;
StringBuffersbValueBeforeMD5=newStringBuffer(128);
try{
md5=MessageDigest.getInstance("MD5");
}catch(NoSuchAlgorithmExceptione){
logger.error("Error:"+e);
}
try{
longtime=System.currentTimeMillis();
longrand=0;
if(secure){
rand=mySecureRand.nextLong();
}else{
rand=myRand.nextLong();
}
sbValueBeforeMD5.append(s_id);
sbValueBeforeMD5.append(":");
sbValueBeforeMD5.append(Long.toString(time));
sbValueBeforeMD5.append(":");
sbValueBeforeMD5.append(Long.toString(rand));
valueBeforeMD5=sbValueBeforeMD5.toString();
md5.update(valueBeforeMD5.getBytes());
byte[]array=md5.digest();
StringBuffersb=newStringBuffer(32);
for(intj=0;j<array.length;++j){
intb=array[j]&TWO_BYTES;
if(b<PAD_BELOW)
sb.append('0');
sb.append(Integer.toHexString(b));
}
valueAfterMD5=sb.toString();
}catch(Exceptione){
logger.error("Error:"+e);
}
}
/*
*
*(,etc.)
*Example:C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
*/
publicStringtoString(){
Stringraw=valueAfterMD5.toUpperCase();
StringBuffersb=newStringBuffer(64);
sb.append(raw.substring(0,8));
sb.append("-");
sb.append(raw.substring(8,12));
sb.append("-");
sb.append(raw.substring(12,16));
sb.append("-");
sb.append(raw.substring(16,20));
sb.append("-");
sb.append(raw.substring(20));
returnsb.toString();
}

//
publicstaticvoidmain(Stringargs[]){
for(inti=0;i<100;i++){
RandomGUIDmyGUID=newRandomGUID();
System.out.println("SeedingString="+myGUID.valueBeforeMD5);
System.out.println("rawGUID="+myGUID.valueAfterMD5);
System.out.println("RandomGUID="+myGUID.toString());
}
}

}
同樣
UUIDuuid=UUID.randomUUID();
System.out.println("{"+uuid.toString()+"}");
UUID是指在一台機器上生成的數字,它保證對在同一時空中的所有機器都是唯一的。通常平台會提供生成UUID的API。UUID按照開放軟體基金會(OSF)制定的標准計算,用到了乙太網卡地址、納秒級時間、晶元ID碼和許多可能的數字。由以下幾部分的組合:當前日期和時間(UUID的第一個部分與時間有關,如果你在生成一個UUID之後,過幾秒又生成一個UUID,則第一個部分不同,其餘相同),時鍾序列,全局唯一的IEEE機器識別號(如果有網卡,從網卡獲得,沒有網卡以其他方式獲得),UUID的唯一缺陷在於生成的結果串會比較長。關於UUID這個標准使用最普遍的是微軟的GUID(GlobalsUniqueIdentifiers)。

⑤ java裡面怎麼樣使用GUID

使用類 java.util.UUID,要求jdk1.5,示例如下:

//生成一個隨機的GUID
java.util.UUID uuid=java.util.UUID.randomUUID();

//將UUID對象轉換為字元串
String output=uuid.toString();

⑥ 什麼是UUID,Java中怎麼產生UUID

UUID 是 通用唯一識別碼(Universally Unique Identifier)的縮寫,是一種軟體建構的標准,亦為開放軟體基金會組織在分布式計算環境領域的一部分。其目的,是讓分布式系統中的所有元素,都能有唯一的辨識信息,而不需要通過中央控制端來做辨識信息的指定。

生成UUID的方法:

public static String getUUID32(){ String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase(); return uuid;// return UUID.randomUUID().toString().replace("-", "").toLowerCase();}

註:因為一般資料庫主鍵為String類型,所以接收類型為String,生成的uuid數據包含-,所以要去掉-,故UUID.randomUUID().toString().replace("-", "").toLowerCase()。

(6)java生成guid擴展閱讀

UUID由以下幾部分的組合:

(1)當前日期和時間,UUID的第一個部分與時間有關,如果你在生成一個UUID之後,過幾秒又生成一個UUID,則第一個部分不同,其餘相同。

(2)時鍾序列。

(3)全局唯一的IEEE機器識別號,如果有網卡,從網卡MAC地址獲得,沒有網卡以其他方式獲得。

UUID的唯一缺陷在於生成的結果串會比較長。關於UUID這個標准使用最普遍的是微軟的GUID(Globals Unique Identifiers)。在ColdFusion中可以用CreateUUID()函數很簡單地生成UUID,其格式為:xxxxxxxx-xxxx- xxxx-xxxxxxxxxxxxxxxx(8-4-4-16)。

其中每個 x 是 0-9 或 a-f 范圍內的一個十六進制的數字。而標準的UUID格式為:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12),可以從cflib 下載CreateGUID() UDF進行轉換。

⑦ 哪位高手能提供個產生GUID的JAVA類

GUID是一個128位長的數字,一般用16進製表示。演算法的核心思想是結合機器的網卡、當地時間、一個隨即數來生成GUID。從理論上講,如果一台機器每秒產生10000000個GUID,則可以保證(概率意義上)3240年不重復。

閱讀全文

與java生成guid相關的資料

熱點內容
app易語言post怎麼學 瀏覽:963
地梁的箍筋加密區位置 瀏覽:300
二分法排序程序及編譯結果 瀏覽:677
日語命令形和禁止型 瀏覽:283
安裝軟體用管理員解壓 瀏覽:503
編譯原理代碼塊 瀏覽:398
小孩可以用壓縮面膜嗎 瀏覽:12
錐形倒角怎麼計演算法 瀏覽:880
java合並鏈表 瀏覽:505
pic單片機編譯器 瀏覽:803
麗水四軸加工中心編程 瀏覽:689
國產系統怎麼解壓 瀏覽:552
戰雙程序員 瀏覽:483
him觸摸編程軟體 瀏覽:931
植物大戰僵屍存檔怎麼轉移安卓 瀏覽:852
java棧的元素 瀏覽:737
程序員與籃球事件 瀏覽:676
app反編譯不完整 瀏覽:788
電腦上的文件夾怎麼調整 瀏覽:8
伺服器無響應是什麼原因呀 瀏覽:985