我想读从我的MySQL数据库,并创建使用UTF-8字符串:'字符被转换为€™在JDBC
:CREATE DATABASE april
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
我使用使感兴趣的表
DROP TABLE IF EXISTS `article`;
CREATE TABLE `article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` longtext NOT NULL,
`date_created` timestamp DEFAULT NOW(),
PRIMARY KEY (`id`)
) CHARACTER SET utf8;
如果我在MySQL命令行UTIL select * from article
,我得到:
OIL sands output at Nexen’s Long Lake project dropped in February.
然而,当我做
ResultSet rs = st.executeQuery(QUERY);
long id = -1;
String text = null;
Timestamp date = null;
while (rs.next()) {
text = rs.getString("text");
LOGGER.debug("text=" text);
}
输出我得到的是:
text=OIL sands output at Nexen’s Long Lake project dropped in February.
我得到通过我的连接:
DriverManager.getConnection("jdbc:" + this.dbms + "://" + this.serverHost + ":" + this.serverPort + "/" + this.dbName + "?useUnicode&user=" + this.username + "&password=" + this.password);
我也试过,而不是useUnicode
参数:
characterEncoding=UTF-8
and
characterEncoding=utf8
我也试过了,而不是行
rs.getBytes("text");
String[] encodings = new String[]{"US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16BE", "UTF-16LE", "UTF-16", "Latin1"};
for (String encoding : encodings) {
text = new String(temp, encoding);
LOGGER.debug(encoding + ": " + text);
}
// Which outputted:
US-ASCII: OIL sands output at Nexen��������s Long Lake project dropped in February.
ISO-8859-1: OIL sands output at Nexenââ¬â¢s Long Lake project dropped in February.
UTF-8: OIL sands output at Nexen’s Long Lake project dropped in February.
UTF-16BE: 佉䰠獡湤猠潵瑰畴琠乥硥滃ꋢ芬ꉳ⁌潮朠䱡步⁰牯橥捴牯灰敤渠䙥扲畡特�
UTF-16LE: 䥏⁌慳摮畯灴瑵愠⁴敎數썮겂蓢玢䰠湯慌敫瀠潲敪瑣搠潲灰摥椠敆牢慵祲�
UTF-16: 佉䰠獡湤猠潵瑰畴琠乥硥滃ꋢ芬ꉳ⁌潮朠䱡步⁰牯橥捴牯灰敤渠䙥扲畡特�
Latin1: OIL sands output at Nexenââ¬â¢s Long Lake project dropped in February.
我使用文件中的一些预定义的sql将字符串加载到数据库中。该文件是UTF-8编码的。
mysql -u april -p -D april < insert_articles.sql
此文件包含行:
INSERT INTO article (text) value ("OIL sands output at Nexen’s Long Lake project dropped in February.");
当我打印出来,我的应用程序中的文件中使用:
BufferedReader reader = new BufferedReader(new FileReader(new File("/home/path/to/file/sql_article_inserts.sql")));
String str;
while((str = reader.readLine()) != null) {
LOGGER.debug("LINE: " + str);
}
我得到正确,预期输出:
LINE: INSERT INTO article (text) value ("OIL sands output at Nexen’s Long Lake project dropped in February.");
任何帮助将亩ch赞赏。
一些系统细节: 我运行在Linux(Ubuntu的)
编辑:
*编辑,以指定OS
*编辑,以读取SQL输入文件详细输出。
*编辑指定更多关于如何将数据插入数据库。
*修改为修复代码中的错字,并澄清示例。
你正在使用什么记录器? – Powerlord 2011-04-05 15:01:32
LOGGER是一个实例:org.apache.log4j.Logger – barryred 2011-04-05 15:12:08
不,这样做,似乎我运行UTF-8:System.getProperty(“file.encoding”)='UTF-8' – barryred 2011-04-05 16:14:38