The garbled problem of using mysqldump to back up the database recently in the project, because the system needs to provide the database backup function, after some searches on the Internet, I think the simple mysqldump (1) java code [java] <span style = "font-size: 14px; "> String backupSQL =" cmd/c mysqldump-urootdd -- extended-insert = false-hlocalhost fts> "+ DB_BACKUPFILE_PATH +" "; Runtime runtime = Runtime. getRuntime (); System.out.println(runtime.exe c (backupSQL); </span> by executing the above code snippet, Chinese characters are garbled, at first, it was suspected that the database encoding was related, so we used the following method to query the database encoding (2)> show variables like 'character % ';
After the above steps, we can confirm that the encoding of the database is correct. After another online search, we can find the following conclusion: "If the character set on the MySQL server is latin1 or another one, Chinese characters from mysqldump are garbled! A simple method is to add the default character set options, such as mysqldump [-h 127.0.0.1-P 3307]-u username -- default-character-set = gbk-p databasename> dumpfile.txt, -- When the default-character-set knows that the data content is Chinese, it can be specified as gbk, so that even if the database itself sets the character set to the Chinese language of the file latin1, it can be normal! ", Finally, change the code to the following form (3) Final java code [java] <span style =" font-size: 14px; "> String backupSQL =" cmd/c mysqldump-urootdd -- default-character-set = gbk -- opt -- extended-insert = false-hlocalhost fts> "+ DB_BACKUPFILE_PATH + ""; runtime runtime = Runtime. getRuntime (); System.out.println(runtime.exe c (backupSQL); </span> after the preceding modification, the exported file's Chinese characters are displayed normally.