----------------------------------------------------->>>>>>>>
原理:MySQL 數據庫備份原理: Navicat等數據庫界面軟件通用的數據庫備份原理就是直
接調用MYSQL本身的系統命令。
MySQL本身的系統命令:
--opt –h localhost --user=root --password=admin --lock-all-tables=true --result-file=E://oes//2221.sql --default-character-set=utf8 oes
解析:主機–h,用戶名--user,密碼—password,鎖定所有表--lock-all-tables=true,
目標文件--result-file,編碼--default-character-set=utf8,數據源oes
Java中執行系統命令方法:
Runtime cmd = Runtime.getRuntime();
Process p = cmd.exec(“”); //執行CMD指令(String)
由于無法確定主機是否配置了MySQL環境變量,所以需要最保險的確定MySQL中mysqldump的位置,它存在于MySQL安裝文件夾得Bin目錄下,問題就是如何獲取MySQL的安裝目錄?
----------------------------------------------------->>>>>>>>
針對于獲取MySQL的安裝目錄,我用的是比較笨的方法:解析注冊表。
找到注冊表中MySQL的軟件信息,里面包含有軟件的安裝地址,卸載地址,版本號等等基本信息,直接取用它的安裝信息就行。
軟件信息在注冊表中的位置:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
軟件關聯注冊表中軟件信息:

----------------------------------------------------->>>>>>>>
類CheckSoftware,解析MySQL軟件安裝地址
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CheckSoftware {
/*
* 遍歷注冊表,查詢MySQL的注冊表關聯
*/
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.exec("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;
}
/*
* 查詢出需要的MySQL服務的安裝路徑
*/
private static String queryValue(String string, String method)
throws IOException {
String pathString = "";
Runtime runtime = Runtime.getRuntime();
Process process = null;
BufferedReader br = null;
process = runtime.exec("cmd /c reg query " + string + " /v " + method);
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
br.readLine();
br.readLine();// 去掉前兩行無用信息
if ((pathString = br.readLine()) != null) {
pathString = pathString.replaceAll(method + " REG_SZ ", ""); // 去掉無用信息
return pathString;
}
return pathString;
}
}
----------------------------------------------------->>>>>>>>
類JavaMysql備份還原數據庫
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class JavaMysql {
/*
* 備份數據庫 1、讀取配置文件 2、啟動智能查詢Mysql安裝目錄 3、備份數據庫為sql文件
*/
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服務安裝地址 :"+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指令 :"+sb.toString());
Runtime cmd = Runtime.getRuntime();
try {
Process p = cmd.exec(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
/*
* 讀取屬性文件
*/
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;
}
/*
* 根據備份文件恢復數據庫
*/
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; // 備份的路徑地址
String stmt1 = mysqlpaths + "mysqladmin -u " + root + " -p" + pass
+ " create finacing"; // -p后面加的是你的密碼
String stmt2 = mysqlpaths + "mysql -u " + root + " -p" + pass
+ " finacing < " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };
try {
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(cmd);
System.out.println("數據已從 " + filepath + " 導入到數據庫中");
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Test測試
*/
public static void main(String[] args) throws IOException {
backup("2221.sql");
}
}
----------------------------------------------------->>>>>>>>
屬性文件: prop.properties,動態配置用戶名及密碼等基本屬性
username = root
password = admin
sql = E://oes//
address=localhost
databaseName=oes
http://cobaya.cn/