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;
}就解决问题了,中文正常了!
之后发现,不是网页编码格式的问题,要在数据源那里,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;
}就解决问题了,中文正常了!
相关文章推荐
- win10 Mysql server 安装
- MySQL 添加列+修改列+删除列
- 记忆中干过的mysql卸载和重装
- mysql索引优化
- Mysql-proxy 读写分离中间组件
- MySql获取数据库大小
- ubuntu下mysql安装及基本操作
- MySQL(1)
- windows(64) install mysql
- 基于MySQL实现按距离排序、范围查找
- MySQL中的ON DUPLICATE KEY UPDATE
- MySQL多表连接查询
- mysql 忘记root密码找回方法
- MySQL(23):事务的隔离级别出现问题之 脏读
- MySQL(22):事务管理之 事务回滚
- MySQL(21):事务管理之 事务提交
- mysql 创建用户与授权
- MySQL(20):事务简介 和 事务的四个特性
- mysql tmp_table_size和max_heap_table_size大小配置
- mysql 注释符号