[color=red]数据库配置:[/color]
java向已经设置好utf8编码的mysql数据库写入中文时乱码,如何解决?
难道java不是默认就以utf-8编码写入数据库的吗?其实解决这个很简单,只要修改下连接数据库的字符串就可以了.
原来连接数据库的字符串是:
jdbc.url=jdbc:mysql://localhost:3306/test
只要改为
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
中文乱码问题就可以解决
[color=red]web.xml 中struts配置:[/color]
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
<!-- struts-default.xml,struts-plugin.xml, -->
</init-param>
[color=red]<init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>[/color]
</filter>
java向已经设置好utf8编码的mysql数据库写入中文时乱码,如何解决?
难道java不是默认就以utf-8编码写入数据库的吗?其实解决这个很简单,只要修改下连接数据库的字符串就可以了.
原来连接数据库的字符串是:
jdbc.url=jdbc:mysql://localhost:3306/test
只要改为
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
中文乱码问题就可以解决
[color=red]web.xml 中struts配置:[/color]
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
<!-- struts-default.xml,struts-plugin.xml, -->
</init-param>
[color=red]<init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>[/color]
</filter>