The blog "Java implements MySQL database backup (I)" uses the IO stream method to back up the MySQL database. this method is complicated. the following introduces another method to back up the MySQL database: importjavaioFile; importjavaioIOExcept blog 《
Implement MySQL database backup in Java (1)I/O stream is used to back up the MySQL database. this method is complicated. the following introduces another method to back up the MySQL database:
Import java. io. file; import java. io. IOException; /*** MySQL database backup *** @ author GaoHuanjie */public class MySQLDatabaseBackup {/*** Java code for MySQL database export ** @ author GaoHuanjie * @ param hostIP MySQL database server IP address * @ param userName the userName required to enter the database * @ param password The password required to enter the database * @ param savePath database export file storage path * @ param fileName database export file name * @ param databaseName name of the database to be exported * @ return returns true, indicating that the export is successful, otherwise, false is returned. */Public static boolean exportDatabaseTool (String hostIP, String userName, String password, String savePath, String fileName, String databaseName) {File saveFile = new File (savePath); if (! SaveFile. exists () {// if the directory does not exist, saveFile. mkdirs (); // create a folder} if (! SavePath. endsWith (File. separator) {savePath = savePath + File. separator;} StringBuilder stringBuilder = new StringBuilder (); stringBuilder. append ("mysqldump "). append ("-- opt "). append ("-h "). append (hostIP); stringBuilder. append ("-- user = "). append (userName ). append ("-- password = "). append (password ). append ("-- lock-all-tables = true"); stringBuilder. append ("-- result-file = "). append (savePath + fileName ). appe Nd ("-- default-character-set = utf8 "). append (databaseName); try {Process process = runtime.getruntime(.exe c (stringBuilder. toString (); if (process. waitFor () = 0) {// 0 indicates that the thread is terminated normally. Return true ;}} catch (IOException e) {e. printStackTrace ();} catch (InterruptedException e) {e. printStackTrace ();} return false;} public static void main (String [] args) throws InterruptedException {if (exportDatabaseTool ("172.16.0.127", "root", "123456 ", "D:/backupDatabase", "2014-10-14. SQL", "test") {System. out. println ("database backup successful !!! ");} Else {System. out. println (" database backup failed !!! ");}}}