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

查看:122
本文介绍了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桥已从Java中删除(自Java 8开始).

要获得对希腊字符的适当支持,我建议使用 UCanAccess .有关如何进行设置的概述,请在此处.

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

 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));
 

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

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

(翻译由 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天全站免登陆