问题
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