使用JDBC连接器5.1从Java读取/写入MySQL中的UTF-8数据时出现问题 [英] Problems reading/writing UTF-8 data in MySQL from Java using JDBC connector 5.1

查看:156
本文介绍了使用JDBC连接器5.1从Java读取/写入MySQL中的UTF-8数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个MySQL数据库(UTF-8)的场景,一个Java代码(一个定时服务),它同步两个数据库(首先读取表单并写入/更新到第二个)和一个允许修改的Web应用程序数据加载到第二个数据库中。

I have a scenario with two MySQL databases (in UTF-8), a Java code (a Timer Service) that synchronize both databases (reading form first of them and writing/updating to second) and a Web application that lets modify data loaded in the second database.

所有数据库访问都是使用IBATIS进行的(但是我发现使用JDBC时遇到了同样的问题, PreparedStatement s和 ResultSet s)

All database access is made using IBATIS (but I detect that I have the same problem using JDBC, PreparedStatements and ResultSets)

当我的java代码从第一个数据库读取数据时,我获得了像<$ c $这样的字符c>'³'当它真的必须'ó'。这些数据是在没有修改第二个数据库的情况下编写的。

When my java code reads data from first database, I obtain characters like 'ó' when really it must be 'ó'. This data is wroten without modifications to the second database.

稍后,当我在我的网络应用程序中看到加载的数据时,尽管< meta http-equiv =Content,我看到了外部字符-Typecontent =text / html; charset = utf-8/>

Later, when I see the loaded data in my web application, I see the extrange character despite the <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />.

如果我使用...解码数据

If I decode the data using ...

new String(data.getBytes("UTF-8"));

...我正确地想象了角色(ó)。但我不能将此解决方案作为一般规则使用,因为当我使用Web应用程序表单修改数据时,我的第二个数据库中的数据不会以UTF-8更新(尽管数据库是UTF-8且我的连接字符串使用了字符编码, characterSetResults和useUnicode参数)。

... I visualize correctly the character (ó). But I can not use this solution as a general rule because when I modify data using web application form, the data is not updated in UTF-8 in my second database (despite the database is UTF-8 and my connection string is using characterEncoding, characterSetResults and useUnicode parameters).

从我的Java代码中我获得以下数据库设置:

From my Java code I obtain the following Database settings:

character_set_client-->utf8 
character_set_connection-->utf8 
character_set_database-->utf8 
character_set_filesystem-->binary 
character_set_results-->utf8 
character_set_server-->latin1 
character_set_system-->utf8 
character_sets_dir-->/usr/local/mysql51/share/mysql/charsets/ 

character_set_server设置无法更改,我不知道我做错了什么!!

the character_set_server setting can't be changed and I don't know what I am doing wrong!!

如何使用JDBC连接器从MySQL读取UTF-8数据( mysql-connector-java-5.1.5-bin.jar )?

How can I read UTF-8 data from MySQL using JDBC connector (mysql-connector-java-5.1.5-bin.jar)?

从第一个数据库读取数据或写入第二个数据库是否有问题?

Is the problem with reading data from the first database or writing to the second database?

推荐答案

稍晚但这会对你有所帮助:

A little late but this will help you:

DriverManager.getConnection(
           "jdbc:mysql://" + host + "/" + dbName 
           + "?useUnicode=true&characterEncoding=UTF-8", user, pass);

这篇关于使用JDBC连接器5.1从Java读取/写入MySQL中的UTF-8数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆