简体   繁体   English

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

[英]Firebird JDBC driver connection character encoding

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

The language is Brazilian portuguese so I need é's and ã's and ç and here all of these special characters come with problems. 这种语言是巴西葡萄牙语,所以我需要é和ã和ç,这里所有这些特殊字符都有问题。

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 é 这是一个奇怪的角色应该是é的图像

有问题的数据表

The problem happens when it recovers from the DB. 当它从数据库中恢复时会发生问题。

Using encoding=ISO/UTF/WIN... query parameter in the JDBC connection URL has solved the problem. 在JDBC连接URL中使用encoding=ISO/UTF/WIN...查询参数解决了这个问题。

For example: 例如:

jdbc:firebirdsql:url:db?encoding=ISO8859_1

When you don't specify the connection character set in Jaybird (either property encoding using the Firebird character set name, or charSet with 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中指定连接字符集(使用Firebird字符集名称进行属性encoding ,或使用Java字符集名称进行charSet ),则Jaybird将回退到连接字符集NONE的Firebird概念,这意味着因为服务器不会从(VAR)CHAR列的存储表示中音译字符并按原样发送其字节。

This means that Jaybird receives a sequence of bytes in an unknown character set. 这意味着Jaybird接收未知字符集中的字节序列。 Jaybird will then use the default character set of your Java installation to convert those bytes to strings. 然后,Jaybird将使用Java安装的默认字符集将这些字节转换为字符串。 So if the db (or column) character set does not match your Java character set, then it can produce incorrect results. 因此,如果db(或列)字符集与Java字符集不匹配,则可能会产生不正确的结果。 Even worse: reading and writing this way from different systems with different default java character sets can produce total garbage or transliteration errors. 更糟糕的是:使用不同的默认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)CHAR column 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 ). 如果没有指定显式连接字符集来强制用户考虑此问题(如果他们仍然需要旧行为,则他们可以明确指定encoding=NONE ),Jaybird(2.3)的下一个版本可能会拒绝连接。

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). 自从我到谷歌寻找答案以来我的2美分..我使用传统的jaybird驱动程序(v1.5)与interbase 7.5.80有问题。 Any encoding i used on the connection other than 'NONE' resulted with timeout getting a connection. 我在“NONE”以外的连接上使用的任何编码都会导致超时连接。 Eventually i kept using '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! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM