java还原mysql数据库_从Java轻松备份和还原mysql数据库

小编典典

注意:以下给出的代码是解决问题的一种方法,可能不是最佳方法。

代码中的所有内容都是可以更改的。如果环境变量中没有mysql,则在mysqldump和mysql之前添加路径(例如,对于XAMPP,C:\ xampp \

mysql \ bin \ mysqldump)

(希望能解决您的问题。花了我一天的时间完全弄清一切并正确实施)

备份方法:

public static void Backupdbtosql() {

try {

/*NOTE: Getting path to the Jar file being executed*/

/*NOTE: YourImplementingClass-> replace with the class executing the code*/

CodeSource codeSource = YourImplementingClass.class.getProtectionDomain().getCodeSource();

File jarFile = new File(codeSource.getLocation().toURI().getPath());

String jarDir = jarFile.getParentFile().getPath();

/*NOTE: Creating Database Constraints*/

String dbName = "YourDBName";

String dbUser = "YourUserName";

String dbPass = "YourUserPassword";

/*NOTE: Creating Path Constraints for folder saving*/

/*NOTE: Here the backup folder is created for saving inside it*/

String folderPath = jarDir + "\\backup";

/*NOTE: Creating Folder if it does not exist*/

File f1 = new File(folderPath);

f1.mkdir();

/*NOTE: Creating Path Constraints for backup saving*/

/*NOTE: Here the backup is saved in a folder called backup with the name backup.sql*/

String savePath = "\"" + jarDir + "\\backup\\" + "backup.sql\"";

/*NOTE: Used to create a cmd command*/

String executeCmd = "mysqldump -u" + dbUser + " -p" + dbPass + " --database " + dbName + " -r " + savePath;

/*NOTE: Executing the command here*/

Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);

int processComplete = runtimeProcess.waitFor();

/*NOTE: processComplete=0 if correctly executed, will contain other values if not*/

if (processComplete == 0) {

System.out.println("Backup Complete");

} else {

System.out.println("Backup Failure");

}

} catch (URISyntaxException | IOException | InterruptedException ex) {

JOptionPane.showMessageDialog(null, "Error at Backuprestore" + ex.getMessage());

}

}

还原方法:

public static void Restoredbfromsql(String s) {

try {

/*NOTE: String s is the mysql file name including the .sql in its name*/

/*NOTE: Getting path to the Jar file being executed*/

/*NOTE: YourImplementingClass-> replace with the class executing the code*/

CodeSource codeSource = YourImplementingClass.class.getProtectionDomain().getCodeSource();

File jarFile = new File(codeSource.getLocation().toURI().getPath());

String jarDir = jarFile.getParentFile().getPath();

/*NOTE: Creating Database Constraints*/

String dbName = "YourDBName";

String dbUser = "YourUserName";

String dbPass = "YourUserPassword";

/*NOTE: Creating Path Constraints for restoring*/

String restorePath = jarDir + "\\backup" + "\\" + s;

/*NOTE: Used to create a cmd command*/

/*NOTE: Do not create a single large string, this will cause buffer locking, use string array*/

String[] executeCmd = new String[]{"mysql", dbName, "-u" + dbUser, "-p" + dbPass, "-e", " source " + restorePath};

/*NOTE: processComplete=0 if correctly executed, will contain other values if not*/

Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);

int processComplete = runtimeProcess.waitFor();

/*NOTE: processComplete=0 if correctly executed, will contain other values if not*/

if (processComplete == 0) {

JOptionPane.showMessageDialog(null, "Successfully restored from SQL : " + s);

} else {

JOptionPane.showMessageDialog(null, "Error at restoring");

}

} catch (URISyntaxException | IOException | InterruptedException | HeadlessException ex) {

JOptionPane.showMessageDialog(null, "Error at Restoredbfromsql" + ex.getMessage());

}

}

2020-11-13

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值