Database character internationalization is the most questionable question, such as the MySQL database, you may add useUnicode = true & characterencoding = GBK as a basic condition for Chinese support in JDBC-URL. However, this sometimes destroyed the integrity of the data. If some people are careless, the data coding is incorrect, which is garbled. Therefore, we need some means to encode processing inside the program. People generally use string to use string in the application (bytes: byte [], eNC: String) /String.getbytes (Enc: string) to make string codes, although it is easy to understand, if you encounter a big field form, manual code Time consumption is laborious. My method: By studying the JDK class library, you can feel the superiority of multi-layer processing mechanisms on data processing. We are fully likely to build an intermediate layer for the internationalization of characters on the database, I do this. Carefully study the root cause of the JDBC operation database, which is easy to find that most cases are the resultSet's String method is in the monsters, 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 (ResultSet rs, String encoding) throws java.io.UnsupportedEncodingException {// check whether the names are supported coding systems. "" .getbytes (eNCoding); this.rs = rs; this.encoding = encoding;} ... // The following method is to perform String string heavy encoding. Public String getString (int index) throws sqlexception {string Data = null; try {data = new String (rs.getBytes (index), encoding);} 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) {}} ...}
It can be seen that all String operations use a specific encoded byte array to access the consistency of data inventory data coding by defining the encoding value, and Encoding can be fully defined in configuration information. At the same time, the above programs can solve some inherent string processing issues, such as the control, such as / r / n, which is likely to be resolved to // r // n to make it unable to go, byte array operations You can solve this problem. This allows for an inherent format of the article that can be fully reserved without the additional conversion operation.
Conclusion, the multi-layer processing mechanism is used to lay a lamination relationship between the database data to make the processing links between the processing links, so that effective control can be performed.
Here is a code that uses dynamic proxy to perform character control (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 * To reprint, please notify the author * / public class I18nResultSetHandler implements InvocationHandler {private ResultSet rs; private String encoding; public I18nResultSetHandler (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] {RETURN DECODESTRING (rs.getbytes); it g} (safoding);} {Return Decodestring } else if ("updatestring")) {object obj = args [0]; if (Obj InstanceOf Integer) {Rs.UpdateBytes ((Integer) .intValue (), EncodeString ((String) args [1], encoding);} else {r.Updatebytes ((String) Obj, Encodestring ((String) args [1 ], encoding));} return null;} return method.invoke (rs, args);} private String decodeString (byte [] bytes, String enc) {try {return new String (bytes, enc);} catch (UnsupportedEncodingException 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: