Back up MySQL database in Java program

Source: Internet
Author: User


1. Obtain the system type. Different systems have different file segmentation symbols.

Public static Boolean islinux (){
String OS = system. getproperty ("OS. Name"). tolowercase ();
If (OS. indexof ("Windows")>-1 ){
Return false;
}
Else {
Return true;
}
}

 

2. generate different commands based on different systems:

// Table name

String DBS = "Action ddinfo device devicealarm deviceinfo devicelog ";

LIST command = new arraylist ();

If (islinux ){
Command. Add (exec_path + "mysqldump ");
Command. Add ("-- opt ");
Command. Add ("-- user = root ");
Command. Add ("-- lock-all-tables = true ");
Command. Add ("-- result-file =" + dumpedfilepath + file. Separator + server_backup_ SQL _file );
Command. Add ("orca ");
String [] STR = DBs. Split ("");
For (INT I = 0; I <Str. length; I ++)
Command. Add (STR [I]);
}
Else {
Command. Add ("\" "+ exec_path +" mysqldump \ ""); // is this work?
Command. Add ("-- opt ");
Command. Add ("-- user = root ");
Command. Add ("-- lock-all-tables = true ");
Command. Add ("-- result-file =" + dumpedfilepath + file. Separator + server_backup_ SQL _file );
Command. Add ("orca ");
String [] STR = DBs. Split ("");
For (INT I = 0; I <Str. length; I ++)
Command. Add (STR [I]);

3. Run the command

Get Runtime

Private runtime cmd = runtime. getruntime ();

PROCESS p = cmd.exe C (string []) command. toarray (New String [0]), null );
String line;
Bufferedreader BR = new bufferedreader (New inputstreamreader (P. geterrorstream ()));
While (line = Br. Readline ())! = NULL)
Log. Error ("<" + line );

 

Process is used to display error information

Related Article

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.