最近开始又转向J2EE的开发。很久没用,重新熟悉。
本地环境ubuntu 12.13,mysql5.x,tomcat7.x,struts2.3.15.x,spring3.1.0,hibernate4.1.x
之前一直连线上服务器上测试数据库,没有发现有中文乱码问题。
现在在本地搭建此环境,使用本地数据库,便于调试,但是发现每次对数据库操作,有中文就会出现????乱码。
那现在一步步排除什么原因引起:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8"/>
<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>
<!-- A filter that sets character encoding that is used to decode --> <!-- parameters in a POST request --> <filter> <filter-name>setCharacterEncodingFilter</filter-name> <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <async-supported>true</async-supported> </filter>
mydata.driverClass=com.mysql.jdbc.Driver #mydata.url=jdbc:mysql://localhost:3306/sdk?useUnicode=true&characterEncoding=UTF-8 mydata.url=jdbc:mysql://localhost:3306/sdk?useUnicode=true&characterEncoding=UTF-8 mydata.username=root wshouyouDB.password=xxxx(我遇到的问题是出现上段代码中)
<!-- 读取jdbc.properties配置文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:database-config.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${mydata.driverClass}" /> <property name="jdbcUrl" value="${mydata.url}" /> <property name="user" value="${mydata.username}" /> <property name="password" value="${mydata.password}" />
CREATE TABLE `gamepad_model` ( `edit_id` varchar(50) NOT NULL, `filedata` text, `gamepad_type` varchar(100) NOT NULL, `pic_url` varchar(255) DEFAULT NULL, `sdk_version` varchar(50) NOT NULL, `update_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`edit_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8需要加上:CHARSET=utf8
insert into gamepad_model(edit_id,filedata,gamepad_type,sdk_version) values('test3_测试','ttt','testtt_你好','1');如果没有问题,则排除mysql问题
mydata.url=jdbc:mysql://localhost:3306/sdk?useUnicode=true&characterEncoding=UTF-8一般不在xml中,连接数据库需要加上
useUnicode=true&characterEncoding=UTF-8但是如果在xml中,需要转意一下
原文地址:http://blog.csdn.net/richerg85/article/details/41171625