1、具体备份的业务类
package dhh;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.TimerTask;
/**
* @author Hayward
* @version 创建时间:2017年12月27日 下午3:49:51
*
*/
public class OracleDatabaseBackup extends TimerTask{
@Override
public void run() {
try {
//这个连接数据库
InputStream inStream = OracleDatabaseBackup.class.getClassLoader().getResourceAsStream("datasource.properties");
Properties prop = new Properties();
prop.load(inStream);
String username = prop.getProperty("userName");
String password=prop.getProperty("password");
String SID=prop.getProperty("SID");
String savePath=prop.getProperty("savePath");
String fileName=prop.getProperty("fileName");
if (exportDatabaseTool(username, password, SID, savePath, fileName+System.currentTimeMillis())) {
System.out.println("数据库成功备份!!。");
} else {
System.out.println("数据库备份失败!!!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Java代码实现Oracle数据库导出
*
*
* @param userName 进入数据库所须要的username
* @param password 进入数据库所须要的密码
* @param SID 用户所在的SID
* @param savePath 数据库导出文件保存路径
* @param fileName 数据库导出文件文件名称
* @return 返回true表示导出成功,否则返回false。
*/
public static 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 {
Process process = Runtime.getRuntime().exec("exp " + userName + "/" + password + "@" + SID + " file=" + savePath + "/" + fileName + ".dmp");
if(process.waitFor() == 0){//0 表示线程正常终止。
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
2、Timer定时器类
package dhh;
import java.util.Timer;
/**
* @author Hayward
* @version 创建时间:2017年12月27日 下午4:43:06
*
*/
public class mm {
public static void main(String[] args) {
Timer timer = new Timer();
// 1000是1s后开始执行 5000是每5秒执行一次
timer.schedule(new OracleDatabaseBackup(), 1000, 5000);
}
}
3、数据库参数的配置文件
userName=sapassword=dhh1990
SID=orcl
savePath=d:/beifen
fileName=dong