java 备份还原 mysql 数据库

来源:互联网 发布:a的平方加b的平方c语言 编辑:程序博客网 时间:2024/05/01 08:58

直接上代码

 

package test.dbupback;import java.io.IOException;/** *  * @author xxxxxxbin It is work ! *  */public class MySqlDbUpBack{public MySqlDbUpBack(){}private static String str = null;private String user;private String password;private String host;private String port;private String database;private String table;private String filepath;public boolean backup() {boolean b = false;StringBuilder sb = new StringBuilder();sb.append(" mysqldump");if (IsNotNullorNotEmpty(this.user)) {sb.append(" -u " + this.user);}if (IsNotNullorNotEmpty(this.password)) {sb.append(" -p" + this.password);}if (IsNotNullorNotEmpty(this.host)) {sb.append(" -h " + this.host);}if (IsNotNullorNotEmpty(this.port)) {sb.append(" -P " + this.port);}if (IsNotNullorNotEmpty(this.database)) {sb.append(" --opt " + this.database);}if (IsNotNullorNotEmpty(this.table)) {sb.append(" " + this.table);}if (IsNotNullorNotEmpty(this.filepath)) {sb.append(" > " + this.filepath);}// 使用mysqldump来备份数据库,格式"mysqldump -u username -ppassword --opt database_name > direction/backup_name.sql"str = " mysqldump -u test -ptest -h 172.16.53.184 -P 3306 --opt keke4 > d:/test.sql";str = " mysqldump -u test -ptest -h 172.16.53.184 -P 3306 --opt keke4 > d:/test.sql";// str =// " mysqldump -h 127.0.0.1 -P 3306 --user=root --password=hightern mi365 @D:/test1.sql";try {Runtime rt = Runtime.getRuntime();rt.exec("cmd /c" + sb);// runtime.getruntime().exec( )这个方法可以实现对命令的调用。具体内容看api// 上面可以cmd调用控制台,然后执行str中的字符串表示的命令。b = true;System.out.println("successly!");} catch (IOException e) {e.printStackTrace();System.out.println("something was wrong!");}return b;}public boolean load() {boolean b = false;str = "mysql -u test -ptest -h 172.16.53.184 -P 3306 test  < d:/test.sql";// mysql命令可以实现数据库的还原。格式"mysql -u username  -ppassword   database_name     <     back_up_dir  "Runtime rt = Runtime.getRuntime();try {rt.exec("cmd /c" + str);b = true;System.out.println("restore successly!");} catch (IOException e) {e.printStackTrace();System.out.println("restore fail!");}return b;}/** * 判断对象是否为空 空字符串 长度零的字符串 *  * @param obj * @return */public boolean IsNotNullorNotEmpty(Object obj) {if (obj instanceof String) {if (obj != null && !obj.equals("")) {int  s = ((String) obj).length();return ((String) obj).length() == 0 ? false : true;}} else if (obj != null) {return true;}return false;}public static void main(String[] args) {MySqlDbUpBack back = new MySqlDbUpBack();back.setDatabase("keke4");back.setFilepath("d:/test.sql");back.setHost("172.16.53.184");back.setPassword("test");back.setPort("3306");back.setUser("test"); back.backup();// back.load();}public void setUser(String user) {this.user = user;}public void setPassword(String password) {this.password = password;}public void setHost(String host) {this.host = host;}public void setPort(String port) {this.port = port;}public void setDatabase(String database) {this.database = database;}public void setTable(String table) {this.table = table;}public void setFilepath(String filepath) {this.filepath = filepath;}}

原创粉丝点击