2011-01-06 111 views
1

我正在尝试编写一个简单的Java 客户端程序,它使用koi8r作为其字符集,并继续失败。MySql Jdbc连接器 - 本地化问题

Class.forName("com.mysql.jdbc.Driver"); 
Connection conn2 = DriverManager.getConnection ("jdbc:mysql://localhost:3306/test","root",null); 
Statement stmt = conn2.createStatement(); 
int result; 
result = stmt.executeUpdate("SET CHARACTER SET koi8r"); 

stmt = conn2.createStatement(); 
result = stmt.executeUpdate("DROP TABLE IF EXISTS װֱֱֲֳֹּ, t1, t2"); 
stmt.close(); 
assertEquals(0, result); 

我越来越

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '???????, t1, t2' at line 1 

当我把这些命令的脚本文件,并使用MySQL客户端,它工作正常

SET CHARACTER SET koi8r 
DROP TABLE IF EXISTS װֱֱֲֳֹּ, t1, t2 

我嗅到了网络,我看到了他们执行jdbc连接器使用??????到服务器,所以我想我错过了连接的一些设置。 其实我试过(setEncoding,setCharactersEncoding,setConncetionCollat​​ion ...),但仍然失败。

+0

嗨我不太了解koi8r,但这个链接可以帮助。 http://www.fileformat.info/info/charset/KOI8-R/index.htm – Ankit 2011-05-06 17:09:29

回答

3

您需要告诉JDBC驱动程序应该使用哪种编码通过网络传输字符。它默认为平台默认编码,这在你的情况下显然不是UTF-8。

您可以通过添加以下两个查询参数的JDBC URL做到这一点:

jdbc:mysql://localhost:3306/test?useUnicode=yes&characterEncoding=UTF-8 

又见the documentation