Back up and restore the Oracle database in Java. I don't know if there are any better methods. I hope some of them can also provide the following methods or code.
Package com. servlet. util;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. nio. ByteBuffer;
Import java. nio. channels. Channels;
Import java. nio. channels. FileChannel;
Import java. nio. channels. ReadableByteChannel;
Import java. util. ArrayList;
Import java. util. List;
Import java. util. Map;
/**
* Database backup and Restoration
* @ Author RootSuper
* @ Version 2012-11-12
*/
@ SuppressWarnings ("unused ")
Public class BackupOracleDatabase {
/** User name */
Private String userName;
/** Password */
Private String userPass;
/** Service address */
Private String serverAddr;
/** Backup file path */
Private String backupFilePath;
/** Backup log path */
Private String logPath;
/**
* Constructor
* @ Param userName: database userName
* @ Param userPass Database User Password
* @ Param serverAddr service address: Port/Database Name
* @ Param backupFilePath: backup file path
*/
Public BackupOracleDatabase (String Name, String Pass,
String Addr, String FilePath, String log ){
This. userName = Name;
This. userPass = Pass;
This. serverAddr = Addr;
This. backupFilePath = FilePath;
This. logPath = log;
}
/**
* Get the command string
* @ Param bool whether the command string for restoring the database is the BACKUP command by default
* @ Return commStr command string
*/
Private String GetCommand (boolean bool ){
String commStr = "EXP @ USER @/@ PASSWORD @ SERVER @ FILE = @ FILEPATH @ LOG = @ LOGPATH @ FULL = Y ";
If (bool ){
CommStr = "IMP @ USER @/@ PASSWORD @ SERVER @ FILE = @ FILEPATH @ LOG = @ LOGPATH @ FULL = Y ";
}
String temp = commStr. replaceAll ("@ USER @", this. userName ). replaceAll ("@ PASSWORD @", this. userPass ). replaceAll ("@ SERVER @", this. serverAddr ). replaceAll ("@ FILEPATH @", this. backupFilePath ). replaceAll ("@ LOGPATH @", this. logPath );
System. out. println (temp );
Return temp;
}
/**
* Back up or restore a database
* @ Param bool: Restore database false: Back up database
* @ Return boolean
*/
Public boolean expBackup (boolean bool ){
Runtime rt = Runtime. getRuntime ();
Process processexp = null;
Try {
Processexp = rt.exe c (GetCommand (bool ));
Return true;
} Catch (IOException e ){
E. printStackTrace ();
}
Return false;
}
}