您的位置:首页 > 数据库 > MySQL

servlet+mysql传入中文乱码

2016-05-13 22:58 417 查看
之前不管怎么该JSP和servlet的编码格式,从网页的表单传入中文的时候,录入到数据库都会乱码,变成?????

之后发现,不是网页编码格式的问题,要在数据源那里,connection的时候在URL后面增加?characterEncoding=utf8才能解决问题

原来的是public Connection getConnection(){
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mytest";
String username="root";
String password="";
conn = DriverManager.getConnection(url, username, password);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
在URL后面加上编码格式之后是public Connection getConnection(){
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mytest?characterEncoding=utf8";
String username="root";
String password="";
conn = DriverManager.getConnection(url, username, password);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}就解决问题了,中文正常了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: