For some strange reason, I cannot add UTF-8 data to my MySQL database. When I enter a non-Latin character, it is saved as. Everything else is kept in order. So, for example, “this example® ™” is stored in order, but “和 英 辞典” is stored as “????”.
Link to connection in order:
private DataSource getDB() throws PropertyVetoException {
ComboPooledDataSource db = new ComboPooledDataSource();
db.setDriverClass("com.mysql.jdbc.Driver");
db.setJdbcUrl("jdbc:mysql://domain.com:3306/db?useUnicode=true&characterEncoding=UTF-8");
db.setUser("...");
db.setPassword("...");
return db;
}
I use PreparedStatement, as you would expect, I even tried typing "set names utf8" as someone suggested.
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = db.getConnection();
stmt = conn.prepareStatement("set names utf8");
stmt.execute();
stmt = conn.prepareStatement("set character set utf8");
stmt.execute();
... set title...
stmt = conn.prepareStatement("INSERT INTO Table (title) VALUES (?)");
stmt.setString(1,title);
stmt.execute();
} catch (final SQLException e) {
...
The table itself seems beautiful.
Default Character Set: utf8
Default Collation: utf8_general_ci
...
Field title:
Type text
Character Set: utf8
Collation: utf8_unicode_ci
I tested it by typing in Unicode ("和 英 辞典" specifically) using a graphical editor, and then selecting from a table - and it was returned just fine. So this seems like a problem with JDBC.
What am I missing?