java oracle数据备份_Java后台备份oracle数据库脚本

package atest;

import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.sql.Connection;

import web.db.DBConnector;

/**

* @author 朱言信

* @desc 备份oracle

*/

public class DadaBackup4Oracle {

public void doWork(){

//执行该数据备份线程

innerBackupClass ibc = new innerBackupClass("这里输入oracle的用户名", "密码", "192.168.0.66:1521/orcl", "输出的地址", "输出的文件名", "要备份的表名");

Thread td = new Thread(ibc);

td.start();

}

class innerBackupClass implements Runnable {

private String user; //用户名

private String password; //密码

private String url; //链接地址

private String outFilePath; //输出的文件路径

//private String outZipFilePath; //输出的zip文件路径

private String filename; //文件名

private String tableNames; //表名

//Sys_databackup databackup = null;

/**

* 构造方法

*/

public innerBackupClass(String user, String password, String url, String outFilePath,

//String outZipFilePath,

String filename,

String tableNames

//Sys_databackup databackup

) {

this.user = user;

this.password = password;

this.tableNames = tableNames;

this.url = url;

this.outFilePath = outFilePath;

//this.outZipFilePath = outZipFilePath;

this.filename = filename;

//this.databackup = databackup;

}

/**

* 线程执行入口

*/

public void run() {

Connection con = DBConnector.getconecttion(); // 取得一个数据库连接

Runtime rt = Runtime.getRuntime();

Process processexp = null;

checkCreatDir(outFilePath);

//这里拼装的是exp导出 当然还可以使用其它的 你懂得

String exp = "exp " + user + "/" + password + "@" + url + " file="

+ outFilePath + "/" + filename + ".dmp";

if(!"".equals(tableNames)){

exp += " tables=(" + tableNames + ")";

}

try {

processexp = rt.exec(exp);

new Thread(new StreamDrainer(processexp.getInputStream())).start();

new Thread(new StreamDrainer(processexp.getErrorStream())).start();

processexp.getOutputStream().close();

int exitValue = processexp.waitFor();

//databackup.setBackupstate("1"); //1为成功 0为失败

if(exitValue != 0){

//databackup.setBackupstate("0");

}

//将备份DMP文件压缩为zip

//删除dmp文件及所在的文件夹

//将备份的结果持久化

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

DBConnector.freecon(con); // 释放数据库连接

}

}

public void checkCreatDir(String dirPath) { // 目录是否存在

File file = new File(dirPath);

if (!file.exists()) {

file.mkdirs();

}

}

}

/**

*输出控制台信息

*/

class StreamDrainer implements Runnable {

private InputStream ins;

public StreamDrainer(InputStream ins) {

this.ins = ins;

}

public void run() {

try {

BufferedReader reader = new BufferedReader(

new InputStreamReader(ins));

String line = null;

while ((line = reader.readLine()) != null) {

System.out.println(line);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值