java Oracle Chinese garbled solution: 1. Convert the read string s manually, the code is [new String(s.getByte(A), B)]; 2. Druid is used to provide a unified layer of encapsulation and encoding conversion for various database drivers.
[Related learning recommendations: java basic tutorial]
java oracle Chinese garbled solution:
Transcoding method
When Java reads Oracle and encounters Chinese garbled characters, we need to transcode. There are many methods of transcoding, the ones I have come across are the following.
1. Pure manual transcoding
Transcode the read string s, such as: new String(s.getByte(A), B)
2 , Druid
Druid is a driver developed by Alibaba itself. It actually encapsulates various database drivers in a unified layer, adding functions such as logging, alarms, and encoding conversion. The configuration method is as follows:
<bean id="opensqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.alibaba.china.jdbc.SimpleDriver" /> <property name="url" value="jdbc:oracle:thin:@10.20.130.210:1521:dwtest" /> <property name="username" value="etl" /> <property name="password" value="etl" /> <property name="connectionProperties"><value>serverEncoding=ISO-8859-1;clientEncoding=GBK;defaultRowPrefetch=50;bigStringTryClob=true</value></property> </bean>
The connectionProperties contains two properties: serverEncoding and clientEncoding. After Java reads the data, if it is found that serverEncoding and clientEncoding are different, the following encoding conversion will be automatically performed.
new String(s.getByte(serverEncoding), clientEncoding)
3. weblade ibatis callback
It adopts the following method of registering ibatis callback.
<bean id="sqlMapExecutorDelegate" class="com.asc.alibaba.dao.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate"> <property name="mappedStatementStrategy" ref="mappedStatementStrategy" /> <property name="handlerList"> <list> <ref bean="stringHandler" /> <!-- <ref bean="objectHandler" />--> </list> </property> </bean> <bean id="stringHandler" class="com.asc.alibaba.dao.ibatis.handler.TypeHandlerAdapter"> <property name="javaType" value="java.lang.String" /> <property name="handlerCallback" ref="stringTypeHandlerCallback" /> </bean>
So that by default, the program will convert the String obtained by ibatis as follows:
new String(s.getByte(“ISO-8859-1”), “GBK”)
The second-party library is introduced as follows:
<dependency> <groupId>com.alibaba.asc.shared</groupId> <artifactId>weblade.core.ibatisext</artifactId> <version>1.2.0-SNAPSHOT</version> </dependency>
Ibatis The callback hides the encoding conversion process, but the problem is that it takes effect on all data sources in the project. This makes it impossible to transparently support data sources that require transcoding and data sources that do not require transcoding at the same time.
Related recommendations: Programming video course
The above is the detailed content of How to deal with garbled Chinese characters in java oracle. For more information, please follow other related articles on the PHP Chinese website!