Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 */
like image 591
jabal Avatar asked Dec 10 '25 18:12

jabal


1 Answers

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.

like image 125
jabal Avatar answered Dec 13 '25 08:12

jabal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!