Niv
Niv

Reputation: 11

MySql Jdbc Connector - localization issue

I am trying to write a simple Java client program that uses koi8r as its character set, and keep on failing.

Class.forName("com.mysql.jdbc.Driver");
Connection conn2 = DriverManager.getConnection ("jdbc:mysql://localhost:3306/test","root",null);
Statement stmt = conn2.createStatement();
int result;
result = stmt.executeUpdate("SET CHARACTER SET koi8r");

stmt = conn2.createStatement();
result = stmt.executeUpdate("DROP TABLE IF EXISTS װֱֱֲֳֹּ, t1, t2");
stmt.close();
assertEquals(0, result);

I'm getting

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '???????, t1, t2' at line 1

When I put these commands in a script file and execute them using MySql client it works fine

SET CHARACTER SET koi8r
DROP TABLE IF EXISTS װֱֱֲֳֹּ, t1, t2

I sniffed the network and I saw the the jdbc connector sends it with ?????? to the server, so I guess I'm missing some setting to the connection. Actually I tried (setEncoding, setCharactersEncoding, setConncetionCollation ...), but still failed.

Upvotes: 1

Views: 1600

Answers (1)

BalusC
BalusC

Reputation: 1109272

You need to tell the JDBC driver which encoding it should use to transfer the characters over network. It namely defaults to platform default encoding which is in your case apparently not UTF-8.

You can do this by adding the following two query parameters to the JDBC URL:

jdbc:mysql://localhost:3306/test?useUnicode=yes&characterEncoding=UTF-8

See also the documentation.

Upvotes: 3

Related Questions