CharSet for MS Access '97 DB using UCanAccess

孤人 提交于 2021-02-05 08:28:13

问题


Hey I am reading a database using ucanaccess-5.0.0. The database is propably < Access 2000, since it's not UTF-8. It seems like I have to setup a charset. I tried this using the charset parameter in the url:

jdbc:ucanaccess://${databaseFile.absolutePath};memory=false;charSet=Cp1250

It seems to be ignored, since I still don't get my german umlauts out of the DB.

What can I do about it?


回答1:


Working Solution:

class DatabaseOpener : JackcessOpenerInterface {
  override fun open(fl: File, pwd: String?): Database {
    return DatabaseBuilder.open(fl).apply {
      this.charset = charset("Cp1252")
    }
  }
}

// URL
"jdbc:ucanaccess://<path-to-mdb-file>;memory=false;jackcessOpener=${DatabaseOpener::class.qualifiedName!!}"




回答2:


on java:

public class CryptCodecOpener implements JackcessOpenerInterface {
    public Database open(File fl,String pwd) throws IOException {
        DatabaseBuilder dbd =new DatabaseBuilder(fl);
        dbd.setCodecProvider(new CryptCodecProvider(pwd));
        dbd.setCharset(Charset.forName("WINDOWS-1251"));
        return dbd.open();

    }

public class MainTest {
    public static void main(String[] args) throws IOException {
        String connUrl = "jdbc:ucanaccess://C:/GATE/Server/config.mdb";
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            Connection conn =
                    DriverManager.getConnection(connUrl + ";jackcessOpener=com.example.demo.CryptCodecOpener");
            Statement st = conn.createStatement();
            String sql = "SELECT * FROM Users LIMIT 10";
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                System.out.println("\n" + rs.getString(1) + "\t" + rs.getString(3));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

maven:

    <dependency>
        <groupId>net.sf.ucanaccess</groupId>
        <artifactId>ucanaccess</artifactId>
        <version>5.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.healthmarketscience.jackcess</groupId>
        <artifactId>jackcess-encrypt</artifactId>
        <version>3.0.0</version>
    </dependency>


来源:https://stackoverflow.com/questions/59714917/charset-for-ms-access-97-db-using-ucanaccess

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