Reprint: http://blog.csdn.net/a19881029/article/details/7902701
Problem Description:
Inserts a string type of data into a VARCHAR2 (64) Type field in the Oracle database, the program uses String.Length () to verify the length of the data, and if the data is in plain English, there is no problem, but if the data contains Chinese, the checksum can be passed, However, it is often reported that the data is very long when it is in storage.
Problem Analysis:
Since the problem is that the data is very long, then the problem should be the data length check, that is, in the String.Length () This method, to see how the JDK describes the method:
[Plain]View Plaincopy
- Length
- public int Length () returns the length of this string. The length equals the number of Unicode code units in the string.
- Designated by:
- Length in the interface charsequence
- Return:
- The length of the character sequence represented by this object.
[Java]View Plaincopy
- Public static void Main (string[] args) throws unsupportedencodingexception {
- String a = "123ABC";
- System.out.println (A.length ());
- A = "Chinese";
- System.out.println (A.length ());
- }
The results are 6 and 2. This method determines the character length of a string string, but in the Oracle database the length of the VARCHAR2 type data is determined in bytes (for example, if the field is defined as VARCHAR2 (64), the byte length of the string to which the field is stored must not exceed 64). If string string is in plain English, then an English letter is a character with a length of 1, 1 bytes, no error, but if the string contains Chinese, a Chinese character is also a char, length is 1, but it takes up more than a few bytes (which is related to the encoding used). If the data contains Chinese, the length of the data is likely to exceed the length limit of the corresponding field in the database
Different databases calculate the length of the string type data in different ways, such as: the length of the data in theMySQL database is determined by character length (for example: The field definition varchar, the length is set to 64, the decimal place is defined as 0, The character length of the string stored in the field must not exceed
How to resolve:
Since it is to judge the length of the data by character as the standard result in error, then the idea is very clear, in the data length check, the data byte length:
[Java]View Plaincopy
- public static void main (string[] args) throws unsupportedencodingexception {
- String a = "123ABC";
- int num = a.getbytes ( "Utf-8"). Length;
- system.out.println (num);
- a = "Chinese";
- num = a.getbytes ( "Utf-8") .length;
- system.out.println (num);
- }
The results are 6 and 6, why convert to Utf-8, because the database is using UTF-8 encoding, since the data is ultimately to be stored in the database, then first of all to ensure that the data in the program, in the database when the encoding consistent (the same character in different encoding format of the number of bytes accounted for inconsistent, This is critical), and then ensure that the program and the database to determine the length of the data in the same way, to avoid the program verification pass, storage, but the length of the data to indicate the long problem.
Byte length of Java string type data