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

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

php网站源码教程大全
php网站源码教程大全

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

4

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

7

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

42

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

4

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

3

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

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

精品课程

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

共4课时 | 0.6万人学习

Node.js 教程
Node.js 教程

共57课时 | 7.7万人学习

CSS3 教程
CSS3 教程

共18课时 | 4.1万人学习

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

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