我要备份数据库,但我无法恢复相同的备份文件。这是备用代码。
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};
我的代码是否正确?我能得到一些建议吗?
答案 0 :(得分:1)
了解MySQL Backup/Restore。以下SQL可以简单地使用JDBC执行,并且更通用。它 也用于处理完整备份。
LOAD DATA INFILE '...'