JAVA code Backup DATABASE and recovery

Source: Internet
Author: User

The MySQL database is backed up.
The code is as follows, run it under your own look. Don't talk nonsense.

Package backup.back;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;

Import Java.io.OutputStreamWriter; /** * Database backup and restore operation * * @Date March 7, 2017/public class Dboperate {/** * database table backup * * @throws EXCEP tion */public static void Tablebackup (String dbuser,string dbpass,string dbhost,string dbport,string Dbname,stri
        Ng Savepath, String tablename) throws Exception {Runtime Runtime = Runtime.getruntime ();
                -U followed by user name,-p is the best not to have a space after the password-p,-family is the name of the database process process = Runtime.exec ("mysqldump-h" + dbhost + "P"
        + Dbport + "-u" + dbuser + "-P" + Dbpass + "" + dbname + "" + tablename); InputStream InputStream = Process.getinputstream ()//Get input stream, write. sql file InputStreamReader reader = new Inputstreamrea Der (InputstreAM);
        BufferedReader br = new BufferedReader (reader);
        String s = null;
        StringBuffer sb = new StringBuffer ();
        while (s = br.readline ())!= null) {Sb.append (s + "\ r \ n");
        } s = sb.tostring ();
        File File = new file (Savepath);
        File.getparentfile (). Mkdirs ();
        FileOutputStream FileOutputStream = new FileOutputStream (file);
        Fileoutputstream.write (S.getbytes ());
        Fileoutputstream.close ();
        Br.close ();
        Reader.close ();
    Inputstream.close (); /** * BACKUP DATABASE * * @param savepath * @throws Exception/public static void DbBackup (St

        Ring Dbuser, String dbpass, String dbhost, String dbport, String dbname, String savepath) throws Exception {
        Runtime Runtime = Runtime.getruntime ();
                -U followed by user name,-p is the best not to have a space after the password-p,-family is the name of the database process process = Runtime.exec ("mysqldump-h" + dbhost + "P" + Dbport+ "-U" + dbuser + "-P" + Dbpass + "" + dbname); InputStream InputStream = Process.getinputstream ()//Get input stream, write. sql file InputStreamReader reader = new Inputstreamrea
        Der (InputStream);
        BufferedReader br = new BufferedReader (reader);
        String s = null;
        StringBuffer sb = new StringBuffer ();
        while (s = br.readline ())!= null) {Sb.append (s + "\ r \ n");
        } s = sb.tostring ();
        File File = new file (Savepath);
        File.getparentfile (). Mkdirs ();
        FileOutputStream FileOutputStream = new FileOutputStream (file);
        Fileoutputstream.write (S.getbytes ());
        Fileoutputstream.close ();
        Br.close ();
        Reader.close ();
    Inputstream.close (); /** * Execute SQL File * * @param savepath * @throws Exception/public static void Dbrecover (String dbuser, String Dbpass, String dbhost, String dbport, String dbname, String savepath) throws Exception
 {       Gets the associated properties of the operation database Runtime Runtime = Runtime.getruntime ();
                Process process = Runtime.exec ("mysql-h" + Dbhost + "-P" + Dbport + "-u" + dbuser + "p" + Dbpass
        + "--default-character-set=utf8" + dbname);
        OutputStream outputstream = Process.getoutputstream ();
        BufferedReader br = new BufferedReader (new InputStreamReader (New FileInputStream (Savepath)));
        String str = NULL;
        StringBuffer sb = new StringBuffer ();
        while (str = Br.readline ())!= null) {sb.append (str + "\ r \ n");
        str = sb.tostring ();
        OutputStreamWriter writer = new OutputStreamWriter (OutputStream, "utf-8");
        Writer.write (str);
        Writer.flush ();
        Outputstream.close ();
        Br.close ();
    Writer.close (); public static void Main (string[] args) {/*try {dbBackup ("root", "root", "localhost", "3306", " Epdb "," D:/back.sql "); System.out.println ("done.")
        ");
        catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
            }*/try {dbrecover ("root", "root", "localhost", "3306", "epdb", "D:/back.sql"); System.out.println ("done.")
        ");
        catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); }
    }

}

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.