1.编写测试类TestOracle
package com;
public class TestOracle {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
OracleDatabaseBackup o = new OracleDatabaseBackup();
if (o.exportDatabaseTool("dimdb", "dimdb", "192.168.1.127/ORCL", "d:/B", "ca")) {
System.out.println("数据库成功备份!!!");
} else {
System.out.println("数据库备份失败!!!");
}
}
}
2.编写执行类TOracleDatabaseBackup
package com;
import java.io.File;
import java.io.IOException;
public class TOracleDatabaseBackup {
public boolean exportDatabaseTool(String userName, String password,
String SID, String savePath, String fileName)
throws InterruptedException {
File saveFile = new File(savePath);
if (!saveFile.exists()) {// 如果目录不存在
saveFile.mkdirs();// 创建文件夹
}
try {
String str="exp " + userName + "/" + password + "@" + SID + " file='"
+ savePath + "/" + fileName + ".dmp' ";
Process process = null;
process = java.lang.Runtime.getRuntime().exec("cmd /c start cmd.exe /c " + str);
System.out.println("备份开始");
System.out.println(str);
if (process.waitFor() == 0) {// 0 表示线程正常终止。
System.out.println("表示线程正常终止");
System.out.println("备份结束");
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}