Firebird JDBC driver connection character encoding

前端 未结 3 2252
暖寄归人
暖寄归人 2021-02-20 17:21

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 encod

相关标签:
3条回答
  • 2021-02-20 18:16

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

    0 讨论(0)
  • 2021-02-20 18:16

    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!

    0 讨论(0)
  • 2021-02-20 18:25

    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
    
    0 讨论(0)
提交回复
热议问题