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):
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));
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.