Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. filereader;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. outputstream;
Import java. Io. outputstreamwriter;
Import java. Io. unsupportedencodingexception;
Import java. util. Logging. level;
Import java. util. Logging. Logger;
/**
*
* Database operations
* Used to back up and restore Data
* 2012-05-08
* Wu Hui
*/
Public class mysqldb {
/**
* Back up data
* @ Param dbname: name of the backup database
* @ Param accname: User Name for access
* @ Param password: User Password
* @ Param backupfilepath: Full backup file path
* @ Param sqlbinaddress: mysqlbin path
* @ Throws ioexception
*/
Public static void backupdb (string dbname, string accname, string password, string backupfilepath, string sqlbinaddress) throws ioexception {
String command = sqlbinaddress + "mysqldump-U" + accname + "-P" + password + "" + dbname;
Inputstream in = NULL;
Inputstreamreader ISR = NULL;
Bufferedreader BR = NULL;
Outputstreamwriter writer = NULL;
Fileoutputstream fout = NULL;
Try {
Runtime RT = runtime. getruntime ();
Process child = rt.exe C (command );
In = Child. getinputstream (); // the output information of the console is used as the input stream.
ISR = new inputstreamreader (in, "utf8"); // sets the output stream encoding to utf8. This must be utf8; otherwise, garbled characters are read from the stream.
// Combine the console output string
BR = new bufferedreader (ISR );
String line;
Stringbuilder sb = new stringbuilder ("");
While (line = Br. Readline ())! = NULL ){
SB. append (Line). append ("\ r \ n ");
}
// Write an object
Fout = new fileoutputstream (backupfilepath );
Writer = new outputstreamwriter (fout, "utf8 ");
Writer. Write (sb. tostring ());
// Note: if the file is written as a buffer, Chinese characters may be garbled. You can avoid using the flush () method.
Writer. Flush ();
} Catch (unsupportedencodingexception ex ){
Logger. getlogger (mysqldb. Class. getname (). Log (level. Severe, null, ex );
} Catch (ioexception ex ){
Logger. getlogger (mysqldb. Class. getname (). Log (level. Severe, null, ex );
} Finally {
Mysqldb. closestream (in, ISR, BR, writer, fout );
}
}
/**
* Recover data
* @ Param dbname: Database Name
* @ Param accname: User Name
* @ Param passwor: Password
* @ Param revertfilepath: full file path
* @ Param sqlbinaddress: mysqlbin path
*/
Public static void revertdb (string dbname, string accname, string password, string revertfilepath, string sqlbinaddress) throws ioexception {
String command = sqlbinaddress + "mysql-U" + accname + "-P" + password + "" + dbname;
Inputstream in = NULL;
Inputstreamreader ISR = NULL;
Bufferedreader BR = NULL;
Outputstreamwriter writer = NULL;
Fileoutputstream fout = NULL;
Try {
Runtime RT = runtime. getruntime ();
Process = rt.exe C (command );
Outputstream out = process. getoutputstream ();
Stringbuilder sb = new stringbuilder ("");
BR = new bufferedreader (New filereader (new file (revertfilepath )));
String line = NULL;
While (line = Br. Readline ())! = NULL ){
SB. append (Line). append ("\ r \ n ");
}
Writer = new outputstreamwriter (Out, "utf8 ");
Writer. Write (sb. tostring ());
Writer. Flush ();
Out. Close ();
BR. Close ();
Writer. Close ();
} Catch (unsupportedencodingexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
Mysqldb. closestream (in, ISR, BR, writer, fout );
}
}
/**
* Close the stream
* @ Param in
* @ Param ISR
* @ Param br
* @ Param writer
* @ Param font
*/
Public static void closestream (inputstream in, inputstreamreader ISR, bufferedreader Br, outputstreamwriter writer, fileoutputstream fout) throws ioexception {
If (in! = NULL ){
In. Close ();
}
If (ISR! = NULL ){
ISR. Close ();
}
If (BR! = NULL ){
BR. Close ();
}
If (writer! = NULL ){
Writer. Close ();
}
If (fout! = NULL ){
Fout. Close ();
}
}
}