Hello,
first of all, thanks for the answers.
I can't explain what it's happening in my program.
When my application starts, the file.encoding parameter value is "UTF-8" and the defaultCharset value is "UTF-8". I obtain those values using:
System.getProperty("file.encoding");
java.nio.charset.Charset.defaultCharset());
However, when I read a UTF-8 database value, I obtain characters like 'ó'. But I know that those characters are UTF-8 valid because when I use String constructor with "UTF-8" charset encoding, I see the right character.
If I change the file.encoding parameter value using -Dfile.encoding=ISO-8859-1, and I read the same database value, I see the right character!!!, but if execute a select like ...
SELECT 'ó' FROM DUAL
... I see an extrange value for that character.
If I modify the select query like this...
SELECT _latin1'ó' FROM DUAL
...I see the right character because I'm telling to Mysql parser that 'ó' has latin1 encoding. But if I execute System.out.println("ó"), I see a wrong value.
I can´t explain this behavior.
This is my test program
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
String userName = "user";
String password = "password";
Properties prop = new Properties();
prop.put("user", userName);
prop.put("password", password);
String url = "jdbc:mysql://localhost/database?characterSetResults=UTF-8&characterEncoding=UTF-8&useUnicode=yes";
Class.forName ("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection (url, prop);
ps = conn.prepareStatement("show variables like '%character%'");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1) +"-->"+rs.getString(2));
}
rs.close();
ps.close();
System.out.println("-----------------------------");
System.out.println("ARGS[0]:"+args[0]);
System.out.println("-----------------------------");
System.out.println("JVM DEFAULT CHARSET:"+java.nio.charset.Charset.defaultCharset());
System.out.println("-----------------------------");
System.out.println("JVM file.encoding:"+System.getProperty("file.encoding"));
System.out.println("-----------------------------");
//THE QUERY
String query = "select 'ó','ó' from dual";
ps = conn.prepareStatement(query);
rs = ps.executeQuery();
rs.next();
System.out.println("FIELD1:"+rs.getString(1));
System.out.println("FIELD2:"+rs.getString(2));
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (rs != null) {
try
{
rs.close ();
}
catch (Exception e) { /* ignore close errors */ }
}
if (ps != null) {
try
{
ps.close ();
}
catch (Exception e) { /* ignore close errors */ }
}
if (conn != null)
{
try
{
conn.close ();
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
If I execute the program passing as args[0] the 'ó' value...
this is the output with UTF-8 file.encoding:
character_set_client-->utf8
character_set_connection-->utf8
character_set_database-->utf8
character_set_filesystem-->binary
character_set_results-->utf8
character_set_server-->latin1
character_set_system-->utf8
character_sets_dir-->/usr/local/mysql51/share/mysql/charsets/
-----------------------------
ARGS[0]:ó
-----------------------------
JVM DEFAULT CHARSET:UTF-8
-----------------------------
JVM file.encoding:UTF-8
-----------------------------
FIELD1:ó
FIELD2:ó
And this is the output using ISO-8859-1
character_set_client-->utf8
character_set_connection-->utf8
character_set_database-->utf8
character_set_filesystem-->binary
character_set_results-->utf8
character_set_server-->latin1
character_set_system-->utf8
character_sets_dir-->/usr/local/mysql51/share/mysql/charsets/
-----------------------------
ARGS[1]:¦
-----------------------------
JVM DEFAULT CHARSET:ISO-8859-1
-----------------------------
JVM file.encoding:ISO-8859-1
-----------------------------
FIELD1:¦
FIELD2:ó
Thanks in advance