The source code recorded in the qmaster category is a part of the code that I think is well written and useful (highly reusable), not all of the source code of the qmaster. Welcome to comments.
Dbconfig. xml
<? XML version = "1.0" encoding = "gb2312"?>
<! -Database configuration file->
<Dbconfig>
<Jdbcdriver> com. MySQL. JDBC. Driver </jdbcdriver> <! -JDBC driver->
<Jdbcurl> JDBC: mysql: // localhost: 3306/questionnaire </jdbcurl> <! -Jdbc URL->
<Dbuser> root </dbuser> <! -Database username->
<Dbpwd> yarpee19890607 </dbpwd> <! -Database Password->
</Dbconfig>
Dbconfig. Java
/**
* Function: parse the dbconfig. xml file and provide database configuration.
*/
Package dB;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import javax. xml. parsers. documentbuilderfactory;
Import javax. xml. parsers. parserconfigurationexception;
Import org. W3C. Dom. Document;
Import org. W3C. Dom. element;
Import org. W3C. Dom. node;
Import org. W3C. Dom. nodelist;
Import org. xml. Sax. saxexception;
Public class dbconfig {
// Corresponding to the configuration in dbconfig. xml
Private string jdbcdriver;
Private string jdbcurl;
Private string dbuser;
Private string dbpwd;
Dbconfig () throws parserconfigurationexception, saxexception, ioexception {
Documentbuilderfactory docbuildfac = documentbuilderfactory. newinstance (); // generate a DOM parser factory
Inputstream is = This. getclass (). getclassloader (). getresourceasstream ("dbconfig. xml"); // relative position src/dbconfig. xml
Document Doc = docbuildfac. newdocumentbuilder (). parse (is); // generate a DOM parser and parse dbconfig. XML to generate a doc.
Element root = Doc. getdocumentelement (); // Root Node
Nodelist = root. getchildnodes (); // subnode linked list of the Root Node
For (INT I = 0; I <nodelist. getlength (); I ++ ){
Node node = nodelist. item (I); // subnode
If (node. getnodetype () = node. element_node ){
If (node. getnodename (). Equals ("jdbcdriver ")){
Jdbcdriver = node. getfirstchild (). getnodevalue (); // Dom treats <node> value </node> as a two-layer node
} Else if (node. getnodename (). Equals ("jdbcurl ")){
Jdbcurl = node. getfirstchild (). getnodevalue ();
} Else if (node. getnodename (). Equals ("dbuser ")){
Dbuser = node. getfirstchild (). getnodevalue ();
} Else if (node. getnodename (). Equals ("dbpwd ")){
Dbpwd = node. getfirstchild (). getnodevalue ();
}
}
}
}
Public String getdbpwd (){
Return dbpwd;
}
Public String getdbuser (){
Return dbuser;
}
Public String getjdbcdriver (){
Return jdbcdriver;
}
Public String getjdbcurl (){
Return jdbcurl;
}
}
DB. Java
/**
* Function: connect to and close the database
*/
Package dB;
Import java. Io. ioexception;
Import java. SQL .*;
Import javax. xml. parsers. parserconfigurationexception;
Import org. xml. Sax. saxexception;
Public class dB {
Private dbconfig Config = NULL;
Private connection conn = NULL;
Public dB () throws classnotfoundexception, parserconfigurationexception, saxexception, ioexception {
Config = new dbconfig ();
Class. forname (config. getjdbcdriver ());
}
Public connection getconnection () throws sqlexception {
Conn = drivermanager. getconnection (config. getjdbcurl (), config. getdbuser (), config. getdbpwd ());
Return conn;
}
Public void close () throws sqlexception {
If (Conn! = NULL)
Conn. Close ();
}
}
Dbbak. Java
/**
* Function: Back up and restore a database
*/
Package dB;
Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. outputstream;
Import java. Io. outputstreamwriter;
Import java. util. calendar;
Import javax. xml. parsers. parserconfigurationexception;
Import org. xml. Sax. saxexception;
Public class dbbak {
Private dbconfig Config = NULL;
Public dbbak () throws parserconfigurationexception, saxexception, ioexception {
Config = new dbconfig ();
}
Public String getfilename (){
Calendar today = calendar. getinstance ();
String year = string. valueof (today. Get (calendar. Year ));
String month = string. valueof (today. Get (calendar. month) + 1); // 0 ~ 11 indicates 1 ~ December
String date = string. valueof (today. Get (calendar. Date ));
String time = string. valueof (today. gettimeinmillis ());
Return year + "-" + month + "-" + date + "-" + time + ". SQL ";
}
Public void backup (string path ){
Try {
String jdbcurl = config. getjdbcurl ();
Int Pos = jdbcurl. lastindexof ("/");
String dbname = jdbcurl. substring (Pos + 1 );
String cmd = "mysqldump-U" + config. getdbuser () + "-P" + config. getdbpwd () + "" + dbname;
Runtime RT = runtime. getruntime ();
Process child = rt.exe C (CMD );
Inputstream in = Child. getinputstream ();
Inputstreamreader xx = new inputstreamreader (in, "ISO-8859-1 ″);
String instr;
Stringbuffer sb = new stringbuffer ("");
String outstr;
Bufferedreader BR = new bufferedreader (XX );
While (instr = Br. Readline ())! = NULL ){
SB. append (instr + "/R/N ");
}
Outstr = new string (sb. tostring (). getbytes ("ISO-8859-1"), "UTF-8 ″);
Fileoutputstream fout = new fileoutputstream (PATH );
Outputstreamwriter writer = new outputstreamwriter (fout, "UTF-8 ″);
Writer. Write (outstr );
Writer. Flush ();
In. Close ();
XX. Close ();
BR. Close ();
Writer. Close ();
Fout. Close ();
}
Catch (exception e ){
E. printstacktrace ();
}
}
Public void load (string path ){
Try {
File F = new file (PATH );
Fileinputstream FCM = new fileinputstream (f );
String jdbcurl = config. getjdbcurl ();
Int Pos = jdbcurl. lastindexof ("/");
String dbname = jdbcurl. substring (Pos + 1 );
String cmd = "mysql-U" + config. getdbuser () + "-P" + config. getdbpwd () + "" + dbname;
Runtime RT = runtime. getruntime ();
Process child = rt.exe C (CMD );
Outputstream out = Child. getoutputstream ();
String instr;
Stringbuffer sb = new stringbuffer ("");
String outstr;
Bufferedreader BR = new bufferedreader (New inputstreamreader ("UTF-8 ″));
While (instr = Br. Readline ())! = NULL ){
SB. append (instr + "/R/N ");
}
Outstr = new string (sb. tostring (). getbytes ("UTF-8"), "ISO-8859-1 ″);
Outputstreamwriter writer = new outputstreamwriter (Out, "ISO-8859-1 ″);
Writer. Write (outstr );
Writer. Flush ();
Out. Close ();
BR. Close ();
Writer. Close ();
FCM. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}
Filelist. Java
/**
* Function: List database backup files.
*/
Package dB;
Import java. Io. file;
Import java. util. arraylist;
Public class filelist {
Public arraylist tree (file F) {// input directory
Arraylist files = new arraylist ();
File [] list = f. listfiles ();
If (list! = NULL ){
For (INT I = 0; I <list. length; I ++ ){
If (list [I]. isfile ()){
If (list [I]. getname (). endswith (". SQL ")){
Files. Add (list [I]. getname ());
}
}
}
}
Return files;
}
}