中文字符无法插入数据库
在mysql终端可以自行插入一条中文信息;
但是在客户端浏览器中输入中文后台出现异常,异常如下:
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not insert: [com.uisf.entity.UisfEmp]; uncategorized SQLException for SQL [insert into alex.uisf_emp (name, emp_no, dept) values (?, ?, ?)]; SQL state [HY000]; error code [1366]; Incorrect string value: '\xC2\x98\xC3\xA5\xC2\xAE...' for column 'name' at row 1; nested exception is java.sql.SQLException: Incorrect string value: '\xC2\x98\xC3\xA5\xC2\xAE...' for column 'name' at row 1
Caused by: java.sql.SQLException: Incorrect string value: '\xC2\x98\xC3\xA5\xC2\xAE...' for column 'name' at row 1
安装数据库设置的编码是utf8;
spring配置hiebernate的配置文件中也设置了utf8格式:
class="org.apache.commons.dbcp.BasicDataSource">
value="com.mysql.jdbc.Driver">
value="jdbc:mysql://localhost:3306/alex?useUnicode=true&characterEncoding=utf-8">
;在struts.xml中也设置了utf8的格式:
这个问题怎么解决啊;
楼主很急啊!
数据库编码
分享到:
------解决方案--------------------
用了spring没有
用了在web.xml里面加上
Set Character Encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
Set Character Encoding
/*
------解决方案--------------------
ajax提交的话,客户端两次编码encodeURI,服务器端一次解码decodeURI
form 提交的话,做一个过滤器将ServletRequest的编码设为utf-8:
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html, charset=utf-8");
response.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
------解决方案--------------------
引用:有一个编码的过滤器;
form提交,从表单接收到的是乱码;
现在就是这个问题,为什么会是乱码
表单页面是什么编码?是不是utf-8?
------解决方案--------------------
引用:Quote: 引用:Quote: 引用:有一个编码的过滤器;
form提交,从表单接收到的是乱码;
现在就是这个问题,为什么会是乱码
表单页面是什么编码?是不是utf-8?
是utf-8
如果form不是post方式提交 改用post方式提交
------解决方案--------------------
引用:在mysql终端可以自行插入一条中文信息;
但是在客户端浏览器中输入中文后台出现异常,异常如下:
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not insert: [com.uisf.entity.UisfEmp]; uncategorized SQLException for SQL [insert into alex.uisf_emp (name, emp_no, dept) values (?, ?, ?)]; SQL state [HY000]; error code [1366]; Incorrect string value: '\xC2\x98\xC3\xA5\xC2\xAE...' for column 'name' at row 1; nested exception is java.sql.SQLException: Incorrect string value: '\xC2\x98\xC3\xA5\xC2\xAE...' for column 'name' at row 1
Caused by: java.sql.SQLException: Incorrect string value: '\xC2\x98\xC3\xA5\xC2\xAE...' for column 'name' at row 1