Java JDBC-ODBC connection access database Chinese garbled Solution

Source: Internet
Author: User
Document directory
  • 1. text fields updated to the database
  • Use the updatebytes method of resultset
  • Use the getbytes method of string
  • 2. Read text from database fields
  • Use the constructor of the string class
  • 1. text fields updated to the database
  • Use the updatebytes method of resultset
  • Use the getbytes method of string
  • 2. Read text from database fields
  • Use the constructor of the string class

From http://blog.csdn.net/iamoyjj/article/details/5805327

Java's string type uses unicode encoding, while Microsoft's access database uses GBK encoding. Garbled characters may occur when you use string-type variables to store text fields in access.

The byte [] type can solve this problem.

 

1. Use the updatebytes method of resultset to update the text fields to the database.
public void updateBytes(String columnName,                        byte[] x)                 throws SQLException
String STR = "Chinese characters to be written to the Database"; // UTF-8
Use the getbytes method of string
public byte[] getBytes(String charsetName)                throws UnsupportedEncodingException
Convert the encoding format to GBK

Rs. updatebytes ("columnname", str. getbytes ("GBK "));

rs.updateRow();
In this way, the Chinese characters written to the ACCESS database are normal;
2. Read text from database fields using string class Constructor
public String(byte[] bytes,              String charsetName)       throws UnsupportedEncodingException
str=new String(rs.getBytes("columnName"),"gbk");
In this way, the Chinese characters read to the string are normal.
The preceding method solves the garbled problem between the access data text field and the string type.
Essentially, the encoding type does not match. Convert the encoding type as needed.
This is an effective way to use the byte data type to achieve the goal and use the appropriate API method.Comment: Use
str=new String(rs.getBytes("columnName"),"gbk");

When reading data from the Access database, if a blank field is encountered, getbytes () returns NULL, and then string conversion, the nullpoint exception will occur.
Therefore, it is necessary to determine whether the returned results of getbytes () are null and not empty before conversion.
It can be determined as follows:
Byte [] BTS = NULL;
String Other = NULL;
BTS = Rs. getbytes ("Others"); // read the other field
If (BTS! = NULL)
Other = new string (BTS, "GBK ");// If not empty, convert

Note that equals (null) cannot be used to determine whether it is null,
That is to say, you cannot use BTS. Equals (null) to determine whether to throw an nullpointer exception.
Because if getbytes reads null fields, a null pointer is returned.

Java's string type uses unicode encoding, while Microsoft's access database uses GBK encoding. Garbled characters may occur when you use string-type variables to store text fields in access.

The byte [] type can solve this problem.

 

1. Use the updatebytes method of resultset to update the text fields to the database.
public void updateBytes(String columnName,                        byte[] x)                 throws SQLException
String STR = "Chinese characters to be written to the Database"; // UTF-8
Use the getbytes method of string
public byte[] getBytes(String charsetName)                throws UnsupportedEncodingException
Convert the encoding format to GBK

Rs. updatebytes ("columnname", str. getbytes ("GBK "));

rs.updateRow();
In this way, the Chinese characters written to the ACCESS database are normal;
2. Read text from database fields using string class Constructor
public String(byte[] bytes,              String charsetName)       throws UnsupportedEncodingException
str=new String(rs.getBytes("columnName"),"gbk");
In this way, the Chinese characters read to the string are normal.
The preceding method solves the garbled problem between the access data text field and the string type.
Essentially, the encoding type does not match. Convert the encoding type as needed.
This is an effective way to use the byte data type to achieve the goal and use the appropriate API method.Comment: Use
str=new String(rs.getBytes("columnName"),"gbk");

When reading data from the Access database, if a blank field is encountered, getbytes () returns NULL, and then string conversion, the nullpoint exception will occur.
Therefore, it is necessary to determine whether the returned results of getbytes () are null and not empty before conversion.
It can be determined as follows:
Byte [] BTS = NULL;
String Other = NULL;
BTS = Rs. getbytes ("Others"); // read the other field
If (BTS! = NULL)
Other = new string (BTS, "GBK ");// If not empty, convert

Note that equals (null) cannot be used to determine whether it is null,
That is to say, you cannot use BTS. Equals (null) to determine whether to throw an nullpointer exception.
Because if getbytes reads null fields, a null pointer is returned.

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.