62,614
社区成员
发帖
与我相关
我的任务
分享
package DB;
import java.io.IOException;
public class MySQL_Backup {
public static void main(String[] args) {
try {
String str = "mysqldump -u root -p 1120 db_test > d:/db_test_bak.sql";
Runtime rt=Runtime.getRuntime();
rt.exec("cmd /c"+str);
//Process proc = rt.exec(new String[]{"mysqldump","-u root"," -p 1120"," --default-character-set=utf8 --opt db_test > d:test_bak.sql"});
//Process p = Runtime.getRuntime().exec("cmd.exe /c" + str);
//p.getErrorStream();
System.out.println("备份成功!");
} catch (IOException e) {
System.out.println("备份失败!");
e.printStackTrace();
}
}
}
package DB;
import java.io.IOException;
public class MySQL_Recover {
public static void main(String[] args) {
String filepath = "d:\\db_test_bak.sql"; // 备份的路径地址
//String stmt1 = "mysqladmin -u root -p 1120 create db_test";
String stmt2 = "mysql -u root -p 1120 db_test < " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };
try {
//Runtime.getRuntime().exec("cmd /c" + stmt1);
Runtime.getRuntime().exec(cmd);
System.out.println("数据已从 " + filepath + " 导入到数据库中");
} catch (IOException e) {
e.printStackTrace();
}
}
}