In my spare time, I would like to summarize how to back up MySQL databases using Java:
In my spare time, I would like to summarize how to back up MySQL databases using Java:
In my spare time, I would like to summarize how to back up MySQL databases using Java:
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. OutputStreamWriter;
Import java. io. PrintWriter;
/**
* MySQL Database Backup
*
* @ Author GaoHuanjie
*/
Public class MySQLDatabaseBackup {
/**
* Java code for MySQL Database Export
*
* @ Author GaoHuanjie
* @ Param hostIP the IP address of the MySQL database server
* @ Param userName the userName required to enter the database
* @ Param userName the userName required to enter the database
* @ Param password the password required to enter the database
* @ Param savePath: path for saving the exported database file
* @ Param fileName: name of the exported file in the database
* @ Param databaseName name of the database to be exported
* @ Return true indicates 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) throws InterruptedException {
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;
}
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
Try {
PrintWriter = new PrintWriter (new OutputStreamWriter (new FileOutputStream (savePath + fileName), "utf8 "));
Process process = runtime.getruntime(cmd.exe c ("mysqldump-h" + hostIP + "-u" + userName + "-p" + password + "-- set-charset = UTF8" + databaseName );
InputStreamReader inputStreamReader = new InputStreamReader (process. getInputStream (), "utf8 ");
BufferedReader = new BufferedReader (inputStreamReader );
String line;
While (line = bufferedReader. readLine ())! = Null ){
PrintWriter. println (line );
}
PrintWriter. flush ();
If (process. waitFor () = 0) {// 0 indicates that the thread is terminated normally.
Return true;
}
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
If (bufferedReader! = Null ){
BufferedReader. close ();
}
If (printWriter! = Null ){
PrintWriter. close ();
}
} Catch (IOException e ){
E. printStackTrace ();
}
}
Return false;
}
Public static void main (String [] args ){
Try {
If (exportDatabaseTool ("172.16.0.127", "root", "123456", "D:/backupDatabase", "2014-10-14. SQL", "test ")){
System. out. println ("the database is successfully backed up !!! ");
} Else {
System. out. println ("database backup failed !!! ");
}
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
-------------------------------------- Split line --------------------------------------
Install MySQL in Ubuntu 14.04
MySQL authoritative guide (original book version 2nd) Clear Chinese scan PDF
Ubuntu 14.04 LTS install LNMP Nginx \ PHP5 (PHP-FPM) \ MySQL
Build a MySQL Master/Slave server in Ubuntu 14.04
Build a highly available distributed MySQL cluster using Ubuntu 12.04 LTS
Install MySQL5.6 and Python-MySQLdb in the source code of Ubuntu 12.04
MySQL-5.5.38 universal binary Installation
Java code for MySQL database backup and import
-------------------------------------- Split line --------------------------------------