/**
* Database backup and Recovery classes
* @author Administrator
*/
Public class Beifen {
/**
* Database Backup
* @param cmd backup command
* @param filePath backup file save location
* @return
* @throws IOException
*/
Public Static Boolean sqldump (string cmd, string filePath)
throws IOException {
Boolean falg = false;
InputStream is = null;
InputStreamReader ISR = null;
BufferedReader br = null;
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
Runtime Run = runtime.getruntime ();
//cmd command:
Process p = run.exec (cmd);
is = P.getinputstream ();//console output information as input stream
ISR = new InputStreamReader (IS, "UTF-8");//Set Input stream encoding format
br = new BufferedReader (ISR);
//Writes console input information to the file output stream
fos = new FileOutputStream (filePath);
bw = new BufferedWriter (new OutputStreamWriter (FOS, "UTF-8"));
String temp = null;
While (temp = br.readline ()) = null) {
Bw.write (temp);
bw.newline ();
}
Falg = true;
System.out.println ("/* Dump SQL File" + FilePath + "ok! */");
} catch (IOException e) {
throw new RuntimeException ("Please add the mysql command to path!") ", e);
} finally {
Bw.flush ();
bw.close ();
br.close ();
}
return falg;
}
/**
* Recover Database
* @param cmd recovery command
* @param sqlpath recover files
* @throws IOException
*/
Public static void Sqlload (string cmd, String sqlpath) throws IOException {
OutputStreamWriter writer = null;
outputstream out = null;
BufferedReader br = null;
try {
Runtime RT = Runtime.getruntime ();
Process child = rt.exec (cmd);
Out = Child.getoutputstream ();//console input information as the output stream
//input stream
br = new BufferedReader (new InputStreamReader (New FileInputStream (
sqlpath), "UTF8"));
//output stream
writer = new OutputStreamWriter (out, "UTF8");
String inStr;
While ((inStr = Br.readline ()) = null) {
Writer.write (INSTR);
writer.write ("\ r \ n");
}
Writer.flush ();
System.out.println ("/* Load SQL File" + SQLPath + "ok! */");
} catch (Exception e) {
e.printstacktrace ();
} finally {
//input/output stream
out.close ();
br.close ();
writer.close ();
}
}
/**
* Test the Main method
* @param args
*/
Public static void Main (string[] args) {
//BACKUP Database
Boolean b=false;
try {
B =
sqldump ("E:/mysql-5.6.14-winx64/bin/mysqldump-uroot-proot 51sql", "C:y_copy04.sql");
} catch (IOException e) {
e.printstacktrace ();
}
System.out.println ("---------------->" +b);
//Recovery database
//try {
//Sqlload ("E:/mysql-5.6.14-winx64/bin/mysql.exe-uroot-proot 51sql"
//, "C:y_copy04.sql");
//} catch (IOException e) {
//E.printstacktrace ();
// }
}
}
Simple database backup and recovery classes