java Firebird JDBC 驱动程序连接字符编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13156329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:41:32  来源:igfitidea点击:

Firebird JDBC driver connection character encoding

javajdbcfirebirdfedora

提问by Vitor Hugo

I have a JSF application running on tomcat6 in Fedora 17 using firebird as the database and all the registers coming from the database to the application are coming with a encoding problem.

我有一个 JSF 应用程序在 Fedora 17 中的 tomcat6 上运行,使用 firebird 作为数据库,并且所有从数据库到应用程序的寄存器都存在编码问题。

The language is Brazilian portuguese so I need é's and ?'s and ? and here all of these special characters come with problems.

语言是巴西葡萄牙语,所以我需要 é's 和 ?'s 和 ? 在这里所有这些特殊字符都有问题。

The é's and ?'s from the original source code are ok, only the ones coming directly from the database are causing me the trouble...

来自原始源代码的 é 和 ? 没问题,只有直接来自数据库的那些才会给我带来麻烦......

Any idea what is going on?

知道发生了什么吗?

Heres a image where that weird character should be é

这是一个图像,其中那个奇怪的字符应该是 é

datatable with the problem

有问题的数据表

The problem happens when it recovers from the DB.

当它从数据库中恢复时会出现问题。

回答by Vitor Hugo

Using encoding=ISO/UTF/WIN...query parameter in the JDBC connection URL has solved the problem.

encoding=ISO/UTF/WIN...在 JDBC 连接 URL 中使用查询参数已经解决了这个问题。

For example:

例如:

jdbc:firebirdsql:url:db?encoding=ISO8859_1

回答by Mark Rotteveel

When you don't specify the connection character set in Jaybird (either property encodingusing the Firebird character set name, or charSetwith a Java character set name), then Jaybird falls back to the Firebird concept of connection character set NONE, which means as much as that the server will not transliterate characters from the storage representation of a (VAR)CHAR column and sends its bytes as is.

如果您没有在 Jaybird 中指定连接字符集(encoding使用 Firebird 字符集名称的属性,或charSet使用 Java 字符集名称),则 Jaybird 会退回到 Firebird 的连接字符集概念NONE,这意味着服务器不会从 (VAR)CHAR 列的存储表示中音译字符并按原样发送其字节。

This means that Jaybird receives a sequence of bytes in an unknown character set. Jaybird will then use the default character set of your Java installation to convert those bytes to strings. So if the db (or column) character set does not match your Java character set, then it can produce incorrect results. Even worse: reading and writing this way from different systems with different default java character sets can produce total garbage or transliteration errors.

这意味着 Jaybird 会收到一个未知字符集的字节序列。Jaybird 将使用您的 Java 安装的默认字符集将这些字节转换为字符串。因此,如果 db(或 column)字符集与您的 Java 字符集不匹配,那么它可能会产生不正确的结果。更糟糕的是:从具有不同默认 java 字符集的不同系统以这种方式读取和写入可能会产生完全垃圾或音译错误。

The solution: always specify an explicit connection character set. Even better is to make sure your database has a default character set (or that every (VAR)CHARcolumn has its character set explicitly defined).

解决方案:始终指定显式连接字符集。更好的是确保您的数据库具有默认字符集(或每(VAR)CHAR列都明确定义了其字符集)。

The next version of Jaybird (2.3) will probably refuse to connect if no explicit connection character set was specified to force users to consider this issue (and if they still want the old behavior then they can explicitly specify encoding=NONE).

如果没有指定明确的连接字符集来强制用户考虑这个问题,那么下一个版本的 Jaybird (2.3) 可能会拒绝连接(如果他们仍然想要旧的行为,那么他们可以明确指定encoding=NONE)。

回答by baraka

My 2 cents since i got here by Google looking for an answer.. I had an issue with interbase 7.5.80 using legacy jaybird driver (v1.5). Any encoding i used on the connection other than 'NONE' resulted with timeout getting a connection. Eventually i kept using 'NONE':

自从我通过 Google 来到这里寻找答案以来,我花了 2 美分。我在使用旧版 jaybird 驱动程序 (v1.5) 时遇到了 interbase 7.5.80 问题。除了“NONE”之外,我在连接上使用的任何编码都会导致连接超时。最终我一直使用“NONE”:

FBWrappingDataSource dataSource = new FBWrappingDataSource();
dataSource.setDatabase("url");
dataSource.setType("TYPE4");
dataSource.setEncoding("NONE");
dataSource.setLoginTimeout(10);
java.sql.Connection c = dataSource.getConnection("sysdba", "masterkey");

And used getBytes while creating a new String instance with a specific encoding:

并在创建具有特定编码的新 String 实例时使用 getBytes:

String column = new String(rs.getBytes("column"), "Windows-1255");

[rs is ResultSet of course]

[rs当然是ResultSet]

Good luck!

祝你好运!