java批处理oracle_Java 调用bat执行的备份Oracle数据库 类

package com.buckupDB;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.swing.filechooser.FileSystemView;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

import com.pm360.mda.platform.db.DBConn;

import com.pm360.mda.platform.util.DBUtil;

import com.pm360.mda.platform.util.Logger;

import com.pm360.mda.platform.util.StringUtil;

/**

*

* 数据备份  impdp / expdp

* @author ZQD32

*

*/

public class BuckupdpDB {

/**

* //获取桌面路径

* @return

*/

private static String getDesktopPath()

{

FileSystemView fsv = FileSystemView.getFileSystemView();

File file= fsv.getHomeDirectory();

return file.getPath();

}

/**

* 获取时间字符

* @return

*/

private static String getDateTime()

{

Date date=new Date();

String str=date.toLocaleString();

str=str.replaceAll(":", "-");

str=str.replaceAll(" ", "-");

return str;

}

/**开启指定进程

* @param map

*/

private static void runingProcess(Map map)

{

String strPath = StringUtil.formatDbColumn(map.get("Paths"));

strPath = strPath.replace(" ", "\" \"");

try

{

StringBuilder sb=new StringBuilder("cmd /c start "+strPath+"\\temp.bat");

Process p = Runtime.getRuntime().exec(sb.toString());

}

catch (IOException e)

{

e.printStackTrace();

}

}

/**

* 执行备份文件

* @return

* @throws IOException

*/

public static String expingProcess(Map map)

{

if(productionExpBat(map).equalsIgnoreCase("good"))

{

runingProcess(map);

}

else

{

return "false";

}

return "true";

}

/**

* 执行导入文件

* @return

* @throws IOException

*/

public static String impingProcess(Map map)

{

if(productionImpBat(map).equalsIgnoreCase("good"))

{

runingProcess(map);

}

else

{

return "false";

}

return "true";

}

/**

* 获取 连接信息

* @param sid

* @param user

*/

private static Map getConnInfo()

{

Map map =new HashMap();

try {

BasicDataSource init = (BasicDataSource)new InitialContext().lookup("java:comp/env/jdbc/**");

String url =init.getUrl();

//获取Oracle数据库实例名

map.put("sid", url.substring(url.lastIndexOf(":")+1));

//获取oracle数据库登录名

map.put("user",init.getUsername());

//获取oracle数据库登密码

map.put("pwd", init.getPassword());

} catch (NamingException e) {

e.printStackTrace();

}

return map;

}

/**

*

* 生成 导出配置Bat文件

*/

private static String productionExpBat(Map map)

{

//得到数据库登录的信息

Map ConnInfoMap =getConnInfo();

//生成数据泵配置文件

productionExpdpPar(map);

//设置一个标志

String flag="";

//创建个缓存 数据集

List tmpBat=new ArrayList();

//获取模板 文件

File Afile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\processBat\\dp\\expdpDB.bat");

//生成空白的bat临时文件

File Bfile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\temp.bat");

//申明读取缓冲器

BufferedReader br=null;

try {

//设置读取缓冲器 文件指向

br = new BufferedReader(new InputStreamReader(new FileInputStream(Afile)));

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

//获取   rar 备份 文件的名字

String filename=StringUtil.formatDbColumn(map.get("FILENAME"));

if(filename==null||filename.equals("")){

//默认为 DBProjBuckup

filename="DBProjBuckup";

}

filename=filename+"_"+getDateTime();

try {

try {

while(br.ready())//判断是否还有可读信息

{

//读取一行数据

String str=br.readLine().toString();

//设置数据库的实例

if(str.startsWith("set sid")){

str="set sid="+ConnInfoMap.get("sid");

}

//设置数据登录名

else if(str.startsWith("set user")){

str="set user="+ConnInfoMap.get("user");

}

//设置数据登密码

else if(str.startsWith("set pwd")){

str="set pwd="+ConnInfoMap.get("pwd");;

}

//设置备份项目id过滤

else if(str.startsWith("set proj_id")){

str="set proj_id="+StringUtil.formatDbColumn(map.get("PROJ_ID"));

}

//设置备份文件名

else if(str.startsWith("set filename")){

str="set filename="+filename;

}

//桌面路径

else if(str.startsWith("set Desktop")){

str="set Desktop="+getDesktopPath();

}

//dmp文件存放路径**************该路径需要查取的

else if(str.startsWith("set savePth")){

str="set savePth="+getDumpPath();

}

tmpBat.add(str);

}

//关闭缓冲器

br.close();

} catch (IOException e) {

try {

br.close();

} catch (IOException e1) {

e1.printStackTrace();

}

e.printStackTrace();

}

//设定写数据缓冲器   文件指向

PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(Bfile)),true);

for(String str : tmpBat)

{

//按行写入

pw.println(str);

}

//pw.println("del /q BuckupParfile.par");

//pw.println("del /q temp.bat");

//关闭缓冲器

pw.close();

flag="good";

} catch (FileNotFoundException e) {

flag="error";

e.printStackTrace();

}

return flag;

}

/**

* 获取数据泵数据存储路径

* @return

*/

private static String getDumpPath()

{

//select OS_PATH from sys.dir$ where  OS_PATH like '%\dpdump\'

String dumpPath=null;

Connection conn=null;

try {

conn=DBConn.getConnection("***");//该方法自己写

dumpPath = (String)DBUtil.getResultFieldValue(conn, "select OS_PATH from sys.dir$ where  OS_PATH like '%\\dpdump\\'");

} catch (SQLException e) {

Logger.error(e);

}finally{

try

{

conn.close();

} catch (SQLException e)

{

Logger.error(e);

}

}

return dumpPath;

}

/**

* 配置数据泵的参数

* @param map

* @return

*/

private static String productionExpdpPar(Map map)

{

String  proj_id = StringUtil.formatDbColumn(map.get("PROJ_ID"));

//获取模板 文件

File Afile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\processBat\\dp\\BuckupParfile.par");

//生成空白的bat临时文件

File Bfile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\BuckupParfile.par");

try {

//申明读取缓冲器  //设置读取缓冲器 文件指向

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(Afile)));

//设定写数据缓冲器   文件指向

PrintWriter pw= new PrintWriter(new OutputStreamWriter(new FileOutputStream(Bfile)),true);

try {

while(br.ready())//判断是否还有可读信息

{

//读取一行数据

String str=br.readLine().toString();

str=str.replace("%proj_id%", proj_id);

pw.println(str);

}

br.close();

pw.close();

} catch (IOException e) {

e.printStackTrace();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

return "";

}

/**

* 生成导入配置bat文件

* @param map

* @return

*/

private static String productionImpBat(Map map)

{

//获取   rar 备份 文件的名字

String FilePath=StringUtil.formatDbColumn(map.get("FilePath"));

String FileName = FilePath.substring(FilePath.lastIndexOf('\\')+1);

//生成 导入前清理 sql 文件

productionClearSql(map);

Map ConnInfoMap =getConnInfo();

//设置一个标志

String flag="";

//创建个缓存 数据集

List tmpBat=new ArrayList();

//获取模板 文件

File Afile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\processBat\\dp\\impdpDB.bat");

//生成空白的bat临时文件

File Bfile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\temp.bat");

//申明读取缓冲器

BufferedReader br=null;

try {

//设置读取缓冲器 文件指向

br = new BufferedReader(new InputStreamReader(new FileInputStream(Afile)));

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

try {

try {

while(br.ready())//判断是否还有可读信息

{

//读取一行数据

String str=br.readLine().toString();

//设置数据库的实例

if(str.startsWith("set sid")){

str="set sid="+ConnInfoMap.get("sid");

}

//设置数据登录名

else if(str.startsWith("set user")){

str="set user="+ConnInfoMap.get("user");

}

//设置数据登密码

else if(str.startsWith("set pwd")){

str="set pwd="+ConnInfoMap.get("pwd");

}

//设置备份文件 路径

else if(str.startsWith("set FilePath")){

str="set FilePath="+FilePath;

}

//dump路径

else if(str.startsWith("set Dumpfile")){

str="set Dumpfile="+getDumpPath();

}

//文件名

else if(str.startsWith("set FileName")){

str="set FileName="+FileName;

}

tmpBat.add(str);

}

//关闭缓冲器

br.close();

} catch (IOException e) {

try {

br.close();

} catch (IOException e1) {

e1.printStackTrace();

}

e.printStackTrace();

}

//设定写数据缓冲器   文件指向

PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(Bfile)),true);

for(String str : tmpBat)

{

//按行写入

pw.println(str);

}

pw.println("ECHO watting …………………………");

pw.println("sqlplus %user%/%pwd%@%sid%  @UpdateParentId.sql ");

pw.println("del /q UpdateParentId.sql");

pw.println("del /q impClearing.sql");

pw.println("del /q temp.bat");

//关闭缓冲器

pw.close();

flag="good";

} catch (FileNotFoundException e) {

flag="error";

e.printStackTrace();

}

return flag;

}

/**

* 生成导入前sql清理文件

* @param map

* @return

*/

private static String productionClearSql(Map map)

{

//获取项目id

//StringUtil.formatDbColumn(map.get("PROJ_ID"))  这个是即将导入  项目的   新的父id

String proj_id =getProjId(map);

//创建个缓存 数据集

List tmpBat=new ArrayList();

//获取模板 文件

File Afile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\processBat\\dp\\impClearing.sql");

//生成空白的bat临时文件

File Bfile=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\impClearing.sql");

//申明读取缓冲器

BufferedReader br=null;

try {

//设置读取缓冲器 文件指向

br = new BufferedReader(new InputStreamReader(new FileInputStream(Afile)));

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

try {

try {

while(br.ready())//判断是否还有可读信息

{

//读取一行数据

String str=br.readLine().toString();

//设置备份文件名  %proj_id%

str = str.replace("%proj_id%", proj_id);

tmpBat.add(str);

}

//关闭缓冲器

br.close();

} catch (IOException e) {

try {

br.close();

} catch (IOException e1) {

e1.printStackTrace();

}

e.printStackTrace();

}

//设定写数据缓冲器   文件指向

PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(Bfile)),true);

for(String str : tmpBat)

{

//按行写入

pw.println(str);

}

pw.println("exit");

//关闭缓冲器

pw.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

return "";

}

/**

* 生成导入   项目的id  和   更新语句

* @param map

* @return

*/

private static String getProjId(Map map)

{

String FilePath=StringUtil.formatDbColumn(map.get("FilePath"));

FilePath = FilePath.substring(FilePath.lastIndexOf("-")+1, FilePath.lastIndexOf('.'));

File UpParentId=new File(StringUtil.formatDbColumn(map.get("Paths"))+"\\UpdateParentId.sql");

PrintWriter pw;

try {

pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(UpParentId)),true);

pw.println("update cm_proj t  set t.parent_proj_id='"+StringUtil.formatDbColumn(map.get("PROJ_ID"))+"',t.parent_path=('"

+getProjPath(StringUtil.formatDbColumn(map.get("PROJ_ID")))+"'||';'||'"+FilePath+"') where t.proj_id='"+FilePath+"';");

pw.println("commit;");

pw.println("exit");

//关闭缓冲器

pw.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

return FilePath;

}

/**

* 获取父级路径

* @param map

* @return

*/

private static String getProjPath(String proj_id)

{

String projPath=null;

Connection conn=null;

try {

conn=DBConn.getConnection("***");//该方法自己写

projPath = (String)DBUtil.getResultFieldValue(conn, "select  parent_path  from cm_proj  where proj_id ='"+proj_id+"'");

conn.close();

} catch (SQLException e) {

Logger.error(e);

}finally{

try

{

conn.close();

} catch (SQLException e)

{

Logger.error(e);

}

}

return projPath;

}

}0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值