如何将UUID与JavaDB/Derby和JDBC一起使用? [英] How should I use UUID with JavaDB/Derby and JDBC?

查看:126
本文介绍了如何将UUID与JavaDB/Derby和JDBC一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用INT作为JavaDB(Apache Derby)中主键的类型,但是由于我正在实现分布式系统,因此我想将类型更改为

I currently use INT as type for primary key in JavaDB (Apache Derby), but since I'm implementing an distributed system I would like to change the type to java.util.UUID. A few questions about this:

  • 我应该将JavaDB/Derby中的哪种数据类型用于UUID?我已经看到有人提到CHAR(16) FOR BIT DATA,但是对此我不太了解. VARCHAR(16)是否可以替代?

  • What datatype in JavaDB/Derby should I use for UUID? I have seen CHAR(16) FOR BIT DATA been mentioned but I don't know much about it. Is VARCHAR(16) an alternative?

如何在JDBC中使用它?例如.在PreparedStatement中,我应该如何设置和获取UUID?

How should I use it with JDBC? E.g. in an PreparedStatement, how should I set and get an UUID?

如果以后我喜欢将数据库更改为SQL Server,是否有与java.util.UUID兼容的数据类型?

If I later would likte to change database to SQL Server, is there a compatible datatype to java.util.UUID?

简而言之,如何将UUID与JavaDB/Derby和JDBC一起使用?

Simply, How should I use UUID with JavaDB/Derby and JDBC?

推荐答案

UUID是一个128位值. CHAR(16) FOR BIT DATA类型反映了它是为简明起见以字符形式存储的位数据.我不认为VARCHAR(16)会起作用,因为它没有位标志.数据库将必须能够将二进制数据转换为处理编码且有风险的字符数据.更重要的是,它不会买任何东西.由于UUID总是 128位,因此使用VARCHAR而不是CHAR不会节省空间.因此,您最好使用预期的CHAR(16) FOR BIT DATA.

UUID is a 128 bit value. The CHAR(16) FOR BIT DATA type reflects that it is bit data stored in character form for conciseness. I don't think VARCHAR(16) would work because it doesn't have the bit flag. The database would have to be able to convert the binary data to character data which deals with encoding and is risky. More importantly, it wouldn't buy you anything. Since a UUID is always 128 bits, you don't get the space savings from using VARCHAR over CHAR. So you might as well use the intended CHAR(16) FOR BIT DATA.

对于JDBC,我认为您使用get/setBytes()方法,因为它处理的是少量二进制数据. (不是肯定的,必须尝试一下)

With JDBC, I think you use the get/setBytes() method since it is dealing with small amounts of binary data. (Not positive, would have to try this)

对SQL Server部分一无所知.

And no idea about the SQL Server part.

这篇关于如何将UUID与JavaDB/Derby和JDBC一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆