日期:2014-05-20  浏览次数:20693 次

JDBC抛出的异常信息乱码
java使用JDBC连接Mysql,抛出的异常信息乱码:Duplicate entry '??-???' for key 'UserIdx'
期望的异常信息为:Duplicate entry '中国' for key 'UserIdx'
mysql编码设置如下:

包括my.ini文件设置为了utf8

java代码:
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3341/hibernate3.3.2?useUnicode=true&characterEncoding=utf8", "root", "sa");
statement= connection.createStatement();
String sql = "insert into User(UserId,Password, RecId) values('中国','123',126453)";
statement.executeQuery("set names 'utf8'");
statement.executeUpdate(sql);

} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}finally{
try {
if (rs != null) {
rs.close();
rs = null;
}
if (statement != null) {
statement.close();
statement = null;
}
if (connection != null) {
connection.close();
connection = null;
}
} catch (Exception e2) {
e2.printStackTrace();
}

}

java文件也是utf-8的格式

请大神指教

------解决方案--------------------
把连接串改成这种格式试试
jdbc\:mysql\://localhost\:3306/house_manage?useUnicode\=true&characterEncoding\=utf-8