0

0

Java 调用bat执行的备份Oracle数据库 类

php中文网

php中文网

发布时间:2016-06-07 17:01:42

|

1066人浏览过

|

来源于php中文网

原创

package com.buckupDB;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.Fil

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)));

SuperCms在线订餐系统
SuperCms在线订餐系统

模板采用响应式设计,自动适应手机,电脑及平板显示;满足单一店铺外卖需求。功能:1.菜单分类管理2.菜品管理:菜品增加,删除,修改3.订单管理4.友情链接管理5.数据库备份6.文章模块:如:促销活动,帮助中心7.单页模块:如:企业信息,关于我们更强大的功能在开发中……安装方法:上传到网站根目录,运行http://www.***.com/install 自动

下载

    //设定写数据缓冲器   文件指向
    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;
  }

}

linux

相关文章

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 0.6万人学习

Node.js 教程
Node.js 教程

共57课时 | 7.2万人学习

CSS3 教程
CSS3 教程

共18课时 | 4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号