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 stringpublic 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 Constructorpublic 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: Usestr=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 stringpublic 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 Constructorpublic 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: Usestr=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.