Java 无法通过 JDBC-ODBC 从 Access 检索 Unicode(立陶宛语)字母
我有数据库,其中一些名称是用立陶宛字母编写的,但是当我尝试使用java获取它们时,它会忽略
DbConnection();
zadanie=connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
sql="SELECT * FROM Clients;";
dane=zadanie.executeQuery(sql);
String kas="Imonė";
while(dane.next())
{
String var=dane.getString("Pavadinimas");
if (var!= null) {var =var.trim();}
String rus =dane.getString("Rusys");
System.out.println(kas+" "+rus);
}
void DbConnection() throws SQLException
{
String baza="jdbc:odbc:DatabaseDC";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){System.out.println("Connection error");}
connect=DriverManager.getConnection(baza);
}
数据库中的立陶宛字母字段类型为TEXT,大小20,不要使用任何额外的字母解码或类似的东西。
它给了我“Imonė Imone”,尽管在 DB 中写的是“Imonė”,等于 rus。
i have DB where some names are written with Lithuanian letters, but when I try to get them using java it ignores Lithuanian letters
DbConnection();
zadanie=connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
sql="SELECT * FROM Clients;";
dane=zadanie.executeQuery(sql);
String kas="Imonė";
while(dane.next())
{
String var=dane.getString("Pavadinimas");
if (var!= null) {var =var.trim();}
String rus =dane.getString("Rusys");
System.out.println(kas+" "+rus);
}
void DbConnection() throws SQLException
{
String baza="jdbc:odbc:DatabaseDC";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){System.out.println("Connection error");}
connect=DriverManager.getConnection(baza);
}
in DB type of field is TEXT, size 20, don't use any additional letter decoding or something like this.
it gives me " Imonė Imone " despite that in DB is written "Imonė" which equals rus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在 JDBC-ODBC 桥已从 Java 8 中删除,这个特定问题将越来越成为一个历史兴趣项目,但需要记录的是:
JDBC-ODBC 桥从未与 Access ODBC 驱动程序(“Jet”和“ACE”)用于代码点 U+00FF 以上的 Unicode 字符。这是因为 Access 将此类字符存储为 Unicode,但它不使用 UTF-8 编码。相反,它使用 UTF-16LE 的“压缩”变体,其中代码点 U+00FF 及以下的字符存储为单个字节,而 U+00FF 以上的字符存储为空字节,后跟其 UTF-16LE 字节对(s)。
如果字符串“Imonė”存储在 Access 数据库中,以便它在 Access 本身中正确显示
则它是存储为
(“ė”为 U+0117)。
JDBC-ODBC 桥不理解它从 Access ODBC 驱动程序接收的最后一个字符的内容,因此它只是返回
另一方面,如果我们尝试使用 UTF-8 编码将字符串存储在 Access 数据库中,就会发生这种情况如果 JDBC-ODBC 桥尝试插入字符串本身,
则该字符串将被编码为 UTF-8
,然后 Access ODBC 驱动程序会将其存储在数据库中,因为
C4
00 14 20
现在 JDBC-ODBC 桥可以正常检索它(因为 Access ODBC 驱动程序在退出时将字符“un-mangles”回
C4 97
),但是如果我们在 Access 中打开数据库,我们会看到JDBC-ODBC 桥从未也永远能够为 Access 数据库提供完整的本机 Unicode 支持。向 JDBC 连接添加各种属性不会解决问题。
要在不使用 ODBC 的情况下获得 Access 数据库的完整 Unicode 字符支持,请考虑使用 UCanAccess。 (更多详细信息请参阅此处的另一个问题。)
Now that the JDBC-ODBC Bridge has been removed from Java 8 this particular question will increasingly become just an item of historical interest, but for the record:
The JDBC-ODBC Bridge has never worked correctly with the Access ODBC Drivers ("Jet" and "ACE") for Unicode characters above code point U+00FF. That is because Access stores such characters as Unicode but it does not use UTF-8 encoding. Instead, it uses a "compressed" variation of UTF-16LE where characters with code points U+00FF and below are stored as a single byte, while characters above U+00FF are stored as a null byte followed by their UTF-16LE byte pair(s).
If the string 'Imonė' is stored within the Access database so that it appears properly in Access itself
then it is stored as
('ė' is U+0117).
The JDBC-ODBC Bridge does not understand what it receives from the Access ODBC driver for that final character, so it just returns
On the other hand, if we try to store the string in the Access database with UTF-8 encoding, as would happen if the JDBC-ODBC Bridge attempted to insert the string itself
the string would be UTF-8 encoded as
and then the Access ODBC Driver will store it in the database as
C4
00 14 20
Now the JDBC-ODBC Bridge can retrieve it okay (since the Access ODBC Driver "un-mangles" the character back to
C4 97
on the way out), but if we open the database in Access we seeThe JDBC-ODBC Bridge has never and will never be able to provide full native Unicode support for Access databases. Adding various properties to the JDBC connection will not solve the problem.
For full Unicode character support of Access databases without ODBC, consider using UCanAccess instead. (More details available in another question here.)
当您使用 JDBC-ODBC 桥时,您可以在连接详细信息中指定字符集。
试试这个:
As you're using the JDBC-ODBC bridge, you can specify a charset in the connection details.
Try this:
尝试使用“Windows-1257”而不是 UTF-8,这是针对波罗的海地区的。
Try to use this "Windows-1257" instead of UTF-8, this is for Baltic region.