Firebird JDBC driver connection character encoding

假如想象 提交于 2020-01-12 14:06:49

问题


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.

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.


回答1:


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

For example:

jdbc:firebirdsql:url:db?encoding=ISO8859_1



回答2:


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.

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.

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).

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).




回答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). Any encoding i used on the connection other than 'NONE' resulted with timeout getting a connection. Eventually i kept using '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 column = new String(rs.getBytes("column"), "Windows-1255");

[rs is ResultSet of course]

Good luck!



来源:https://stackoverflow.com/questions/13156329/firebird-jdbc-driver-connection-character-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!