在使用字符串作为值的查询中,为什么 ResultSet 会少一个字符?

发布于 2024-09-11 06:15:27 字数 741 浏览 1 评论 0原文

或者更简单,我有这段代码(Oracle 数据库)

SELECT TO_NUMBER (tkb.id_kucnog_broja) id, 'Buildings and home numbers' vrsta_pj,

,在 Java 中我有这个

for (int j = 0; j < rs.getMetaData().getColumnCount(); j++) {
data = rs.getString(j + 1);
}

,它得到的结果是“建筑物和家庭号码”,这意味着它“剪切”了最后一个字符(如果我更改字符串,则结果相同)。我可以用 2 个字符创建字符串,但我真的很想知道为什么会发生这种情况。

编辑:

设法查明问题。真正的字符串是“Zgrade i kućni brojevi”(我在这里翻译了它),似乎符号“ć”(我猜任何其他类似“š”或“đ”的符号)就是问题所在。如果我写任何大小的字符串(没有进行太多实验,做了很多工作:))并且不放入克罗地亚语“特殊”符号,一切都很好,但是当我放入它时,字符串会缩短按一个字符。特别有趣的是,当我在 Toad 中执行它时,它运行得很好。

EDIT2:似乎克罗地亚特殊字符占据了 2 个位置。如果我输入“\u107”,问题仍然存在。我可以制定一个解决方法(编写 '&#263;' 或在其末尾添加任何字符串),以便解决问题(以一种我知道如何完成工作的方式) )但我仍然很好奇如何正确地做到这一点。

Or simpler, I have this code (Oracle database)

SELECT TO_NUMBER (tkb.id_kucnog_broja) id, 'Buildings and home numbers' vrsta_pj,

and in Java I have this

for (int j = 0; j < rs.getMetaData().getColumnCount(); j++) {
data = rs.getString(j + 1);
}

and the result it gets is 'Buildings and home number', meaning it 'cuts' the last char (same thing if I change the string). I can make the string with 2 's' chars, but I'd really like to know why does this happen.

EDIT:

Managed to pinpoint the problem. The real string is 'Zgrade i kućni brojevi' (I've translated it here) and it seems the sign 'ć' (and i'm guessing any other like 'š' or 'đ') is(are) the problem. If I write any sized string (didn't experiment too much, got a lot of work :) ) and don't put in croatian 'special' signs, everything is ok, but the moment I put it in, the string gets shortened by one char. What's specially interesting is that it works just fine when I execute it in Toad.

EDIT2: Seems croatian special chars take 2 places. If I put '\u107' the problem remains. I can make a workaround (write 'ć' or add any string at the end of this one) so the problem is solved (in a way that I know how to get the job done) but I'm still curious how to do it properly.

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

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

发布评论

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

评论(1

素罗衫 2024-09-18 06:15:27

基本上,数据库不仅仅是数据转储。当您从数据库插入/更新/选择字符串 (VARCHAR2/NVARCHAR2/CHAR/NCHAR/CLOB) 时,它会考虑客户端和数据库的字符集并执行任何必要的转换。

有些字符集是单字节,有些是固定长度多字节(例如,所有字符占用两个字节),有些是可变长度多字节(例如,常见字符占用一个字节,“特殊”字符占用两到四个字节)。

因此,您需要考虑数据库中字符串的长度(以字节和字符为单位)和Java中字符串的长度(同样以字节/字符为单位)。我怀疑你最终得到的是 java 字符串的长度(以字符为单位),但随后将其视为以字节为单位的长度。

值得检查你的驱动程序。

Basically, a database is not just a data dump. When you insert/update/select a string (VARCHAR2/NVARCHAR2/CHAR/NCHAR/CLOB) from a database it takes into consideration the characterset of the client and the database and performs any necessary conversion.

Some characterset are single bye, some are fixed length multi-byte (eg all characters take two bytes) and some are variable length multi-byte (eg common characters take one byte, 'special' characters take between two and four).

So you need to consider the length of the string in the database (in bytes and characters) and the length of the string in Java (again in bytes/characters). I suspect you are ending up with the length of the java string in characters, but then treating that as the length in bytes.

Worth checking your drivers.

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