加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

Java – 使用PreparedStatement在mysql数据库中存储UTF-8字符(

发布时间:2020-12-11 23:47:19 所属栏目:MySql教程 来源:网络整理
导读:使用准备好的语句我试图将俄语字符存储在特定表的字段中.表和字段使用utf8字符集和utf8_bin排序规则. 如果我通过IDE或命令行手动运行插入,则字符串将按预期保存. INSERT INTO utf8Table VALUES('Анатолий Солоницын'); 然后,我可以查询该表

使用准备好的语句我试图将俄语字符存储在特定表的字段中.表和字段使用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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读