使用 JDBC 从 Sybase 表中以字符串形式读取 GUID

发布于 2024-12-12 09:22:05 字数 428 浏览 2 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行至春深 2024-12-19 09:22:05

您收到的 byte[] 正是它应该的样子:根据列定义,一个 16 字节或更少的序列。

您尝试创建什么 UID 表示形式? - 它们通常不表示为 Base64,而是表示为 Hex。

尝试直接将 byte[] 转换为 String 肯定不是您想要的。
如果您迭代该数组,将每个字节转换为其(2 位!)十六进制表示形式(字符串),并将这些字符串连接为长度为 32 的字符串,结果可能看起来更像您所需要的。

如果您想使用 java.util .UUID,您应该分别从第一组和第二组八个字节构建两个 long 值,以传递给构造函数。

编辑:

java.util.UUID中,您可以找到:

/*
 * Private constructor which uses a byte array to construct the new UUID.
 */
private UUID(byte[] data) {
    long msb = 0;
    long lsb = 0;
    assert data.length == 16;
    for (int i=0; i<8; i++)
        msb = (msb << 8) | (data[i] & 0xff);
    for (int i=8; i<16; i++)
        lsb = (lsb << 8) | (data[i] & 0xff);
    this.mostSigBits = msb;
    this.leastSigBits = lsb;
}

知道为什么它不是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 a String 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 two long 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:

/*
 * Private constructor which uses a byte array to construct the new UUID.
 */
private UUID(byte[] data) {
    long msb = 0;
    long lsb = 0;
    assert data.length == 16;
    for (int i=0; i<8; i++)
        msb = (msb << 8) | (data[i] & 0xff);
    for (int i=8; i<16; i++)
        lsb = (lsb << 8) | (data[i] & 0xff);
    this.mostSigBits = msb;
    this.leastSigBits = lsb;
}

I have no idea why it is not public...

Given your byte[] you can create the two long values needed for the UUID(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 its toString() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文