使用 JDBC 从 Sybase 表中以字符串形式读取 GUID
在我的 Sybase 12.0 ASE 数据库中,我有一个表,其中包含定义为二进制 (16) 并用于存储 GUIDS/UUID 的列。当我使用SQL客户端+ ansi SQL对该表运行选择查询时,一切看起来都很好..我可以在结果集中看到我期望的guid值(guid在sql输出中看起来像这样“1ae5608d12311de123d001185135a13”)
但是在代码中尝试通过 JDBC 执行此操作时(我正在使用 Sybase “com.sybase.jdbc3.jdbc.SybDriver”驱动程序),当我迭代结果集并尝试读取该列时,它返回的基本上是垃圾。如果我执行 ResultSet.getObject(col),其中 col 是结果集中的二进制列索引,我会看到类型是 byte[]。我尝试将其转换为字符串并编码为 Base64,但无济于事。
关于如何做到这一点有什么想法吗?
谢谢
In my Sybase 12.0 ASE database, I have at table that contains a column defined as binary(16) and used to store GUIDS/UUIDs. When i run a select query using a SQL client + ansi SQL for that table, all looks fine..i can see the guid value as i expect it in the resultset (the guid looks something like this "1ae5608d12311de123d001185135a13" in the sql output)
However in code when trying to do this via JDBC(I am using the Sybase "com.sybase.jdbc3.jdbc.SybDriver" driver), when i iterate over the result set and try to read that columnn, it comes back as basically garbage. if i do a ResultSet.getObject(col), where col is the binary column index in my resultset, i see that the type is byte[]. I have tried converting this to string and encoding as Base64, but to no avail.
Any ideas on how to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到的
byte[]
正是它应该的样子:根据列定义,一个 16 字节或更少的序列。您尝试创建什么 UID 表示形式? - 它们通常不表示为 Base64,而是表示为 Hex。
尝试直接将
byte[]
转换为String
肯定不是您想要的。如果您迭代该数组,将每个字节转换为其(2 位!)十六进制表示形式(字符串),并将这些字符串连接为长度为 32 的字符串,结果可能看起来更像您所需要的。
如果您想使用
java.util .UUID
,您应该分别从第一组和第二组八个字节构建两个long
值,以传递给构造函数。编辑:
在
java.util.UUID
中,您可以找到:我不知道为什么它不是
public
...鉴于您的
byte[]
您可以使用此代码创建UUID(long,long
构造函数所需的两个long
值。您只需要注意从数据库接收到的数组中的字节顺序; 最低有效字节,即最高有效字节(可能是[0]和[15],或者[15]和[0]。)
一旦你有了一个 。
UUID
的实例,您可以使用其toString()
来获取通用表示形式,如果您不想使用
UUID
,则可以使用它。自己将字节转换为十六进制以进行输出,并且可能插入一些破折号以提高可读性。The
byte[]
you receive is exactly what it should be: A sequence of 16 bytes or less, according to the column definition.What representation of the UID are you trying to create? - They're not usually represented as Base64 but rather as Hex.
Trying to convert the
byte[]
to aString
directly is certainly not what you want.If you iterate over the array, converting each byte to its (2-digit!) hexadecimal representation (String), and concat those strings to a string of length 32, the result will probably look a lot more like what you need.
If you want to use
java.util.UUID
, you should build twolong
values from the first and the second set of eight bytes, respectively, to be passed to the constructor.Edit:
In
java.util.UUID
you can find:I have no idea why it is not
public
...Given your
byte[]
you can create the twolong
values needed for theUUID(long,long
constructor with exactly this code.You will only have to pay attention to the ordering of the bytes in the array you receive from the DB; which one is the least significant byte, which is the most significant byte. (Probably either [0] and [15], or [15] and [0].)
Once you have an instance of
UUID
you can use itstoString()
to get the common representation.If you don't want to use
UUID
you'd have to convert bytes to hex yourself for output and maybe insert some dashes for readability.