Java – 使用PreparedStatement在mysql数据库中存储UTF-8字符(特别是俄语字符)

使用准备好的语句我试图将俄语字符存储在特定表的字段中.表和字段使用utf8字符集和utf8_bin排序规则.

如果我通过IDE或命令行手动运行插入,则字符串将按预期保存.

INSERT INTO utf8Table VALUES('Анатолий Солоницын');

然后,我可以查询该表,并且还可以返回预期的String.

通过tomcat的context.xml文件中的资源设置Connection,其中包含以下配置:

正如我所说,我能够很好地读取字符串/字符,所以我假设表和连接设置都正确设置.

我通过以下方式使用PreparedStatement / CallableStatement:

CallableStatement callableStmt = __mySqlConnector.getConnection().prepareCall("INSERT INTO utf8Table VALUES(?)");

callableStmt.setString(1,"Анатолий Солоницын");

callableStmt.executeUpdate();

什么是插入数据库而不是АнатолийСолоницын:???????? ?????????

Also note that “normal” utf-8 strings,such as über,are getting saved
properly.

System.getProperty("file.encoding") 

java.nio.charset.Charset.defaultCharset().name() 

也都返回UTF-8.

任何帮助表示赞赏.谢谢

最佳答案
您需要在数据库URL中定义连接编码,而不是在属性中定义.

请参阅http://dev.mysql.com/doc/refman/4.1/en/connector-j-reference-charsets.html

Warning Do not issue the query ‘set names’ with Connector/J,as the
driver will not detect that the character set has changed,and will
continue to use the character set detected during the initial
connection setup.

dawei

【声明】:淮南站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。