Java calls the backup Oracle Database Class executed by bat

Source: Internet
Author: User
Packagecom. buckupDB; importjava. io. BufferedReader; importjava. io. File; importjava. io. FileInputStream; importjava. io. Fil

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;


/**
*
* Data Backup impdp/expdp
* @ Author ZQD32
*
*/
Public class BuckupdpDB {

/**
* // Obtain the desktop path
* @ Return
*/
Private static String getDesktopPath ()
{
FileSystemView fsv = FileSystemView. getFileSystemView ();
File file = fsv. getHomeDirectory ();
Return file. getPath ();
}


/**
* Get Time Characters
* @ Return
*/
Private static String getDateTime ()
{
Date date = new Date ();
String str = date. toLocaleString ();
Str = str. replaceAll (":","-");
Str = str. replaceAll ("","-");
Return str;
}

/** Enable the specified process
* @ 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(cmd.exe c (sb. toString ());
}
Catch (IOException e)
{
E. printStackTrace ();
}
}

/**
* Execute the backup file
* @ Return
* @ Throws IOException
*/
Public static String expingProcess (Map Map)
{
If (productionExpBat (map). equalsIgnoreCase ("good "))
{
RuningProcess (map );
}
Else
{
Return "false ";
}
Return "true ";
}

/**
* Execute the import File
* @ Return
* @ Throws IOException
*/
Public static String impingProcess (Map Map)
{
If (productionImpBat (map). equalsIgnoreCase ("good "))
{
RuningProcess (map );
}
Else
{
Return "false ";
}
Return "true ";
}

/**
* Get connection information
* @ 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 ();
// Obtain the Oracle database instance name
Map. put ("sid", url. substring (url. lastIndexOf (":") + 1 ));
// Obtain the oracle database Logon Name
Map. put ("user", init. getUsername ());
// Obtain the oracle database Login Password
Map. put ("pwd", init. getPassword ());
} Catch (NamingException e ){
E. printStackTrace ();
}
Return map;
}


/**
*
* Generate the export configuration Bat file
*/
Private static String productionExpBat (Map Map)
{
// Obtain the database logon information
Map ConnInfoMap = getConnInfo ();
// Generate the Data Pump Configuration File
Productionexpdpha (map );
// Set a flag
String flag = "";
// Create a cache Dataset
List TmpBat = new ArrayList ();
// Obtain the Template File
File Afile = new File (StringUtil. formatDbColumn (map. get ("Paths") + "\ processBat \ dp \ expdpDB. bat ");
// Generate a blank temporary bat file
File Bfile = new File (StringUtil. formatDbColumn (map. get ("Paths") + "\ temp. bat ");
// Declare read buffer
BufferedReader br = null;
Try {
// Set the direction of the read buffer File
Br = new BufferedReader (new InputStreamReader (new FileInputStream (Afile )));
} Catch (FileNotFoundException e1 ){
E1.printStackTrace ();
}
// Obtain the name of the rar backup file
String filename = StringUtil. formatDbColumn (map. get ("FILENAME "));
If (filename = null | filename. equals ("")){
// The default value is DBProjBuckup.
Filename = "DBProjBuckup ";
}
Filename = filename + "_" + getDateTime ();

Try {
Try {
While (br. ready () // determines whether there is any readable information
{
// Read a row of data
String str = br. readLine (). toString ();
// Set the database instance
If (str. startsWith ("set sid ")){
Str = "set sid =" + ConnInfoMap. get ("sid ");
}
// Set the data Login Name
Else if (str. startsWith ("set user ")){
Str = "set user =" + ConnInfoMap. get ("user ");
}
// Set the data Login Password
Else if (str. startsWith ("set pwd ")){
Str = "set pwd =" + ConnInfoMap. get ("pwd ");;
}
// Set backup Project id Filtering
Else if (str. startsWith ("set proj_id ")){
Str = "set proj_id =" + StringUtil. formatDbColumn (map. get ("PROJ_ID "));
}
// Set the backup file name
Else if (str. startsWith ("set filename ")){
Str = "set filename =" + filename;
}
// Desktop path
Else if (str. startsWith ("set Desktop ")){
Str = "set Desktop =" + getDesktopPath ();
}
// Dmp file storage path ************** this path needs to be queried
Else if (str. startsWith ("set savePth ")){
Str = "set savePth =" + getDumpPath ();
}
TmpBat. add (str );
}
// Disable the buffer.
Br. close ();
} Catch (IOException e ){
Try {
Br. close ();
} Catch (IOException e1 ){
E1.printStackTrace ();
}
E. printStackTrace ();
}

// Set the direction of the write data buffer File
PrintWriter pw = new PrintWriter (new OutputStreamWriter (new FileOutputStream (Bfile), true );
For (String str: tmpBat)
{
// Write data by row
Pw. println (str );
}
// Pw. println ("del/q BuckupParfile. par ");
// Pw. println ("del/q temp. bat ");

// Disable the buffer.
Pw. close ();

Flag = "good ";
} Catch (FileNotFoundException e ){
Flag = "error ";
E. printStackTrace ();
}
Return flag;
}

/**
* Obtain the data storage path of the Data Pump
* @ 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 ("***"); // you can write this method by yourself.
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;
}

/**
* Configure data pump parameters
* @ Param map
* @ Return
*/
Private static String productionexpdpha (Map Map)
{
String proj_id = StringUtil. formatDbColumn (map. get ("PROJ_ID "));
// Obtain the Template File
File Afile = new File (StringUtil. formatDbColumn (map. get ("Paths") + "\ processBat \ dp \ BuckupParfile. par ");
// Generate a blank temporary bat file
File Bfile = new File (StringUtil. formatDbColumn (map. get ("Paths") + "\ BuckupParfile. par ");
Try {
// Declare read buffer // set the object pointing
BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (Afile )));

// Set the direction of the write data buffer File
PrintWriter pw = new PrintWriter (new OutputStreamWriter (new FileOutputStream (Bfile), true );

Try {
While (br. ready () // determines whether there is any readable information
{
// Read a row of data
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 "";
}

/**
* Generate the import configuration bat file
* @ Param map
* @ Return
*/
Private static String productionImpBat (Map Map)
{
// Obtain the name of the rar backup file
String FilePath = StringUtil. formatDbColumn (map. get ("FilePath "));
String FileName = FilePath. substring (FilePath. lastIndexOf ('\') + 1 );

// Clear the SQL file before the import.
ProductionClearSql (map );

Map ConnInfoMap = getConnInfo ();



// Set a flag
String flag = "";
// Create a cache Dataset
List TmpBat = new ArrayList ();
// Obtain the Template File
File Afile = new File (StringUtil. formatDbColumn (map. get ("Paths") + "\ processBat \ dp \ impdpDB. bat ");
// Generate a blank temporary bat file
File Bfile = new File (StringUtil. formatDbColumn (map. get ("Paths") + "\ temp. bat ");
// Declare read buffer
BufferedReader br = null;
Try {
// Set the direction of the read buffer File
Br = new BufferedReader (new InputStreamReader (new FileInputStream (Afile )));
} Catch (FileNotFoundException e1 ){
E1.printStackTrace ();
}

Try {
Try {
While (br. ready () // determines whether there is any readable information
{
// Read a row of data
String str = br. readLine (). toString ();
// Set the database instance
If (str. startsWith ("set sid ")){
Str = "set sid =" + ConnInfoMap. get ("sid ");
}
// Set the data Login Name
Else if (str. startsWith ("set user ")){
Str = "set user =" + ConnInfoMap. get ("user ");
}
// Set the data Login Password
Else if (str. startsWith ("set pwd ")){
Str = "set pwd =" + ConnInfoMap. get ("pwd ");
}
// Set the backup file path
Else if (str. startsWith ("set FilePath ")){
Str = "set FilePath =" + FilePath;
}
// Dump path
Else if (str. startsWith ("set Dumpfile ")){
Str = "set Dumpfile =" + getDumpPath ();
}
// File name
Else if (str. startsWith ("set FileName ")){
Str = "set FileName =" + FileName;
}
TmpBat. add (str );
}
// Disable the buffer.
Br. close ();
} Catch (IOException e ){
Try {
Br. close ();
} Catch (IOException e1 ){
E1.printStackTrace ();
}
E. printStackTrace ();
}

// Set the direction of the write data buffer File
PrintWriter pw = new PrintWriter (new OutputStreamWriter (new FileOutputStream (Bfile), true );
For (String str: tmpBat)
{
// Write data by row
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 ");
// Disable the buffer.
Pw. close ();
Flag = "good ";
} Catch (FileNotFoundException e ){
Flag = "error ";
E. printStackTrace ();
}

Return flag;
}

/**
* Generate SQL statements to clear files before import
* @ Param map
* @ Return
*/
Private static String productionClearSql (Map Map)
{
// Obtain the Project id
// StringUtil. formatDbColumn (map. get ("PROJ_ID") is the new parent id of the project to be imported.
String proj_id = getProjId (map );
// Create a cache Dataset
List TmpBat = new ArrayList ();
// Obtain the Template File
File Afile = new File (StringUtil. formatDbColumn (map. get ("Paths") + "\ processBat \ dp \ impClearing. SQL ");
// Generate a blank temporary bat file
File Bfile = new File (StringUtil. formatDbColumn (map. get ("Paths") + "\ impClearing. SQL ");
// Declare read buffer
BufferedReader br = null;
Try {
// Set the direction of the read buffer File
Br = new BufferedReader (new InputStreamReader (new FileInputStream (Afile )));
} Catch (FileNotFoundException e1 ){
E1.printStackTrace ();
}
Try {
Try {
While (br. ready () // determines whether there is any readable information
{
// Read a row of data
String str = br. readLine (). toString ();
// Set the backup file name % proj_id %
Str = str. replace ("% proj_id %", proj_id );
TmpBat. add (str );
}
// Disable the buffer.
Br. close ();
} Catch (IOException e ){
Try {
Br. close ();
} Catch (IOException e1 ){
E1.printStackTrace ();
}
E. printStackTrace ();
}

// Set the direction of the write data buffer File
PrintWriter pw = new PrintWriter (new OutputStreamWriter (new FileOutputStream (Bfile), true );
For (String str: tmpBat)
{
// Write data by row
Pw. println (str );
}
Pw. println ("exit ");
// Disable the buffer.
Pw. close ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
}

Return "";
}


/**
* Generate the import Project id and update statement
* @ 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 ");
// Disable the buffer.
Pw. close ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
}
Return FilePath;
}

/**
* Obtain the parent path
* @ Param map
* @ Return
*/
Private static String getProjPath (String proj_id)
{
String projPath = null;
Connection conn = null;
Try {
Conn = DBConn. getConnection ("***"); // you can write this method by yourself.
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;
}

}

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.