1. java code implementation for MySQL database backup and recovery: For details, see packagecom. spring. util; importjava. io. bufferedReader; importjava. io. bufferedWriter; importjava. io. fileInputStream; importjava. io. fileOutputStream; importjava. io. IOException; import
1. java code implementation for MySQL database backup and recovery: For details, see package com. spring. util; import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import
1. MySQL database backup and recovery, java code implementation: For details, see the following:
Package com. spring. util;
Import java. io. BufferedReader;
Import java. io. BufferedWriter;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java. io. OutputStream;
Import java. io. OutputStreamWriter;
// MYSQL Database Backup
Public class MySQLDump {
// Back up the database
Public static boolean sqlDump (String cmd, String filePath ){
Boolean falg = false;
Try {
Runtime run = Runtime. getRuntime ();
Process p = run.exe c (cmd );
InputStream is = p. getInputStream (); // the output information of the console is used as the input stream.
InputStreamReader isr = new InputStreamReader (is, "UTF-8"); // sets the input stream encoding format
BufferedReader br = new BufferedReader (isr );
// Write the console input information to the file output stream
FileOutputStream fos = new FileOutputStream (filePath );
BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (fos, "UTF-8 "));
String temp = null;
While (temp = br. readLine ())! = Null ){
Bw. write (temp );
Bw. newLine ();
}
Bw. flush ();
Bw. close ();
Br. close ();
Falg = true;
System. out. println ("/* Dump SQL File" + filePath + "OK! */");
} Catch (IOException e ){
Throw new RuntimeException ("add the mysql command to the path! ", E );
}
Return falg;
}
// Restore the database
Public static void sqlLoad (String cmd, String sqlPath ){
Try {
Runtime rt = Runtime. getRuntime ();
Process child = rt.exe c (cmd );
OutputStream out = child. getOutputStream (); // the input information of the console is used as the output stream.
// Input stream
BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (sqlPath), "UTF-8 "));
// Output stream
OutputStreamWriter writer = new OutputStreamWriter (out, "UTF-8 ");
String inStr;
While (inStr = br. readLine ())! = Null ){
Writer. write (inStr );
Writer. write ("\ n \ r ");
}
Writer. flush ();
// Do not forget to close the output stream
Out. close ();
Br. close ();
Writer. close ();
System. out. println ("/* Load SQL File" + sqlPath + "OK! */");
} Catch (IOException e ){
Throw new RuntimeException (e );
}
}
Public static void main (String [] args ){
// Back up the database
SqlDump ("C:/Program Files/MySQL Server 5.5/bin/mysqldump-uroot-proot itcastoa", "d:/itcastOA. SQL ");
// Restore the database
SqlLoad ("C:/Program Files/MySQL Server 5.5/bin/mysql.exe-uroot-proot itcastoa", "d:/itcastOA. SQL ");
}
}