Four Methods for backing up databases in Java (MySQL and MSSQL database backup)

Source: Internet
Author: User

MySQL database backup, and then studies how to call the CMD command in Java to implement the backup function of mysqldump. The specific implementation is as follows:

1. Set the environment variable first: there are two ways. First, add mysql_home to the environment variable and set the content
MySQL installation path, and then add the path in Path
% Mysql_home % Bin
. The second method is not to create mysql_home, but to directly add
MySQL installation path bin. In this way, you can directly find the mysqldump and MySQL commands when calling cmd.

2. Now write Java method classes to back up and restore databases. The Code is as follows:
Import java. Io. ioexception;

Public class db_deal
{


Private Static string
STR = NULL;


Public static void backup (){

// Use mysqldump to back up the database, in the format of "mysqldump-u username-ppassword -- OPT
Database_name> direction/backup_name. SQL"

STR = "mysqldump-u root-proot
-- Opt hsf-
>
D:/test. SQL ";


Try {

Runtime RT = runtime. getruntime ();

Rt.exe C ("CMD/C" + Str );

// Runtime.getruntime(cmd.exe C (
) This method can call the command. For details, see API

// You can run cmd to call the console and then execute the string representation command in Str.

System. Out. println ("successly! ");

} Catch (ioexception e ){

E. printstacktrace ();

System. Out. println ("something
Wrong! ");


}


}


Public static void load (){

STR = "mysql-u root-proot j2603
<D:/test. SQL ";

// The MySQL command can restore the database. Format: mysql-u Username
-Ppassword
Database_name
<
Back_up_dir"

Runtime RT = runtime. getruntime ();

Try {

Rt.exe C ("CMD/C" + Str );

System. Out. println ("Restore successly! ");

} Catch (ioexception e ){

E. printstacktrace ();

System. Out. println ("Restore fail! ");

}

}
}

3. instantiate the class at the end of the backup, and then call backup () and load () to implement simple backup and restoration of the MySQL database.

// Let's take a look at the backup MSSQL database code.

String
Webtruepath = getservletcontext (). getrealpath (request. getservletpath (); // obtain the actual Servlet Path

Java. Io. File file = new java. Io. File (webtruepath );
File = file. getparentfile ();
String
Path = file. getpath () + "/../Web-INF/lib/" + name + ". dbbak"; // name file name

String baksql = "backup database school to disk =? With
Init "; // SQL statement
Java. SQL. preparedstatement
Bak = DBs. getconnection (). preparestatement (baksql );
Bak. setstring (1, PATH); // path must be an absolute path
If (! Bak.exe cute () over = "backup successful ";
Else Over = "backup failed ";
Bak. Close ();
Database recovery
If (! DBS. Close ()){

Over = "failed to close all links ";
} Else {

String
Webtruepath = getservletcontext (). getrealpath (request. getservletpath ());


Java. Io. File file = new java. Io. File (webtruepath );

File = file. getparentfile ();

String Path = file. getpath () + "/../Web-INF/lib/" + name;

String resql = "Restore database school from disk =? With
Replace ";

Class. forname (dbinf. getdriverclassname ());

Java. SQL. Connection
Con = drivermanager. getconnection (dbinf. getmester ());

Java. SQL. preparedstatement
Restmt = con. preparestatement (resql );

Restmt. setstring (1, PATH); // The path must be an absolute path.

If (! Restmt.exe cute () over = "restored successfully ";

Else Over = "recovery failed ";

Restmt. Close ();

Con. Close ();
}

It's a bit annoying to recover. We need to close all connections to the database. Here I am connected to mestar. Actually, it is okay.
JDBC: Microsoft: sqlserver: // localhost: 1433; user = username; Password = pass

Connect to the database server without connecting to the specified database.

Original article: http://blog.sina.com.cn/s/blog_67af0caa0100lgxm.html

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.