Reputation: 551
I have a problem to insert Japanese characters with my Java application in MySQL.
If I insert this string "今日は", I get this "???".
My table and it's fields uses UTF-8. I don't think that the table structure is involved, because while I insert the same string from PHP, it works.
CREATE TABLE `pool` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`message` varchar(160) COLLATE utf8_bin NOT NULL DEFAULT ''
PRIMARY KEY (`id`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
I'm connecting to the databose with the lib "BoneCPConfig" who manage for me a pool of connexion :
Class.forName("com.mysql.jdbc.Driver");
Properties props = new Properties();
props.setProperty("characterEncoding", "UTF-8");
props.setProperty("useUnicode", "true");
BoneCPConfig config = new BoneCPConfig(props);
config.setJdbcUrl("jdbc:mysql://" + MYSQL_HOST_NAME + "/" + MYSQL_BASE_NAME);
config.setMinConnectionsPerPartition(MIN_CONNEXION_PER_PARTITION);
config.setPartitionCount(PARTITION_COUNT);
config.setUsername(MYSQL_LOGIN);
config.setPassword(MYSQL_PASSWORD);
I do my insert like this :
String query = "INSERT INTO pool (message) VALUES(?) ";
PreparedStatement insertStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
insertStatement.setString(1, "今日は");
insertStatement.execute();
I think my problem is in the connexion configuration, but I don't see what to do :-/ Have you an idea ?
problem solved, see below
Upvotes: 2
Views: 2395
Reputation: 551
The problem was the lib BoneCP who don't use the property passed in parameters... I it by replacing :
Properties props = new Properties();
props.setProperty("characterEncoding", "UTF-8");
props.setProperty("useUnicode", "true");
BoneCPConfig config = new BoneCPConfig(props);
config.setJdbcUrl("jdbc:mysql://" + MYSQL_HOST_NAME + "/" + MYSQL_BASE_NAME);
with :
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl("jdbc:mysql://" + MYSQL_HOST_NAME + "/" + MYSQL_BASE_NAME + "?user=" + MYSQL_LOGIN + "&password=" + MYSQL_PASSWORD + "&characterEncoding=utf-8");
Upvotes: 4