About mysql database "Incorrectstringvalue: & amp; #39; \ xFF \ xFE \ x0DNSb

Source: Internet
Author: User

Maybe many people may encounter such problems when using the mysql database like me. They cannot display Chinese characters and cannot be loaded into the database.

This is the character encoding problem that programmers will encounter. Next we will use the JAVA link database as the column description.

Package fang. demo; import java. io. file; import java. io. fileInputStream; import java. io. inputStream; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import javax. swing. JPopupMenu. separator; public class ConnectDemo04 {// define the MySQL database driver public static final String DBDRIVER = "org. gjt. mm. mysql. driver "; // define the MySQL database connection address public static final String DBURL = "jdbc: mysql: // localhost: 3306/demo2"; // MySQL database connection username public static final String DBUSER = "root "; // MySQL database Connection password public static final String DBPASS = ""; public static void main (String args []) throws Exception {// All exceptions throw Connection conn = null; // database connection PreparedStatement stmt = null; // database operation ResultSet res = null; Class. forName (DBDRIVER); // load the driver String name = "fang sa'an"; // name String SQL = "Insert into userclob (name, note) VALUES (?,?) "; Class. forName (DBDRIVER); // load the driver conn = DriverManager. getConnection (DBURL, DBUSER, DBPASS); stmt = conn. prepareStatement (SQL); // create the PreapredStatement object File f = new File ("d:" + File. separator + "test.txt"); InputStream input = null; input = new FileInputStream (f); // read the file stmt through the input stream. setString (1, name); stmt. setAsciiStream (2, input, (int) f. length () Begin stmt.exe cuteUpdate (); conn. close (); // close the database }};

This Java program reads the test.txt file under the dashboard.

Error reported:

When this problem occurs, we can see that the error message is of an incorrect data type (probably like this). Then I tried to run both the trial numbers and English letters and then write them into the database.

Database Data Diagram

It can be seen that this problem must be caused by incorrect data character encoding. How can this problem be solved?

It is easy to know from the data tables I created, they are all utf8 types of universal international data types (recommended)

But what about notepad encoding? What type is it?

But ......

However, we can open the txt file and click Save As-> set encoding.

Set to data encoding utf8

Run the java program again

You will find the error disappears. Open the database file and you will find that the data has been successfully written into the data table.

Of course, the principle of website error reporting is the same as that in this case. If you have set the character encoding of the data table carefully, it will be okay.

For settings, see another blog.

Related Article

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.