ssh框架中插入数据到mysql出现乱码

ssh框架中插入数据到mysql出现乱码

分类: SSH有关 200人阅读 评论(0) 收藏 举报

目录(?)[+]

  1. 再次列出能找的解决办法:
  2. 连接数据库的时候
  3. jdbc.properties: jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=utf8 applicationContext.xml: jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=utf8
  4. 设置mysql字符集
  5. show variables like'character_set_%';
  6. 把显示的character_set_**全都设置成utf8,即是:set character_set_serve='utf8'
  7. show variables like '%collation%';
  8. 把显示的都设置成为如下:set collation_server='utf8_general_ci'
  9. 修改mysql配置文件my.ini
  10. default-character-set=utf8
  11. 建一个过滤类
  12. package com.broadengate.crm.filter;
  13. import java.io.IOException;
  14. import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse;
  15. public class CharacterEncodingFilter implements Filter {
  16. public void destroy() { }
  17. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); chain.doFilter(request, response); }
  18. public void init(FilterConfig arg0) throws ServletException { }
  19. }
  20. web.xml配置加 encodefilter com.broadengate.crm.filter.CharacterEncodingFilter encodefilter /*
  21. 修改tomcat中的配置文件server.xml
  22. 改为
  23. 在web.xml配置spring过滤器
  24. char
  25. org.springframework.web.filter.CharacterEncodingFilter
  26. encoding
  27. UTF-8
  28. char
  29. /*

再次列出能找的解决办法:

1、连接数据库的时候

jdbc.properties: jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=utf8
applicationContext.xml: jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=utf8

2、设置mysql字符集

show variables like'character_set_%';

把显示的character_set_**全都设置成utf8,即是:set character_set_serve='utf8'

show variables like '%collation%';

把显示的都设置成为如下:set collation_server='utf8_general_ci'

3、修改mysql配置文件my.ini

default-character-set=utf8

4、建一个过滤类

package com.broadengate.crm.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharacterEncodingFilter implements Filter {

public void destroy() {
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}

public void init(FilterConfig arg0) throws ServletException {

}

}

web.xml配置加
<filter>
<filter-name>encodefilter</filter-name>
<filter-class>com.broadengate.crm.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodefilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

5、修改tomcat中的配置文件server.xml

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />

改为

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>

6、在web.xml配置spring过滤器

<filter>

<filter-name>char</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>char</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

再次列出能找的解决办法:

1、连接数据库的时候

jdbc.properties: jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=utf8
applicationContext.xml: jdbc:mysql://localhost:3306/myproject?useUnicode=true&amp;characterEncoding=utf8

2、设置mysql字符集

show variables like'character_set_%';

把显示的character_set_**全都设置成utf8,即是:set character_set_serve='utf8'

show variables like '%collation%';

把显示的都设置成为如下:set collation_server='utf8_general_ci'

3、修改mysql配置文件my.ini

default-character-set=utf8

4、建一个过滤类

package com.broadengate.crm.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharacterEncodingFilter implements Filter {

public void destroy() {
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}

public void init(FilterConfig arg0) throws ServletException {

}

}

web.xml配置加
<filter>
<filter-name>encodefilter</filter-name>
<filter-class>com.broadengate.crm.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodefilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

5、修改tomcat中的配置文件server.xml

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />

改为

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>

6、在web.xml配置spring过滤器

<filter>

<filter-name>char</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>char</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

你可能感兴趣的:(mysql)