LeedMx
LeedMx

Reputation: 434

Obtainig illegible characters from uCanAccess result set

I'm querying an old Access 97 database with Spanish characters ñ á é í ó ú. I can read the characters fine using access so I'm assuming it uses ISO-8859-1 however I am not able to convert them to UTF-8.

Not sure if my problem has to do with my connection:

public static Connection baseAccess(String base) {
    try {
        java.util.Properties propiedades = new java.util.Properties();
        propiedades.put("charSet", "ISO-8859-1");
        return DriverManager.getConnection(String.format("jdbc:ucanaccess://%s", base),propiedades);
    } catch (SQLException ex) {}
}

Or with my parsing of the String:

try (ResultSet rs = UtileriaDb.baseAccess(contabilidad).createStatement().executeQuery(sql);){
    while(rs.next()){
        String fromAccess = rs.getString(1);
        System.out.println(fromAccess);
        String transformed = new String(fromAccess.getBytes("ISO-8859-1"),"UTF-8");
        System.out.println(transformed);
    }
} catch (Exception ex) { }

So when I expect:

Año Café

I get:

A�o Caf�

A?o Caf?

Tried by getting the byte content from the result set by doing: byte[] fromAccess = rs.getBytes(1); but I got an exception

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.4 incompatible data type in conversion: from SQL type VARCHAR to [B, value: myText
    at net.ucanaccess.jdbc.UcanaccessResultSet.getBytes(UcanaccessResultSet.java:339)

So that's a dead end. Is there maybe another way to go about it?

Upvotes: 1

Views: 304

Answers (1)

LeedMx
LeedMx

Reputation: 434

As @Scratte correctly pointed out, rs.getString(1) was returning a String that already contained replacement characters. Thanks for his help in pointing me towards this question and this discussion

I ended up implementing a ucanaccess JackcessOpenerInterface and worked wonderfully

package com.company.somepackage;

import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import net.ucanaccess.jdbc.JackcessOpenerInterface;

public class CharsetOpener implements JackcessOpenerInterface {
    public Database open(File f, String pwd) throws IOException {
        DatabaseBuilder db = new DatabaseBuilder(f);
        db.setCharset(Charset.forName("ISO-8859-1"));
        try {
            db.setReadOnly(false);
            return db.open();
        } catch (IOException e) {
            db.setReadOnly(true);
            return db.open();
        }
   }
}

And for the implementation in my Connection generator:

public static Connection baseAccess(String base) {
    try {
        java.util.Properties propiedades = new java.util.Properties();
        propiedades.put("jackcessOpener", "com.company.somepackage.CharsetOpener");
        return DriverManager.getConnection(String.format("jdbc:ucanaccess://%s", base),propiedades);
    } catch (SQLException ex) {}
}

Notice that the actual property is jackcessOpener and must point to a fully qualified class name.

Upvotes: 1

Related Questions