Byte length of Java string type data

Source: Internet
Author: User

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
    1. Length
    2. public int Length () returns the length of this string. The length equals the number of Unicode code units in the string.
    3. Designated by:
    4. Length in the interface charsequence
    5. Return:
    6. The length of the character sequence represented by this object.
[Java]View PlainCopy
    1. Public static void Main (string[] args) throws unsupportedencodingexception {
    2. String a = "123ABC";
    3. System.out.println (A.length ());
    4. A = "Chinese";
    5. System.out.println (A.length ());
    6. }

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

The length of the string type data is calculated differently for different databases, such as: the length of the varchar type data in the MySQL database (for example, the field definition varchar, the length is 64, the decimal bit is defined as 0, the character length of the string that is stored in the field must not exceed 64)

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
    1. public static  void main (string[] args)  throws  unsupportedencodingexception {  
    2.      String a =  "123ABC";   
    3.      int num = a.getbytes ( "Utf-8"). Length;   
    4.     system.out.println (num);   
    5.     a =  "Chinese";   
    6.     num = a.getbytes ( "Utf-8") .length;  
    7.     system.out.println (num);   
    8. }  

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, but the data in the storage of long-length problem

Byte length of Java string type data

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.