2013年10月12日星期六

Chinese characters can not be inserted into the database

In mysql terminal can insert a Chinese information ;
But in the client browser, enter the Chinese back abnormal , abnormal as follows:

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&amp;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>

how to solve this problem ah ;
landlord impatient ah !
------ Solution ---------------------------------------- ----
with a spring without
spent inside with in the 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>

------ Solution ------------------------------------- -------
ajax submitted , the client twice coding encodeURI, once decoded decodeURI server
form submission , then do a filter will ServletRequest encoding is set to 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);
}

------ Solution ------------------------------------- -------


What are coded form page ? Is not utf-8?
------ Solution ---------------------------------- ----------

    
What are coded form page ? Is not utf-8?          
is utf-8  

If the form is not submitted to switch to post post submission
------ Solution --------------------- -----------------------
not the root cause of this problem can not be inserted , is your backstage received Chinese string without transcoding cause long ( exceeds the length of the field in the table ) , you first string into Chinese and then insert enough
------ Solution ------------------ --------------------------

You say is very insightful   
my side database no problem , you can insert the Chinese data in the terminal   
changed the filter, the filter takes effect   
parameters received from the form is not garbled   
But it is inserted into the database garbled  

you can insert Chinese data in the terminal , does not mean your database is utf8 encoding
look at the database encoding it, inserted into the database is basically a database encoding garbled question of
------ Solution ------------------ --------------------------

You say is very insightful       
my side database no problem , you can insert the Chinese data in the terminal       
changed the filter, the filter takes effect       
parameters received from the form is not garbled       
But it is inserted into the database garbled                
    
you can insert Chinese data in the terminal , does not mean your database is utf8 encoding     
look at the database encoding it, inserted into the database is a distortion of the basic problem is the database code          
I installed the database when setting really is utf-8  

This can be hard to say , mysql in coding is very pit father , completely re- set the best again
------ For reference only -------------- -------------------------
used, in web.xml also added this filter ;
program:

<!-- 编码过滤器start -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.uisf.util.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- end -->
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
HttpServletResponse response = (HttpServletResponse) resp;
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragrma", "no-cache");
response.setDateHeader("Expires", 0);
chain.doFilter(req, resp);
System.out.println("調用編碼過濾器");
}

very difficult to understand . . .
------ For reference only -------------------------------------- -
debug debug it to see is the parameter passed to the background or background into the database garbled garbled
parameters of how you become the a \ xC2 \ x98 \ xC3 \ xA5 \ xC2 \ xAE ...?

------ For reference only ---------------------------------- -----
debug tried ;
received parameter is garbled ;
garbled characters in length exceeds 3 on the plug does not go in ;
it is unclear what piece out of the question
------ For reference only ------------------------- --------------

Since it is so , the reasons may be:
1 of your front page is not utf-8 encoding ?
2 struts configuration of your code is not correct or is not utf-8
3 there is a possibility that your database encoding is not utf-8
------ For reference only ------------------- --------------------
a coding filter ;
form submission received from the form is garbled ;
Now is the problem , why is garbled
------ For reference only ------------------------- --------------

  
What are coded form page ? Is not utf-8?  
is utf-8
------ For reference only ------------------------------ ---------
into this try:
<!--解决Form提交乱码问题  -->
<filter>
<filter-name>encodingFilter</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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

------ For reference only ----------------------------------- ----

this method with your
forms received parameter not garbled
But why is inserted into the database or distortion
------ For reference only -------------------------- -------------
first with httpwatch facie transmission parameters of the past , is not already garbled ( garbled words , JSP page coding right ) . Then in the background debug , they look is not garbled ( garbled words , filter does not work. Struts2 the filter into the filter before ) . Finally the database is garbled , then, is the problem of database
------ For reference only --------------------------- ------------

UisfEmp emp=new UisfEmp();
emp.setName(name);
emp.setEmpNo(empNo);
emp.setDept(dept);
System.out.println("获取到的name参数"+emp.getName());
empservice.saveEmp(emp);
此处输出的name不是乱码
但是还是报这个异常:
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: '\xE6\xBD\x98\xE5\xAE\x81' for column 'name' at row 1; nested exception is java.sql.SQLException: Incorrect string value: '\xE6\xBD\x98\xE5\xAE\x81' for column 'name' at row 1

------ For reference only -------------------- -------------------

You say is very insightful
my side database no problem , you can insert the Chinese data in the terminal
changed the filter, the filter takes effect
parameters received from the form is not garbled
But it is inserted into the database garbled
------ For reference only -------------------------- -------------
normal now , what the database character set
------ For reference only --------------- ------------------------

You say is very insightful     
my side database no problem , you can insert the Chinese data in the terminal     
changed the filter, the filter takes effect     
parameters received from the form is not garbled     
But it is inserted into the database garbled          
  
you can insert Chinese data in the terminal , does not mean your database is utf8 encoding   
look at the database encoding it, inserted into the database is a distortion of the basic problem is the database code  
I installed the database when setting really is utf-8
------ For reference only ---------------------- -----------------

You say is very insightful   
my side database no problem , you can insert the Chinese data in the terminal   
changed the filter, the filter takes effect   
parameters received from the form is not garbled   
But it is inserted into the database garbled   Landlord, I say a more incisive :















to the sub- ah ! ! ! !
------ For reference only -------------------------------------- -

You say is very insightful     
my side database no problem , you can insert the Chinese data in the terminal     
changed the filter, the filter takes effect     
parameters received from the form is not garbled     
But it is inserted into the database garbled        Landlord, I say a more incisive :   
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
to the sub- ah ! ! ! !   . . Points to the wrong ah sorry. .

没有评论:

发表评论