JDBC从包含控制字符的数据库表中读取数据

发布于 2024-11-04 13:20:10 字数 1531 浏览 0 评论 0原文

我有以下代码从 Oracle 表中读取数据(注意:这是在 Jdk 1.4.2 上运行)

ResultSet message = messageStatement.executeQuery(getMsgSql);
String messageData = message.getString("MESSAGE_DATA");

MESSAGE_DATA 列中的数据包含文本,但也包含分隔消息中数据元素的控制字符(即 (char )31、(字符)29) 和 (字符)28) 。

我发现由于某种原因 message.getString() 有时会截断消息。我可以阅读大部分消息,但其中一些已被截断。我应该使用不同的方法读取数据吗?如果是这样怎么办?

我尝试使用 sqlplus 查看数据库中的数据,它就在那里,只是被 message.getString() 方法截断了。当我尝试输出结果时,我看到了这一点,即 System.out.println(message.getString())。

谢谢

更新

我在Oracle中使用length()函数运行了一个sql查询,length(“MESSAGE_DATA”)返回2032,而在java中message.getString(1).length()返回2000。我不确定为什么会发生这种情况。

更新

好吧,我可能会想到一些我认为的事情。我刚刚尝试了较新版本的 JDBC 驱动程序,它似乎可以正常工作。所使用的驱动程序是为 jdk1.4 交付而提供的旧版本。有谁知道为什么会这样?而且,使用较新版本的 jdbc 驱动程序是否会产生任何影响,特别是考虑到应用程序在 JDK 1.4 上运行这一事实。 oracle 版本是 10.1.0.3.0

更新

我不确定这是否有帮助,但这里是两个 JDBC 驱动程序的版本。

不起作用的 JDBC 驱动程序

=====  Database info =====
DatabaseProductName: Oracle
DatabaseProductVersion: Oracle Database 10g Release 10.1.0.3.0 - 64bit Production
=====  Driver info =====
DriverName: Oracle JDBC driver
DriverVersion: 8.0.5.2.0
DriverMajorVersion: 8
DriverMinorVersion: 0

起作用的 JDBC 驱动程序

=====  Database info =====
DatabaseProductName: Oracle
DatabaseProductVersion: Oracle Database 10g Release 10.1.0.3.0 - 64bit Production
=====  Driver info =====
DriverName: Oracle JDBC driver
DriverVersion: 10.2.0.1.0
DriverMajorVersion: 10
DriverMinorVersion: 2

谢谢

I have the following bit of code that reads data from the an Oracle table (Note: This is running on Jdk 1.4.2)

ResultSet message = messageStatement.executeQuery(getMsgSql);
String messageData = message.getString("MESSAGE_DATA");

The data in the MESSAGE_DATA column contains text but also control characters that separate data elements in the message (i.e (char)31, (char)29) and (char)28) .

What i am finding is that for some reason message.getString() is sometimes truncating the message. I can read the majority of messages but some of them are truncated. Am i supposed to be reading the data using a different method? If so how?

I have tried to use sqlplus to look at the data in the database and it is all there it is just truncated by the message.getString() method. I saw this when i tried to output the result i.e. System.out.println(message.getString()).

Thanks

Update

I ran an sql query using the length() function in Oracle and length("MESSAGE_DATA") returns 2032 whereas in java message.getString(1).length() returns 2000. Im not sure why this is happening.

Update

Ok i might be on to something i think. I just tried a newer version of the JDBC driver and it seems to be working. The driver that was being used was an older version that was delivered for the jdk1.4 delivery. Does anyone know why this is the case? And also, does using a newer version of the jdbc driver have any implications especially given the fact that the application is running on JDK 1.4. The oracle version is 10.1.0.3.0

Update

I am not sure if this is any helpfull but here are the versions of the two JDBC drivers.

The JDBC driver that does not work

=====  Database info =====
DatabaseProductName: Oracle
DatabaseProductVersion: Oracle Database 10g Release 10.1.0.3.0 - 64bit Production
=====  Driver info =====
DriverName: Oracle JDBC driver
DriverVersion: 8.0.5.2.0
DriverMajorVersion: 8
DriverMinorVersion: 0

The JDBC Driver that does work

=====  Database info =====
DatabaseProductName: Oracle
DatabaseProductVersion: Oracle Database 10g Release 10.1.0.3.0 - 64bit Production
=====  Driver info =====
DriverName: Oracle JDBC driver
DriverVersion: 10.2.0.1.0
DriverMajorVersion: 10
DriverMinorVersion: 2

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

悸初 2024-11-11 13:20:10

我的猜测:数据库有一个非默认字符集。如果 Java 驱动程序不知道该字符集,它将期望默认值(无论它是什么),并进行错误的解码,有时可能会截断字符串。

My guess: database has a non-default charset. If Java driver is unaware of that charset, it will expect the default (whatever it is), and do a decoding wrong, sometimes possible truncating the string.

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