Java web application (back up and restore database)

Source: Internet
Author: User

 
 

Principle: MySQL database backup principle: database interface software such as Navicat Common Database Backup principle is direct

Call the system commands of MYSQL.

 

MySQL system command:

-- Opt-h localhost -- user = root -- password = admin -- lock-all-tables = true -- result-file = E: // oes // 2221. SQL -- default-character-set = utf8 oes

Resolution: Host-h, user name -- user, password-password, lock all tables -- lock-all-tables = true,

Target file -- result-file, encoding -- default-character-set = utf8, data source oes

 

Java:

Runtime cmd = Runtime. getRuntime ();

Process p = cmd.exe c (""); // execute the CMD command (String)

 

Unable to determine whether the MySQL environment variable is configured on the host, it is safe to determine the location of mysqldump in MySQL. It exists in the Bin directory of the MySQL installation folder, the problem is how to obtain the MySQL installation directory?

 

----------------------------------------------------- >>>>>>>>

 

For obtaining the installation directory of MySQL, I used a stupid method: parsing the registry.

Find the MySQL software information in the registry, including the installation address, uninstall address, version number, and other basic information of the software, and directly use its installation information.

 

Location of software information in the registry:

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall \

 

Software Information in the Software Association registry:

 

----------------------------------------------------- >>>>>>>>

Class CheckSoftware, parsing the MySQL software installation address

 

Import java. io. BufferedReader;

Import java. io. IOException;

Import java. io. InputStreamReader;

 

Public class CheckSoftware {

/*

* Traverse the registry and query the MySQL Registry Association.

*/

Public static String check () throws Exception {

Runtime runtime = Runtime. getRuntime ();

Process process = null;

Process = runtime

. Exec ("cmd/c reg query HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall \\");

BufferedReader in = new BufferedReader (new InputStreamReader (

Process. getInputStream ()));

String string = null;

While (string = in. readLine ())! = Null ){

Process = runtime.exe c ("cmd/c reg query" + string

+ "/V DisplayName ");

BufferedReader name = new BufferedReader (new InputStreamReader (

Process. getInputStream ()));

String message = queryValue (string, "DisplayName ");

If (message! = Null & message. contains ("MySQL ")){

String message2 = queryValue (string, "InstallLocation ");

Return message2;

}

 

}

In. close ();

Process. destroy ();

Return null;

}

 

/*

* Query the installation path of the MySQL service.

*/

Private static String queryValue (String string, String method)

Throws IOException {

String pathString = "";

 

Runtime runtime = Runtime. getRuntime ();

Process process = null;

BufferedReader br = null;

Process = runtime.exe c ("cmd/c reg query" + string + "/v" + method );

Br = new BufferedReader (new InputStreamReader (process. getInputStream ()));

Br. readLine ();

Br. readLine (); // Remove useless information from the first two rows

If (pathString = br. readLine ())! = Null ){

PathString = pathString. replaceAll (method + "REG_SZ", ""); // Remove useless information

Return pathString;

}

Return pathString;

}

}

 

 

----------------------------------------------------- >>>>>>>>

JavaMysql-like backup and restoration of databases

 

Import java. io. File;

Import java. io. IOException;

Import java. io. InputStream;

Import java. util. Properties;

 

Public class JavaMysql {

/*

* Backup database 1. Read the configuration file 2. Start the smart query Mysql installation directory 3. The backup database is an SQL File

*/

Public static void backup (String SQL ){

 

Properties pros = getPprVue ("prop. properties ");

String username = pros. getProperty ("username ");

String password = pros. getProperty ("password ");

 

CheckSoftware c = null;

Try {

System. out. println ("MySQL service installation address:" + c. check (). toString ());

} Catch (Exception e2 ){

E2.printStackTrace ();

}

String mysqlpaths;

Try {

Mysqlpaths = c. check (). toString () + "bin" + "\\";

 

String databaseName = pros. getProperty ("databaseName ");

String address = pros. getProperty ("address ");

String sqlpath = pros. getProperty ("SQL ");

File backupath = new File (sqlpath );

If (! Backupath. exists ()){

Backupath. mkdir ();

}

 

StringBuffer sb = new StringBuffer ();

 

Sb. append (mysqlpaths );

Sb. append ("mysqldump ");

Sb. append ("-- opt ");

Sb. append ("-h ");

Sb. append (address );

Sb. append ("");

Sb. append ("-- user = ");

Sb. append (username );

Sb. append ("");

Sb. append ("-- password = ");

Sb. append (password );

Sb. append ("");

Sb. append ("-- lock-all-tables = true ");

Sb. append ("-- result-file = ");

Sb. append (sqlpath );

Sb. append (SQL );

Sb. append ("");

Sb. append ("-- default-character-set = utf8 ");

Sb. append (databaseName );

System. out. println ("cmd command:" + sb. toString ());

Runtime cmd = Runtime. getRuntime ();

Try {

Process p = cmd.exe c (sb. toString ());

} Catch (IOException e ){

E. printStackTrace ();

}

} Catch (Exception e1 ){

E1.printStackTrace ();

}

}

 

/*

* Read attribute files

*/

Public static Properties getPprVue (String properName ){

 

InputStream inputStream = JavaMysql. class. getClassLoader ()

. GetResourceAsStream (properName );

Properties p = new Properties ();

 

Try {

P. load (inputStream );

InputStream. close ();

} Catch (IOException e ){

E. printStackTrace ();

}

 

Return p;

 

}

 

/*

* Recover a database based on the backup file

*/

Public static void load (String filename ){

Properties pros = getPprVue ("prop. properties ");

String root = pros. getProperty ("jdbc. username ");

String pass = pros. getProperty ("jdbc. password ");

String mysqlpaths = c. check (). toString () + "bin" + "\\";

String sqlpath = pros. getProperty ("SQL ");

String filepath = mysqlpaths + sqlpath + filename; // backup path address

 

String stmt1 = mysqlpaths + "mysqladmin-u" + root + "-p" + pass

+ "Create finacing"; // your password is added after-p.

String stmt2 = mysqlpaths + "mysql-u" + root + "-p" + pass

+ "Finacing <" + filepath;

String [] cmd = {"cmd", "/c", stmt2 };

Try {

Runtime.getruntime(cmd.exe c (stmt1 );

Runtime.getruntime(cmd.exe c (cmd );

System. out. println ("data has been imported from" + filepath + "to Database ");

} Catch (IOException e ){

E. printStackTrace ();

}

 

}

 

/*

* Test

*/

Public static void main (String [] args) throws IOException {

Backup ("2221. SQL ");

}

}

 

----------------------------------------------------- >>>>>>>>

Property file: prop. properties, dynamic configuration of basic attributes such as user name and password

 

Username = root

Password = admin

SQL = E: // oes //

Address = localhost

DatabaseName = oes

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.