Java Backup Database tutorial four ways (MySQL tutorial, MSSQL database Backup)
MySQL database backup things, and then studied how the Java language to invoke cmd command to implement mysqldump backup function. Specifically implemented as follows:
1. Set up environment variables first: There are two ways. First, add mysql_home to the environment variable, set the content to the MySQL installation path, and then add the path%mysql_home%bin. The second method, instead of creating a new mysql_home, adds the MySQL installation path bin directly to the path. In this way, the call to CMD can be directly found mysqldump and MySQL commands.
2. Now write Java method classes to implement backup and restore of the database. 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, format "mysqldump-u username-ppassword--opt database_name > direction/backup_name.sql"
Str= "Mysqldump-u root-proot--opt hjh > D:/test.sql";
try {
Runtime Rt=runtime.getruntime ();
Rt.exec ("cmd/c" +str);
Runtime.getruntime (). EXEC () This method enables the invocation of a command. See the API for specific content
The above can cmd invoke the console, and then execute the command in the string representation 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 enables restore of the database. Format "Mysql-u Username-ppassword database_name < Back_up_dir"
Runtime Rt=runtime.getruntime ();
try {
Rt.exec ("cmd/c" +str);
System.out.println ("Restore successly!");
catch (IOException e) {
E.printstacktrace ();
System.out.println ("Restore fail!");
}
}
}
3. The last place to back up the class instantiation, and then call Backup (), load () to achieve a simple MySQL database backup and restore
Here's a look at backup MSSQL database code
String Webtruepath=getservletcontext (). Getrealpath (Request.getservletpath ());//Take the true path of the servlet
Java.io.file file=new Java.io.file (Webtruepath);
File=file.getparentfile ();
String Path=file.getpath () + "\.. \web-inf\lib\ "+name+". Dbbak ";//name filename
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.execute ()) over= "Backup succeeded";
else over= "Backup failed";
Bak.close ();
Database recovery
if (!dbs.close ()) {
Over= "Close all links failed";
}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);//path must be an absolute path
if (!restmt.execute ()) over= "restore Success";
else over= "restore failed";
Restmt.close ();
Con.close ();
}
Recovery time, a bit annoying, to close all the connection with the database, here I even mestar, can actually
Jdbc:microsoft:sqlserver://localhost:1433;user=username;password=pass
Connect to the database server, not the specified database. That's okay.