Solution to JDBC Chinese issues about JDK1.2

zhaozj2021-02-17  146

After a few days, I finally had a preliminary understanding of JDK1.2 JDBC Chinese, although there is still a problem, I think everyone may be anxious? :) So I will put my initial idea first, welcome Everyone is supplemented. The Chinese problem of JDK1.2 is mainly caused by Unicode (in fact, in version 1.1), Unicode characters consist of 16bit, information about Unicode more detailed information, interesters can go to www.unicode. ORG consultation, in the version 1.0, one Chinese is composed of two char (8bit), and 1.1 or above is composed of a char (16bit). This one can use System.out.Println (S.Length ()) The statement confirms that S is Chinese strings. Now let's talk about a solution, first declare that these results are just testing in my environment. If there is a problem in your environment, be sure to tell.

My environment is JDK1.2 Win95 Sybase JDBC Driver (PowerJ2.5) Oracle JDBC Driver (Oracle 8.0.3). My first experience is to use JDBC-ODBC bridge access database Chinese input has problems, I I haven't resolved yet, I use JDBCODBC Driver is coming from JDK1.2. The second thing is to use the JDBC direct database, there may be two cases: 1, Chinese input is no problem (Oracle8), you can be in SQL Write Chinese directly in the statement, such as:

STATEMENT.EXECUTEUPDATE ("Test", ....) 2, Chinese input has problems (Sybase11), seeing errors, knowing that Unicode conversion, the solution is to convert Unicode to ASCII form, ie one Chinese characters are removed into two char, with two class bytetocharconverter and Chartobyteconvert, in Sun.IO. *, You can find it in JDK HOME / JRE / LIB / I18n.jar, no documentation, I also get usually found online.

When entering the input, convert to an ASCII code string with chineseStringToascii. Use asCIITOCHINESSSTRING to Unicode string with asCIIToCHINESSTRIN. The source code is as follows:

Public static string asciitoCHINESSSSSTRING (String s) {

CHAR [] Orig = S.ToChararray ();

Byte [] dest = new byte [Orig.Length];

For (int i = 0; i

DEST [I] = (Byte) (Orig [i] & 0xFF);

Try {

Bytetocharconverter tochar = bytetocharconverter.getConverter ("g

B2312 ");

Return New String (Tochar.convertall (DEST));

}

Catch (Exception E) {

System.out.println (e);

Return S;

}

}

Public static string chineseStringToascii (string s) {

Try {

Chartobyteconverter Tobyte = Chartobyteconverter.getConverter ("g

B2312 ");

Byte [] Orig = Tobyte.convertall (S.ToChararray ());

CHAR [] dest = new char [orig.Length];

For (int i = 0; i

DEST [I] = (char) (Orig [I] & 0xFF);

Return New String (DEST);

}

Catch (Exception E) {

System.out.println (e); Return S;

}

}

The main program is as follows:

Try {

StMT;

Class.Forname ("com.sybase.jdbc.sybdriver");

Connection conn = drivermanager.getConnection ("JDBC: Sybase: TDS: 202.9

7.228.249: 5000 / TODO "," SA "," ");

STMT = conn.createstatement ();

String s = "He Haitao";

S = ChineseStringToascii (s);

Stmt.executeUpdate ("INSERT INTO RUSSIA VALUES ('" s ", 1, 1,'" s

"')");

} catch (exception e) {

System.out.println (e);

}

Use string s = asciitochinese job when reading data (rs.getstring (1))

--------------------

Because the hand can only test these two databases, it is not guaranteed about the situation that may occur for other databases. However, I am estimated that it is basically above. In addition, the Driver provided by companies with JDK1.2 is also upgraded. For example, Sybase original Driver is not good. The principle is like this, in practice, you can explore yourself. The above two methods refer to an article of a BBS in Taiwan. In addition, Yzhang I don't know what version you want, You can find a JDBC Driver package in the Directory installed in the JConnect software, and possible formats are zip or jar, which will be copied. I am copy from Powerj.

转载请注明原文地址:https://www.9cbs.com/read-31429.html

New Post(0)