亲测有效
转自:https://blog.csdn.net/top_code/article/details/8538639
今天在一台新的CentOS机器上使用c3p0连接池操作mysql数据库出现中文乱码问题,具体表现为:查询时无中文乱码问题,写数据时中文乱码,查看了机器上数据库字符集也是UTF8,应该不会出现中文乱码才对,最后在c3p0配置文件中 jdbcUrl后加上:?useUnicode=true&characterEncoding=UTF8 中文就不会乱码了。
原始c3p0-config.xml
-
<?xml version=“1.0” encoding=“UTF-8”?>
-
<c3p0-config>
-
-
<!– This app is massive! 线上:data_production 线下:datadev –>
-
<named-config name=“pic_download”>
-
<property name=“driverClass”>com.mysql.jdbc.Driver
</property>
-
<property name=“jdbcUrl”>jdbc:mysql://ricky.test.com:3306/data_production
</property>
-
<property name=“user”>bingbing
</property>
-
<property name=“password”>bingbing
</property>
-
<property name=“initialPoolSize”>15
</property>
-
<property name=“maxIdleTime”>30
</property>
-
<property name=“maxPoolSize”>20
</property>
-
<property name=“minPoolSize”>5
</property>
-
<property name=“maxStatements”>2000
</property>
-
</named-config>
-
</c3p0-config>
修改后的c3p0-config.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<c3p0-config>
-
-
<!-- This app is massive! 线上:data_production 线下:datadev -->
-
<named-config name="pic_download">
-
<property name="driverClass">com.mysql.jdbc.Driver
</property>
-
<property name="jdbcUrl">jdbc:mysql://ricky.test.com:3306/data_production?useUnicode=true&characterEncoding=UTF8
</property>
-
<property name="user">bingbing
</property>
-
<property name="password">bingbing
</property>
-
<property name="initialPoolSize">15
</property>
-
<property name="maxIdleTime">30
</property>
-
<property name="maxPoolSize">20
</property>
-
<property name="minPoolSize">5
</property>
-
<property name="maxStatements">2000
</property>
-
</named-config>
-
</c3p0-config>
注意由于我使用的XML配置文件,所以 &需要转义为 &
OK,搞定!