使用准备好的语句我试图将俄语字符存储在特定表的字段中.表和字段使用utf8字符集和utf8_bin排序规则.
如果我通过IDE或命令行手动运行插入,则字符串将按预期保存.
INSERT INTO utf8Table VALUES('Анатолий Солоницын');
然后,我可以查询该表,并且还可以返回预期的String.
通过tomcat的context.xml文件中的资源设置Connection,其中包含以下配置:
sql.DataSource"
driverClassName="com.MysqL.jdbc.Driver"
useUnicode="true"
characterEncoding="UTF8"
....
正如我所说,我能够很好地读取字符串/字符,所以我假设表和连接设置都正确设置.
我通过以下方式使用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中定义连接编码,而不是在属性中定义.
原文链接:https://www.f2er.com/mysql/433120.htmlsql.DataSource"
driverClassName="com.MysqL.jdbc.Driver"
url="jdbc:MysqL://localhost:3306/dbName?characterEncoding=UTF-8"
...
请参阅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.