H
howachen
Hi,
I use java to connect to MySQL (5.x), using the latest connector.
I have declared the character setting in the connection:
//-----------------------------------------------------------------------------
Properties props = new Properties();
props.put("characterEncoding", "UTF-8");
props.put("useUnicode", "true");
props.put("user", "root");
props.put("password", "password");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://" + "127.0.0.1" + "/"
+ "test_db";
this.setConnection(DriverManager.getConnection(dbUrl, props));
//-----------------------------------------------------------------------------
in the query part
//-----------------------------------------------------------------------------
String sqlstr = "SELECT * FROM test_table WHERE id = 8";
try {
PreparedStatement s = this.getConnection().prepareStatement(sqlstr);
ResultSet rs = s.executeQuery();
while (rs.next()) {
String text1Val = "";
String text2Val = "";
text1Val = rs.getString("text1"); // THIS DOES NOT WORK
try {
text2Val = new String(rs.getString("text1").getBytes(),
"UTF-8"); // THIS WORK!
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
}
.....
//----------------------------------------
the value storing in "text1" field is a UTF-8 character.
Why need this kind of overhead in character conversion when using java?
thanks...
I use java to connect to MySQL (5.x), using the latest connector.
I have declared the character setting in the connection:
//-----------------------------------------------------------------------------
Properties props = new Properties();
props.put("characterEncoding", "UTF-8");
props.put("useUnicode", "true");
props.put("user", "root");
props.put("password", "password");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://" + "127.0.0.1" + "/"
+ "test_db";
this.setConnection(DriverManager.getConnection(dbUrl, props));
//-----------------------------------------------------------------------------
in the query part
//-----------------------------------------------------------------------------
String sqlstr = "SELECT * FROM test_table WHERE id = 8";
try {
PreparedStatement s = this.getConnection().prepareStatement(sqlstr);
ResultSet rs = s.executeQuery();
while (rs.next()) {
String text1Val = "";
String text2Val = "";
text1Val = rs.getString("text1"); // THIS DOES NOT WORK
try {
text2Val = new String(rs.getString("text1").getBytes(),
"UTF-8"); // THIS WORK!
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
}
.....
//----------------------------------------
the value storing in "text1" field is a UTF-8 character.
Why need this kind of overhead in character conversion when using java?
thanks...