Java ODBC MS-Access Unicode 字符问题 [英] Java ODBC MS-Access Unicode character problems

查看:30
本文介绍了Java ODBC MS-Access Unicode 字符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个连接到 Access 数据库的应用程序.我已经通过使用 .mdb 文件建立我的 Java 程序的 ODBC 连接,但是我遇到了 Unicode 字符的这个问题.如果记录是用英语(拉丁)字符编写的,那么 .mdb 文件可以识别这些字符,但如果记录是用希腊语编写的,则会出现一些奇怪的字符,我无法使用 ResultSet 对象获取记录.有人可以帮忙吗?

解决方案

当字符串包含代码点高于 U+007F 的 Unicode 字符时,JDBC-ODBC Bridge 将无法与 Access ODBC 驱动程序一起正常工作.希腊字符属于该类别,因此 JDBC-ODBC Bridge 方法对您不起作用.(更多详细信息此处.)此外,JDBC-ODBC Bridge 已从 Java 中删除(自 Java 8 起).>

要获得对希腊字符的适当支持,我建议使用 UCanAccess.有关如何设置的概述,请参阅我的另一个答案此处.

一旦您的项目被配置为使用 UCanAccess,您就可以使用如下代码处理您的 Access 数据库:

Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:/__tmp/unicode.accdb");字符串语言 = "希腊语";PreparedStatement ps = conn.prepareStatement("SELECT [word], [english_equiv] " +从[词汇]" +"WHERE 语言=?");ps.setString(1, 语言);结果集 rs = ps.executeQuery();而(rs.next()){System.out.println(String.format(""%s" 是 "%s" 的 %s.",rs.getString("word"),语,rs.getString("english_equiv")));}rs.close();ps.close();String newWord = "ηλεκτρονικός υπολογιστής";String newEnglishEquiv = "计算机";ps = conn.prepareStatement("插入 [词汇] ([单词], [语言], [english_equiv]) " +值(?,?,?)");ps.setString(1, newWord);ps.setString(2, 语言);ps.setString(3, newEnglishEquiv);ps.executeUpdate();System.out.println(String.format(""%s" 已添加到表中.",新词));

该代码产生以下控制台输出:

"γιορτή" 在希腊语中是盛宴"的意思ηλεκτρονικός υπολογιστής"已添加到表格中.

(翻译由 Google 翻译 提供.)

I am trying to make an application that connects to an Access database. I have made it through making the ODBC connection of my Java program with an .mdb file but I have this problem with Unicode characters. If a record is written in English (Latin) characters then the .mdb file recognizes the characters but if the record is written in Greek then some weird characters appear and I can't get the record with the ResultSet object. Can someone help?

解决方案

The JDBC-ODBC Bridge will not work correctly with the Access ODBC driver when strings contain Unicode characters whose code point is above U+007F. Greek characters fall into that category, so the JDBC-ODBC Bridge approach will not work for you. (More details here.) Also, the JDBC-ODBC Bridge has been removed from Java (since Java 8).

To get proper support for Greek characters I would recommend using UCanAccess. For an overview of how to set that up, see another of my answers here.

Once your project has been configured to use UCanAccess you can work with your Access database using code like this:

Connection conn=DriverManager.getConnection(
        "jdbc:ucanaccess://C:/__tmp/unicode.accdb");
String language = "Greek";

PreparedStatement ps = conn.prepareStatement(
        "SELECT [word], [english_equiv] " +
        "FROM [vocabulary] " +
        "WHERE language=?");
ps.setString(1, language);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
    System.out.println(String.format(
            ""%s" is %s for "%s".", 
            rs.getString("word"),
            language,
            rs.getString("english_equiv")));
}
rs.close();
ps.close();

String newWord = "ηλεκτρονικός υπολογιστής";
String newEnglishEquiv = "computer";
ps = conn.prepareStatement(
        "INSERT INTO [vocabulary] ([word], [language], [english_equiv]) " +
        "VALUES (?,?,?)");
ps.setString(1, newWord);
ps.setString(2, language);
ps.setString(3, newEnglishEquiv);
ps.executeUpdate();
System.out.println(String.format(
        ""%s" has been added to the table.", 
        newWord));

That code produces the following console output:

"γιορτή" is Greek for "feast"
"ηλεκτρονικός υπολογιστής" has been added to the table.

(Translations courtesy of Google Translate.)

这篇关于Java ODBC MS-Access Unicode 字符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆