java的UUID型別欄位,如何通過jdbc進行資料庫的CRUD
阿新 • • 發佈:2019-01-29
關鍵字:UUID byte[] jdbc mysql java
1、UUID/GUID概念
UUID含義是通用唯一識別碼 (Universally Unique Identifier),這 是一個軟體建構的標準,也是被開源軟體基金會 (Open Software Foundation, OSF) 的組織應用在分散式計算環境 (Distributed Computing Environment, DCE) 領域的一部份。
A UUID is a 16-byte (128-bit) number. In its canonical form, a UUID is represented by 32 hexadecimal digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 digits and four hyphens). For example:
550e8400-e29b-41d4-a716-446655440000
詳細介紹請參考http://en.wikipedia.org/wiki/Universally_Unique_Identifier。
2、java中的類java.Util.UUID
jdk1.5增加了類java.Util.UUID,用於方便生成UUID。
UUID uuid=UUID.randomUUID();
String uuidStr=uuid.toString();//生成的如:9b17a4f1-cae4-42ce-9cba-b899dcac8517
UUID類中還有個方法也常用:UUID.fromString(name)
Creates a UUID from the string standard representation as described in the toString method.
3、資料庫中UUID的儲存型別
常用的儲存方式兩種,以mySql資料庫為例(關於oracle資料庫,測試後再貼)
字串方式:char(36)
位元組方式(二進位制):binaray(36)
建立表結構:
create table guid(id binary(36),uuid char(36));
4、jdbc如何操作
@Test
public void guid(){
UUID uuid=UUID.randomUUID();
String sqlSelect="select id,uuid from guid";
String sqlInsert="insert into guid(id,uuid) values(?,?)";
String sqlDelete="delete from guid where id=?";
try{
JDBConnection conn=new JDBConnection();
try{
//insert
PreparedStatement ps=conn.getConect().prepareStatement(sqlInsert);
//id列,引數為byte[]或者String都可以
ps.setObject(1, uuid.toString().getBytes());
//uuid列
ps.setString(2, uuid.toString());
ps.executeUpdate();
//select
ResultSet result=conn.executeQuery(sqlSelect);
while(result.next()){
Object id=result.getObject(1);//獲取的byte[]
Object uid=result.getObject(2);
String ids=result.getString(1);
Assert.assertEquals(uid, uuid.toString());
Assert.assertEquals(ids,uuid.toString());
//byte[]轉換為UUID字串
Assert.assertEquals(new String((byte[])id),uuid.toString());
Assert.assertEquals(UUID.fromString(ids).toString(),uuid.toString());
}
//delete
ps=conn.getConect().prepareStatement(sqlDelete);
//id列,引數為byte[]或者String都可以
ps.setObject(1, uuid.toString().getBytes());
ps.executeUpdate();
}catch(Exception ex){
ex.printStackTrace();
}finally{
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
1、UUID/GUID概念
UUID含義是通用唯一識別碼 (Universally Unique Identifier),這 是一個軟體建構的標準,也是被開源軟體基金會 (Open Software Foundation, OSF) 的組織應用在分散式計算環境 (Distributed Computing Environment, DCE) 領域的一部份。
A UUID is a 16-byte (128-bit) number. In its canonical form, a UUID is represented by 32 hexadecimal digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 digits and four hyphens). For example:
550e8400-e29b-41d4-a716-446655440000
There are 340,282,366,920,938,463,463,374,607,431,768,211,456 possible UUIDs (16 to the 32nd power), or about 3 × 1038.
2、java中的類java.Util.UUID
jdk1.5增加了類java.Util.UUID,用於方便生成UUID。
UUID uuid=UUID.randomUUID();
String uuidStr=uuid.toString();//生成的如:9b17a4f1-cae4-42ce-9cba-b899dcac8517
UUID類中還有個方法也常用:UUID.fromString(name)
Creates a UUID from the string standard representation as described in the toString method.
3、資料庫中UUID的儲存型別
常用的儲存方式兩種,以mySql資料庫為例(關於oracle資料庫,測試後再貼)
字串方式:char(36)
位元組方式(二進位制):binaray(36)
建立表結構:
create table guid(id binary(36),uuid char(36));
4、jdbc如何操作
@Test
public void guid(){
UUID uuid=UUID.randomUUID();
String sqlSelect="select id,uuid from guid";
String sqlInsert="insert into guid(id,uuid) values(?,?)";
String sqlDelete="delete from guid where id=?";
try{
JDBConnection conn=new JDBConnection();
try{
//insert
PreparedStatement ps=conn.getConect().prepareStatement(sqlInsert);
//id列,引數為byte[]或者String都可以
ps.setObject(1, uuid.toString().getBytes());
//uuid列
ps.setString(2, uuid.toString());
ps.executeUpdate();
//select
ResultSet result=conn.executeQuery(sqlSelect);
while(result.next()){
Object id=result.getObject(1);//獲取的byte[]
Object uid=result.getObject(2);
String ids=result.getString(1);
Assert.assertEquals(uid, uuid.toString());
Assert.assertEquals(ids,uuid.toString());
//byte[]轉換為UUID字串
Assert.assertEquals(new String((byte[])id),uuid.toString());
Assert.assertEquals(UUID.fromString(ids).toString(),uuid.toString());
}
//delete
ps=conn.getConect().prepareStatement(sqlDelete);
//id列,引數為byte[]或者String都可以
ps.setObject(1, uuid.toString().getBytes());
ps.executeUpdate();
}catch(Exception ex){
ex.printStackTrace();
}finally{
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}