Using a prepared statement I am trying to store Russian characters in a field on a particular tables. The table and field is using the utf8 character set and utf8_bin collation.
If I manually run an insert via an IDE or command line, the string saves as expected.
INSERT INTO utf8Table VALUES('Анатолий Солоницын');
I am then able to query that table and can get back the expected String as well.
The Connection is setup through a resource in tomcat’s context.xml file with the following config:
<Resource
name="jdbc/DoolliDB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
useUnicode="true"
characterEncoding="UTF8"
....
</Resource>
As I said, I am able to read the string/characters just fine, so I assume that both the table and the connection settings are set properly.
I am using a PreparedStatement/CallableStatement in the following way:
CallableStatement callableStmt = __mySqlConnector.getConnection().prepareCall("INSERT INTO utf8Table VALUES(?)");
callableStmt.setString(1, "Анатолий Солоницын");
callableStmt.executeUpdate();
Instead of Анатолий Солоницын, what is getting inserted into the database is: ???????? ?????????
Also note that “normal” utf-8 strings, such as über, are getting saved
properly.
Both
System.getProperty("file.encoding")
and
java.nio.charset.Charset.defaultCharset().name()
are also both returning UTF-8.
Any help is appreciated. Thanks
You need to define the connection encoding in the database url, not in the attribute.
Please see http://dev.mysql.com/doc/refman/4.1/en/connector-j-reference-charsets.html