首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

JDBC抛出的错误信息乱码

2013-11-30 
JDBC抛出的异常信息乱码java使用JDBC连接Mysql,抛出的异常信息乱码:Duplicate entry ??-??? for key Us

JDBC抛出的异常信息乱码
java使用JDBC连接Mysql,抛出的异常信息乱码:Duplicate entry '??-???' for key 'UserIdx'
期望的异常信息为:Duplicate entry '中国' for key 'UserIdx'
mysql编码设置如下:
JDBC抛出的错误信息乱码
包括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

热点排行