java 调用mysql uuid,使用Java读取MySQL二进制(16)UUID

This should be a very simple question, I'm just missing something basic here and I'm having 'one of those days...'

Cannot use Hibernate or other ORM. Using Java PreparedStatement.

MySQL stuff:

CREATE TABLE `article` (

`articleID` binary(16) NOT NULL,

`publisherID` bigint(20) DEFAULT NULL,

PRIMARY KEY (`articleID`),

) ENGINE=InnoDB DEFAULT CHARSET=utf8$$

insert into article ( articleID, publisherID )

values ( (UNHEX(REPLACE(UUID(),'-',''))), 1111 );

Java stuff

PreparedStatement ps = connection.prepareStatement( "select articleID, publisherID from article" );

ResultSet rs = ps.executeQuery();

while( rs.next())

{

byte[] artIDArr = rs.getBytes( "articleID" );

UUID artID = UUID.nameUUIDFromBytes( artIDArr );

}

rs.close();

ps.close();

Now, reading the UUIDs from the database...

select hex(articleID) from article;

1C711C50E4773873AB1533401E2F420C

A1FCD341EE9311E297B700FFB00BB509

A95E06B6EEE611E297B700FFB00BB509

But dumping out what I read in the java code:

6c825dc9-c98f-37ab-b01b-416294811a84

de6337f9-f276-3e30-b9a3-8d9338a1977f

57ccb5af-1a66-329f-b069-69638e1af24f

Now, is this because I'm removing the dashes from the UUID before storing them as binary, and the rehydration is assuming they're there?

What is the correct method for reading a UUID stored as binary(16) in MySql to a Jav UUID object?

Edit: if I change the the preparedStatment query to "select hex(articleID) as articleID..." and read it as a string, it's of course what the DB contains, but UUID throws an exception because the string is missing the dashes...

解决方案UUID artID = UUID.nameUUIDFromBytes(artIDArr);

Uses MD5 and patches bytes. Use something like

static UUID toUUID(byte[] bytes) {

if (bytes.length != 16) {

throw new IllegalArgumentException();

}

int i = 0;

long msl = 0;

for (; i < 8; i++) {

msl = (msl << 8) | (bytes[i] & 0xFF);

}

long lsl = 0;

for (; i < 16; i++) {

lsl = (lsl << 8) | (bytes[i] & 0xFF);

}

return new UUID(msl, lsl);

}

UUID artID = toUUID(artIDArr);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值