简体   繁体   中英

mysql character encoding

I have a MySQL server with the following 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

I have a Java client that connects to this db using DBCP with this config:

 <bean id="dsJoomla" class="hu.eutrust.wsfresh.CustomDataSource">
        <property name="driverClassName" value="${joomla.db.driver}"/>
        <property name="username" value="${joomla.db.user}"/>
        <property name="password" value="${joomla.db.pass}"/>
        <property name="url" value="${joomla.db.url}"/>
        <property name="connectionProperties" value="characterEncoding=UTF-8;useUnicode=true;"/>
    </bean>

Then I perform somewhere in the code an update like this:

template.update("insert into test (nev) values (:nev)", new MapSqlParameterSource("nev", "Árvíztűrő"));

After I check the results in phpMyAdmin I see a row:

ID  NEV
2 Árvízt?r?

The ő and ű characters that are not in the latin1 set are bad. I suppose this is because the character_set_server is latin1. But using phpMyAdmin I can manually edit the record and enter 'Árvíztűrő' and after that the record is correct. So I suppose it is possible to enter the desired value into the database with these settings. How can I do it with my Java client? How shall I configure the connection? Why do my ő and ű chars go wrong if the connection if 100% utf8?

show create table test output is:

CREATE TABLE `test` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `NEV` varchar(64) NOT NULL,
 PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

show create database joomla output is:

CREATE DATABASE `joomla` /*!40100 DEFAULT CHARACTER SET utf8 */

The connection properties set through the DBCP connection pool bean were not treated as appending them to the JDBC url.

So finally appending ?characterEncoding=UTF-8&useUnicode=true to the connection url helped me out.

If you're using Eclipe, did you check your encoding:

  • Window -> Preference -> General -> workspace
  • right click at project -> property -> resource

changing it to UTF-8 may help.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM