How to use java MySql database backup and recovery procedures?

How to use java MySql database backup and recovery procedures?
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");

}
}

分类:Database 时间:2011-07-29 人气:206
分享到:
blog comments powered by Disqus

相关文章

  • public static void main (String args []) parameters 2010-05-15

    String args [] is the main function of main parameters, namely array of strings. Using eclipse in the editing, right-click to run the Java files in the Run As option, select Run Configurations. Main class in the Project and complete the project, resp

  • Why public static void main (String args []){} 2010-11-06

    main () is a Java program entry, program execution is the beginning of this entry. Class is a class member, main () method must be public members. So that it can be called in the execution environment. main () method does not produce the object can b

  • How the eclipse of the main the String [] args parameter passed, and then export it 2010-12-26

    How the eclipse of the main the String [] args parameter passed, and then export it? In the Run menu to find Open Run dialog opens Select Java Application in the program need to run Select the Arguments Tab Enter the parameters in the Programe Argume

  • 浅析C#中的Main(String[] args)参数输入问题 2014-07-20

    本篇文章主要是对C#中的Main(String[] args)参数输入问题进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 指定相关的测试代码 首先,写一个用于测试的关于Main(String[] args)参数输入有关的代码类,如下: using System; public class Hello { public static void Main(String[] args) { switch (args[0]) { case "老板": Console.Write

  • String [] args = new String [] {sql.toString ()} 2011-06-29

    In this problem StringBuffer sql = new StringBuffer (); Which sql.toString results: 'Test User 1', 'male' String [] args = new String [] {sql.toString ()} args.length 1 results Obviously not what we need. Note: new String []{"","",&quo

  • java access to the five main information page: Test (to) 2010-12-04

    1. Required Documents param.txt: storing the path need to extract information page TestPage: need to extract the information stored pages Out.txt: page content output 2. Test program package test; import java.io.*; import Source.*; // Extract the mai

  • java to get the five main information page: Test (rpm) 2010-12-04

    1. Required Documents param.txt: storing the path need to extract information page TestPage: need to extract the information stored pages Out.txt: page content output 2. Test program package test; import java.io.*; import Source.*; // Extract the mai

  • mysql set user password modification and distribution of competences 2010-03-10

    My mysql installed in c: \ mysql First, change your password the first approach: 1, change the password before the root is not the case c: \ mysql \ bin> mysqladmin-u root password "your password" 2, change the root password before the case,

  • How to lift the Oracle account locked (the account is locked) 2010-04-03

    If in plsql developer in order to scott / tiger login prompt ora-28000 the account is locked. Solution: New Bahrain Oracle10g, the use scott / tiger test the following error message: oracle10g the account is locked oracle10g the password has expired

  • Linux to quickly reset the MySQL user (root) password method 2011-05-24

    Method One: The easiest way is installing mysql, the system prompts the method. Use mysqladmin to complete. shell> mysqladmin-u root password "newpwd" shell> mysqladmin-u root-h host_name password "newpwd" password behind the dou

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 1.506 (s). 12 q(s)