How to deal with garbled Chinese characters in java oracle
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.
