2

I have a Java program that needs to access a database in Firebird with encoding DOS437.

I need convert the result of a query to UTF8, How can I do this?

My code in java:

public List<Proceso> ObtenerListaProcesos() throws SQLException {
    List<Proceso> procesos = new ArrayList<>();
    Proceso proceso = null;
    Connection conn = null;
    ResultSet rs = null;
    String query = "select * from procesos where prc_web='V' and prc_valido='V' and prc_valido_fabrica='V'  and prc_suplemento_Acabado='F' and prc_codigo!='-' order by prc_descripcion;";
    String textoProceso="";
    try {
        conn = this.abrirConexion();
        PreparedStatement p = conn.prepareStatement(query);

        rs = p.executeQuery();
        while (rs.next()) {

            proceso = new Proceso(rs.getString("PRC_CODIGO"),
                    rs.getString("PRC_DESCRIPCION"));

            procesos.add(proceso);
        }

    } catch (Exception e) {
        log.error("Error en ObtenerListaProcesos: " + e.getMessage());
    } finally {
        this.cerrarConexion(conn);
    }

    return procesos;
}

I've an encoding in the firebird connection:

<conexionServidor>jdbc:firebirdsql:servidor/3050:F:/apps/GESTIONGYM2004/BD/BDGYM.FDB</conexionServidor>
<userBD>SYSDBA</userBD>
<passBD>masterkey</passBD>
<encodingBD>DOS437</encodingBD>

I need write the result of query in utf-8 to show in html file, but if use : new String(rs.getBytes("columnname"), "Cp437"), the result is not in UTF-8.

Alejandro Galera
  • 3,445
  • 3
  • 24
  • 42
asirvent
  • 67
  • 1
  • 6
  • 3
    Please check [this](http://stackoverflow.com/a/13163986/655756) answer in order to set correct encoding in JDBC driver, and further converting in UTF-8 in Java should be automatically. – n1ckolas Nov 26 '13 at 12:42

1 Answers1

3

Assuming your database has a default character set (or each (VAR)CHAR column its specific character set), you simply need to specify a connection character set and Firebird will transliterate from the storage encoding to the connection encoding; this can be any character set that includes the full repertoire of DOS437 (eg UTF8, but also DOS437 itself).

If your database does not have a default character set (it is NONE), or the columns with the data have character set NONE there won't be a fully automatic transliteration and you will need to explicitly specify connection character set DOS437 and Jaybird will handle the conversion.

You can specify the connection character set using the connection property encoding (which accepts the Firebird character set name) or charSet (which accepts the Java character set name), eg:

jdbc:firebirdsql://localhost//path/to/db.fdb?encoding=DOS437

You mention that you want to convert to utf8, but that is irrelevant. You simply need Jaybird to read the data in the right character set to get the right characters into Java. After that it is simply a matter of writing it out as UTF-8 to its destination.

If you have very special needs and specifying the connection character set is not possible, you can do the conversion yourself by using new String(rs.getBytes("columnname"), "Cp437"), but this should be a last resort only.

Disclaimer: I am a developer of Jaybird (Firebird JDBC driver)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197