MySQL database backup and recovery code written in java:

Source: Internet
Author: User
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 ");
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.