Java - saving UTF-8 characters (in particular, Russian characters) in mysql database using PreparedStatement

Using a prepared expression, I try to save Russian characters in a field in certain tables. The table and field use the utf8 character set and utf8_bin sort.

If I manually start the insertion through the IDE or on the command line, the line saves as expected.

INSERT INTO utf8Table VALUES(' ');

Then I can query this table and also return the expected row.

The connection is established through the resource in the tomcat context.xml file with the following configuration:

<Resource 
    name="jdbc/DoolliDB"  
        auth="Container"   
        type="javax.sql.DataSource"   
        driverClassName="com.mysql.jdbc.Driver"
        useUnicode="true"
        characterEncoding="UTF8"
        ....
</Resource>

As I said, I can read strings / characters well, so I assume that both the table and the connection settings are set correctly.

I use PreparedStatement / CallableStatement as follows:

CallableStatement callableStmt = __mySqlConnector.getConnection().prepareCall("INSERT INTO utf8Table VALUES(?)");

callableStmt.setString(1, " ");

callableStmt.executeUpdate();

, :?????????????????

, "" utf-8, ΓΌber, .

System.getProperty("file.encoding") 

java.nio.charset.Charset.defaultCharset().name() 

UTF-8.

.

+5
2

URL- , .

<Resource 
    name="jdbc/DoolliDB"  
        auth="Container"   
        type="javax.sql.DataSource"   
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/dbName?characterEncoding=UTF-8"
        ...
</Resource>

. http://dev.mysql.com/doc/refman/4.1/en/connector-j-reference-charsets.html

" " Connector/J, , , , .

+4

3 sql-, PHP, , :

  • set character_set_results = utf8
  • set character_set_connection = utf8
  • set character_set_client = utf8

:

CallableStatement callableStmt = __mySqlConnector.getConnection().prepareCall("set character_set_results=utf8");
0

All Articles