When JDBC gets a database connection, it's written on the URL. The Meaning of Useunicode=true&characterencoding=utf-8

Source: Internet
Author: User

As a bridge between Java and database, JDBC is one of the foundation of Javaweb background, which needs us to be familiar with its principle and usage.

JDBC General steps for using are:

1, the use of reflection loading driver, such as Class.forName ("Com.mysql.jdbc.Driver");

2, access to the database connection objects, such as Java.sql.Connection conn = Drivermanager.getconnection (Url,user,password);

3, create statement objects or PreparedStatement objects, such as PreparedStatement ptmt=conn.preparestatement (SQL);


In order to prevent garbled, we get the database connection, in the URL will be written? Useunicode=true&characterencoding=utf-8 such a piece of code, what is the meaning of it?

First, understand what Unicode and utf-8 are.

Unicode encoding:

1, the International Unified Code, it established a world-wide unified Code table. All the words in the world are unique in this Code table.

2, the Java programming language is the default encoding method. After the program is compiled, all characters in the. class file are encoded in Unicode.


UTF-8 Code:

1, is the international general code, strong versatility, in foreign IE can be correctly displayed in Chinese and English.

2, UTF-8 an English character occupies one byte, a Chinese character occupies three bytes; So in the Chinese characters more pages, the use of UTF-8 encoding than GBK and other GB code occupy a larger space, the speed of the clock card more slowly. What is the meaning of &amp? '?' The symbol is the start flag that passes the arguments through the Get method after the URL, and the ' & ' symbol is used to connect between the parameters because they have special meaning for HTML, so the escape character is used in Java, and & is escaped in HTML as ' & ' symbol, for parameter connections. (The Get method differs from the Post method to see the difference between get and post in another blog form form data transmission mode)
third, the actual meaning of adding this piece of code

The added function is to specify the encoding and decoding format of the characters.

For example, the MySQL database is GBK encoded, and the project database is UTF-8 encoded. At this point, if you add a useunicode=true&characterencoding=utf-8, then the effect has the following two aspects:

1. When saving data:

When storing project data, the database decodes the data into bytecode in the UTF-8 format and then gbk the decoded bytecode back into the database using the encoded code.

2. When data is taken:

When fetching data from a database, the database decodes the data in the database into bytecode in GBK format, then encodes the decoded bytecode again in the UTF-8 format, and then returns the data to the client.


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.