Simple database backup and recovery classes

Source: Internet
Author: User

/**
* 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

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.