JDK1.2 Solutions for JDBC Chinese issues

Source: Internet
Author: User
Tags stmt sybase access database
Solve | The problem | Chinese sender: HHT (Duke), letter area: Java


Title: JDK1.2 Solutions for JDBC Chinese issues &re Yzhang


Letter Station: NetEase Virtual Community (Mon 11 13:29:43 1999), Transfer letters





busy for a few days, finally JDK1.2 JDBC Chinese problem has a preliminary understanding, although there are problems, but I


think everyone might be in a hurry, right?:) So I put my initial ideas out first, welcome everyone to add.


JDK1.2 's Chinese problem is mainly due to the introduction of Unicode (in fact, in version 1.1), U

The characters of
Nicode are composed of 16bit, with more detailed information about Unicode interested people can go to Www.unico


de.org Lookup, in version 1.0, one Chinese is made up of two char (8bit), and over 1.1 versions

The
is made up of a char (16bit). You can use System.out.println (s.length (


)) statement to verify that S is a string of Chinese characters.


now say the solution, first of all, to declare that these results are only tested in my environment, if you

There are problems in the
environment, please be sure to inform.


my environment is jdk1.2+win95+sybase JDBC Driver (PowerJ2.5 included) +oracle jdbc D


River (incidental to Oracle8.0.3).


My first experience is to use the JDBC-ODBC Bridge Access database Chinese input There is a problem, I have not solved, I use


's JDBCODBC driver is JDK1.2-brought.


The second experience is to use JDBC directly linked to the database, there may be two situations:


1, Chinese input no problem (ORACLE8), that is, you can write directly in the SQL statement in Chinese, such as:


Statement.executeupdate ("Inser into tablename Values" ("Test",....)


2, Chinese input has a problem (SYBASE11), see the error is known to be Unicode conversion caused by the solution is to the U

The
Nicode is converted to ASCII, where a Chinese character is split into two char and used in two classes Bytetocharconve


Rter and Chartobyteconverter, in sun.io.*, can be in JDK Home\jre\lib\i18n.jar


found, no documents, I also really find usage on the Internet.

When
is entered, it is converted to an ASCII code string using Chinesestringtoascii asciitochines


estring into a Unicode string. The source code is as follows:


public static string asciitochinesestring (string s) {


char[] orig = S.tochararray ();


byte[] dest = new Byte[orig.length];


for (int i=0;i<orig.length;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<orig.length;i++)


Dest[i] = (char) (Orig[i] & 0xFF);


return new String (dest);


    }


catch (Exception e) {


System.out.println (e);


return s;


    }


  }

The
main program fragment is as follows:


try{


Statement 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);


}


with String s=asciitochinesestring (rs.getstring (1)) When reading data


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


because you can only test these two databases at hand, you may not be able to protect against other databases


. But I estimate that basically is the above two kinds of situation. In addition, with JDK1.2 Some companies provide driver also rise


level, such as Sybase original driver is not good. The principle is this, in the actual operation to rely on their own touch


. The above two methods refer to an article of a Taiwan BBS.


In addition, Yzhang don't know what version you want, you can find JDBC in jconnect Software installation directory


driver package, the possible format is a zip or jar, you can handcuff him out of the line. I'm just going to cuff
from the POWERJ.

to
.










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.