本人用junit写了一个测试类,往mysql中写入数据。代码如下:
spring配置文件:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${driverClassName}" />
<property name="jdbcUrl" value="${url}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
</bean> <bean id="dblog" class="com.essp.uas.dao.db.DBLog">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/uas?useUnicode=true&characterEncoding=utf-8
username=root
password=
initialSize=1
maxActive=10
maxIdle=2
minIdle=1package com.essp.uas.test;import java.sql.Types;import javax.annotation.Resource;import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.essp.uas.dao.ILog;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( { "/applicationContext-db.xml" })
public class DBTestUnit { @Resource
private ILog log; @Before
public void init() {
} @After
public void destory() {
} @Test
public void testMidifyLdapAttrbute() {
Object[] args = new Object[] {
"137031112",
"门户",
"1365336825896",
"中文",
"2013-06-05 01:16:15",
"192.168.10.118",
"1370363952868",
"7C0307F32418DC717FE3777BE0C2ACB8",
"org,湖北省"
};
int[] argstypes = new int[] {
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR
};
log.writelog(args, argstypes);
}}以上是java端的代码,可运行后写入mysql发现中文都是??
我的mysql字符集设置如下:
collation_connection utf8_general_ci
collation_database utf8_general_ci
collation_server utf8_general_ci
建表语句
`table_name` (
`ID` varchar(20) CHARACTER SET utf8 NOT NULL DEFAULT '',
`AppName` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`USERID` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
`USER_NAME` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
`CREATE_TIME` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
`IP` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`APPID` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
`TOKEN` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
`Department` varchar(60) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8我能想到的所有地方都改为utf-8了,问题还是没解决。请教玩mysql的童鞋
spring配置文件:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${driverClassName}" />
<property name="jdbcUrl" value="${url}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
</bean> <bean id="dblog" class="com.essp.uas.dao.db.DBLog">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/uas?useUnicode=true&characterEncoding=utf-8
username=root
password=
initialSize=1
maxActive=10
maxIdle=2
minIdle=1package com.essp.uas.test;import java.sql.Types;import javax.annotation.Resource;import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.essp.uas.dao.ILog;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( { "/applicationContext-db.xml" })
public class DBTestUnit { @Resource
private ILog log; @Before
public void init() {
} @After
public void destory() {
} @Test
public void testMidifyLdapAttrbute() {
Object[] args = new Object[] {
"137031112",
"门户",
"1365336825896",
"中文",
"2013-06-05 01:16:15",
"192.168.10.118",
"1370363952868",
"7C0307F32418DC717FE3777BE0C2ACB8",
"org,湖北省"
};
int[] argstypes = new int[] {
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR
};
log.writelog(args, argstypes);
}}以上是java端的代码,可运行后写入mysql发现中文都是??
我的mysql字符集设置如下:
collation_connection utf8_general_ci
collation_database utf8_general_ci
collation_server utf8_general_ci
建表语句
`table_name` (
`ID` varchar(20) CHARACTER SET utf8 NOT NULL DEFAULT '',
`AppName` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`USERID` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
`USER_NAME` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
`CREATE_TIME` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
`IP` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`APPID` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
`TOKEN` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
`Department` varchar(60) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8我能想到的所有地方都改为utf-8了,问题还是没解决。请教玩mysql的童鞋
这个错了把,
url=jdbc:mysql://localhost:3306/uas?useUnicode=true&characterEncoding=utf-8
应该是这样!还有密码最好不要为空
问题的原因是连接mysql参数有误。
&是xml中的&写法
在properties文件中直接写&即可。这个问题可以结束了,希望有兴趣的童鞋帮忙看看昨天我题的一个类似的问题,是关于mysql中文查询方面的,连接地址如下:http://bbs.csdn.net/topics/390507630?page=1#post-394953021