在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格式:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/alex?useUnicode=true&characterEncoding=utf-8">
</property>
<property name="username" value="root">
</property>
<property name="password" value="love0824">
</property>
</bean>;
在struts.xml中也设置了utf8的格式:
<constant name="struts.i18n.encoding" value="utf-8"></constant>
这个问题怎么解决啊;
楼主很急啊!
数据库编码
------解决方案--------------------
用了spring没有
用了在web.xml里面加上
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
------解决方案--------------------
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);
}
------解决方案--------------------
表单页面是什么编码?是不是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