Firebird JDBC driver connection character encoding

偶尔善良 提交于 2019-12-04 02:14:22
Vitor Hugo

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

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

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!

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