Internationalization of Java Database characters

Source: Internet
Author: User
Tags format array control characters encode string integer return string methods mysql database
Data | Database Database character internationalization is the most questions you ask, such as MySQL database you may add USEUNICODE=TRUE&CHARACTERENCODING=GBK as a basic condition for Chinese support in Jdbc-url. But this sometimes destroys the integrity of the data, if some people are careless, it will lead to data coding errors, generated garbled. Therefore, we need some means of coding within the program. It is generally easy to use string encoding and decoding using string (bytes:byte[], enc:string)/string.getbytes (enc:string) in the application, but it is time-consuming and laborious to code manually if you encounter a large format.

My method: By studying the JDK class library, we can feel the superiority of the multi-layer processing mechanism in the data processing. It is entirely possible for us to create a middle tier on the database for the internationalization of characters, and that's what I do. A closer look at the source of the character encoding problem in the JDBC operation database, it is easy to find that most cases are resultset string methods, so we can write a ResultSet intermediate layer for international processing, the source code is as follows:


public class I18nresultset implements resultset{private String encoding, private ResultSet RS; public i18nresultset (Resul Tset RS, String encoding) throws java.io.unsupportedencodingexception{//checks whether the encoded name is supported by the system. "". getBytes (encoding); this.rs = RS; this.encoding = encoding; ...//The following methods are to encode string strings. public string getString (int index) throws sqlexception{string data = null; try{data = new String (rs.getbytes (index), enc oding); }catch (Java.io.UnsupportedEncodingException uee) {}} public string getString (stirng field) throws sqlexception{string data = null; try{data = new String (rs.getbytes (field), encoding);} catch (Java.io.UnsupportedEncodingException uee) {}} public void updatestring (int index, String value) throws sqlexception{try{rs.updatebytes (Index, value.getbytes (encoding)); catch (Java.io.UnsupportedEncodingException uee) {}} public void Updatestring (string field, String value) throws sqlexception{try{rs.updatebytes (field, value.getbytes (encoding)); catch (Java.io.UnsuPportedencodingexception uee) {}}


As you can see, all string operations are accessed using a specific encoded byte array, which enables the consistency of database access data encoding by defining encoding values, and encoding can be dynamically defined in the configuration information.

At the same time, the above program can solve some of the inherent string processing problems, such as control characters such as \ r \ n Import into the database is very likely to be resolved to \\r\\n so that it can not wrap, through the byte array operation, you can solve this problem. This can be preserved completely like an article's inherent format without the need for additional conversion operations.

conclusion, the layered processing of the database data by using the multi-layer processing mechanism can make the processing link loose coupling relationship, so that the effective control can be achieved.

Here's a code for character control using a dynamic proxy (original):

Package Com.yipsilon.crocodile.database;import Java.sql.resultset;import Java.lang.reflect.InvocationHandler; Import Java.lang.reflect.method;import java.io.unsupportedencodingexception;/** * Author Yipsilon * If you want to reprint, please notify the author */public Class I18nresultsethandler implements invocationhandler{private ResultSet RS; private String encoding; public I18nresult SetHandler (ResultSet RS, String encoding) throws unsupportedencodingexception{this.rs = rs; "". getBytes (encoding); this.encoding = encoding; public object invoke (object proxy, Method method, object[] args) throws throwable{String methodname = Method.getname (); if (Methodname.equals ("getString")) {Object obj = args[0]; if (obj instanceof Integer) {return decodestring ( (Integer) obj). Intvalue ()), encoding); }else{return Decodestring (Rs.getbytes ((String) obj), encoding);} else if (methodname.equals ("updatestring")) {Object obj = args[0]; if (obj instanceof integer) {rs.updatebytes ((Integer) obj). Intvalue (), encodestring ((String) args[1], encoding)); }else{rs.updatebytes ((string) obj, encodestring ((String) args[1], encoding)); Return Method.invoke (RS, args); private string decodestring (byte[] bytes, string enc) {try{return new String (bytes, enc);} catch (Unsupportedencodingex Ception uee) {return new String (bytes);}} Private byte[] Encodestring (String str, string enc) {try{return str.getbytes (ENC);} catch (Unsupportedencodingexception Uee) {return str.getbytes ();}}}


Call when used:

ResultSet rs = ...; The original resultset result set string encoding = "GBK"; Character encoding (ResultSet) proxy.newproxyinstance (Rs.getclass (). getClassLoader (), Rs.getclass (). Getinterfaces (), New I18nresultsethandler (RS, encoding));



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.