How to use java MySql database backup and recovery procedures?
Advertisements
Note: To add to the mysql bin directory in the Path environment variable
MySql database will be exported to a file backup
import java.io. *;
import java.lang .*;
public class BeiFen {
public static void main (String [] args) {
/ / Database Export
String user = "root"; / / database account
String password = "root"; / / login password
String database = "test"; / / need to back up the database name
String filepath = "e: \ \ test.sql"; / / address of the backup path
String stmt1 = "D: \ \ mysql5027 \ \ bin \ \ mysqldump" + database + "-u" + user + "-p"
+ Password + "- result-file =" + filepath;
/ *
* String stmt1 = "mysqldump" + database + "-u" + user + "-p"
* + Password + "- result-file =" + filepath;
*
* String mysql = "mysqldump test-u root-proot
* - Result-file = d: \ \ test.sql ";
* /
try {
Runtime.getRuntime (). Exec (stmt1);
System.out.println ("data has been exported to file" + filepath + "in");
}
catch (IOException e) {
e.printStackTrace ();
}
}
}
Data from a text file on the disk to restore the database to MySql
import java.io. *;
import java.lang .*;
/ *
* Restoring MySql database
* * /
public class Recover {
public static void main (String [] args) {
String filepath = "d: \ \ test.sql"; / / address of the backup path
/ / Create database test
String stmt1 = "mysqladmin-u root-proot create test";
String stmt2 = "mysql-u root-proot test <" + filepath;
String [] cmd = {"cmd", "/ c", stmt2};
try {
Runtime.getRuntime (). Exec (stmt1);
Runtime.getRuntime (). Exec (cmd);
System.out.println ("data from" + filepath + "into the database");
} Catch (IOException e) {
e.printStackTrace ();
}
}
}
Another program
package jakie;
/ / Import java.io. *;
public class Backup {
private String user_name; / / database user name
private String user_psw; / / database password
private String db_name; / / need to back up the database name
private String host_ip;
private String user_charset;
private String backup_path; / / store the backup file path
private String stmt;
public Backup (String user_name, String user_psw, String db_name, String host_ip, String user_charset, String backup_path) {
this.user_name = user_name;
this.user_psw = user_psw;
this.db_name = db_name;
/ / Host IP;
if (host_ip == null | | host_ip.equals (""))
this.host_ip = "localhost"; / / default to native
else
this.host_ip = host_ip;
/ / Character set
if (user_charset == null | | user_charset.equals (""))
this.user_charset = ""; / / default to set the character set installed
else
this.user_charset = "- default-character-set =" + user_charset;
this.backup_path = backup_path;
this.stmt = "d: \ \ MYSQL \ \ bin \ \ mysqldump" + this.db_name + "-h" + this.host_ip + "-u" + this.user_name + "-p" + this.user_psw
+ This.user_charset + "- result-file =" + this.backup_path;
}
public boolean backup_run () {
boolean run_result = false;
try {
Runtime.getRuntime (). Exec (this.stmt);
run_result = true;
} Catch (Exception e) {
e.printStackTrace ();
} Finally {
return run_result;
}
}
public static void main (String [] args) {
Backup backup = new Backup ("root", "000000", "jakie", null, "utf8", "e: \ \ test.sql");
boolean result = backup.backup_run ();
if (result)
System.out.println ("Backup successful");
}
}
Related Posts of How to use java MySql database backup and recovery procedures?
-
log4j easy application in java
JAVA development, frequently used the log output, in a so-called most of the software company will have its own set of configuration style, re-read the configuration file to initialize property of the log, it will be good, but sometimes may not need to fu
-
JDBC example of a long time do not have JDBC forgot
A back-up here to stay. The first: The second:
-
In the servlet use Bean
According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...
-
hibernate generic generic DAO
package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *
-
Servlet brief introduction
Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...
-
First Hibernate Example
Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co
-
Struts2 + hibernate + spring problem user log in
dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q












