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 (); }
}
}