gpt4 book ai didi

java - 我想恢复数据库

转载 作者:行者123 更新时间:2023-11-29 14:05:09 24 4
gpt4 key购买 nike

我要备份数据库,但无法恢复相同的备份文件。这是备份代码。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.swing.JDialog;
import javax.swing.JFileChooser;

class Backupsql extends JDialog {

public boolean backupDB(String dbUser, String dbPass, String dbName) {
String sourcePath = null;
boolean result = false;
JFileChooser fc = new JFileChooser();
int i = fc.showSaveDialog(Backupsql.this);
File file = fc.getSelectedFile();
sourcePath = file.getPath();
String filename = file.getName();
sourcePath = sourcePath + ".sql";

String executeCmd =
"C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump -u "
+ dbUser + " -p" + dbPass + " " + dbName + " -r " + sourcePath;

Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Backup created successfully");
compressFile(sourcePath);
result = true;
} else {
System.out.println("Could not create the backup");
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("error" + e);
}
return result;
}

public static void compressFile(String srcFile) {
byte[] buffer = new byte[1024];
try {
String destPath = srcFile.replace(".sql", ".zip");
FileOutputStream fos = new FileOutputStream(destPath);
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze = new ZipEntry(srcFile);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(srcFile);

int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
zos.close();
System.out.println("Done");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

这是我正在使用的恢复命令。

String[] executeCmd = new String[]{
"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysql.exe", "--user="
+ user, "--password=" + password, "-e", " source " + Unzippath};

我的代码正确吗?我能得到一些建议吗?

最佳答案

查看 MySQL Backup/Restore 。下面的SQL可以简单地用JDBC执行,并且更通用。它也用于处理完整备份。

LOAD DATA INFILE '...'

关于java - 我想恢复数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14498270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com