jdbc-mysql Chinese garbled solve

When a client or JDBC connection support Chinese custom encoding format (usually UTF-8), insert such data, let mysql is automatically transcoded to us, there are two possible ways:

1, if the operation by the DriverManager.getConnection JDBC (url) encoding, may be added in the url of JDBC useUnicode = true & characterEncoding = UTF-8 solution garbled

jdbc.url=jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8

2, if by other data sources, such as DBCP, tomcat-jdbc, c3p0, spring-jdbc, hibernate reads the configuration file, additional useUnicode in the url = true & characterEncoding = UTF-8 does not work, but by the data source its configuration to take effect, such as the following configurations:

<!-- Tomcat data source -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
 <property name="driverClassName" value="${jdbc.driverClassName}" />
 <property name="url" value="${jdbc.url}" />
 <property name="username" value="${jdbc.username}" />
 <property name="password" value="${jdbc.password}" />
 <property name="dbProperties">
   <props>
       <prop key="useUnicode">yes</prop>
       <prop key="characterEncoding">utf8</prop>
   </props>
 </property>
 <!-- Configuration refer to optimizing connection performance -->
 <property name="initialSize" value="10" />
 <property name="maxActive" value="100" />
 <property name="maxIdle" value="50" />
 <property name="minIdle" value="10" />
 <property name="suspectTimeout" value="60" />
 <property name="timeBetweenEvictionRunsMillis" value="30000" />
 <property name="minEvictableIdleTimeMillis" value="60000" />
 <property name="testOnBorrow" value="true" />
 <property name="validationQuery" value="SELECT 1" />
 <property name="validationInterval" value="30000" />
 <!-- End Configuration refer to optimizing connection performance -->
</bean>

among them:

<props>
     <prop key="useUnicode">yes</prop>
     <prop key="characterEncoding">utf8</prop>
</props>

UseUnicode equivalent to the url = true & characterEncoding = UTF-8

Guess you like

Origin www.cnblogs.com/xiangyuguan/p/11306693.html
Recommended