當前位置:首頁 » 編程語言 » mysqljava備份

mysqljava備份

發布時間: 2022-05-22 20:35:35

A. 如何用java備份、還原mysql資料庫(求詳細代碼)

首先。Java備份還原是不現實的,因為不允許創建和恢復資料庫級別。
在安裝目錄的data下,就是所有的mysql資料庫文件。例如:
D:\Program Files\MySQL\MySQL Server 5.1\data
冷備:關閉mysql資料庫,拷貝走整個目錄即可,需要的時候還原回來覆蓋就行了。
熱備:\bin\mysqlmp.exe 可以執行熱備。

B. java用MySql 單擊備份按鈕實現備份該怎麼寫啊!

首先設置mysql環境變數

package com.vote.utils;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BackupMysql {

public static void main(String[] args) {
//new Backup().start();
//new Recovery().start();
}

static class Backup extends Thread {
public void run() {
String user = "root"; // Database Account
String password = "root"; // Login Password
String database = "資料庫"; // Need to back up the Database name
String filepath = "d:\\資料庫bak\\" + getFileName(database,".bak"); // Address of the backup path
File savefile = new File("d:\\資料庫bak\\", "aa.txt");
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
String command = "mysqlmp " + database + " -h localhost " + " -u " + user + " -p" + password + " --default-character-set=utf8 --result-file=" + filepath;
try {
Runtime.getRuntime().exec(command);
System.out.println("Successful data backup of " + database);
}catch(IOException e) {
e.printStackTrace();
}
}
private String getFileName(String soure,String suffix) {
String fileName = null;
DateFormat df = new SimpleDateFormat(".yyyy.MM.dd.hh.mm.ss.");
String dfs = df.format(new Date());
String rans = ((int)(Math.random() * 100) + 1) + "";
int rlen = rans.length();
if(rlen == 1){
rans = "0" + rans;
}
fileName = soure + dfs + Integer.toHexString(Integer.parseInt(rans)) + suffix;
return fileName;
}
}

static class Recovery extends Thread {
public void run() {
String user = "root"; // Database Account
String password = "root"; // Login Password
String database = "資料庫"; // Need to back up the Database name
String filepath = "d:\\資料庫bak\\資料庫.2011.10.27.12.56.04.50.bak"; // Address of the recovery path
String command = " -h localhost " + " -u " + user + " -p" + password;
String cmd[] = {"cmd","/c","mysql " + command + " " + database + " < " + filepath};
try {
Runtime.getRuntime().exec("mysqladmin " + command + " create " + database);
Runtime.getRuntime().exec(cmd);
}catch(IOException e) {
e.printStackTrace();
}
}
}

}

C. 如何使用java程序備份和恢復MySql資料庫

將MySql中的資料庫導出到文件中 備份 import java.io.*; import java.lang.*; public class BeiFen { public static void main(String[] args) { // 資料庫導出 String user = "root"; // 資料庫帳號 String password = "root"; // 登陸密碼 String database = "test"; // 需要備份的資料庫名 String filepath = "e:\\test.sql"; // 備份的路徑地址 String stmt1 = "mysqlmp " + database + " -u " + user + " -p" + password + " --result-file=" + filepath;/** String mysql="mysqlmp test -u root -proot * --result-file=d:\\test.sql";*/try {Runtime.getRuntime().exec(stmt1); System.out.println("數據已導出到文件" + filepath + "中");}catch (IOException e) { e.printStackTrace();}}} 將數據從磁碟上的文本文件還原到MySql中的資料庫 importjava.io.*; importjava.lang.*;/**還原MySql資料庫**/publicclassRecover{ publicstaticvoidmain(String[]args){ Stringfilepath="d:\\test.sql";//備份的路徑地址 //新建資料庫test

D. 如何使用java程序備份和恢復MySql資料庫

java用開源的ssh jar包連接到b伺服器執行備份/恢復命令,同樣通過命令也可以獲取到備份的文件信息,恢復資料庫也是一樣的,通過命令把文件傳輸到b伺服器,通過命令進行還原

E. 如何用Java實現MySQL資料庫的備份和恢復

註:要將mysql的bin目錄加入到環境變數Path中
將MySql中的資料庫導出到文件中 備份
import java.io.*;
import java.lang.*;
public class BeiFen {
public static void main(String[] args) {
// 資料庫導出
String user = "root"; // 資料庫帳號
String password = "root"; // 登陸密碼
String database = "test"; // 需要備份的資料庫名
String filepath = "e:\\test.sql"; // 備份的路徑地址
String stmt1 = "mysqlmp " + database + " -u " + user + " -p"
+ password + " --result-file=" + filepath;
/*
* String mysql="mysqlmp test -u root -proot
* --result-file=d:\\test.sql";
*/
try {
Runtime.getRuntime().exec(stmt1);
System.out.println("數據已導出到文件" + filepath + "中");
}
catch (IOException e) {
e.printStackTrace();
}
}
}

將數據從磁碟上的文本文件還原到MySql中的資料庫
import java.io.*;
import java.lang.*;

/*
* 還原MySql資料庫
* */
public class Recover {

public static void main(String[] args) {

String filepath = "d:\\test.sql"; // 備份的路徑地址
//新建資料庫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("數據已從 " + filepath + " 導入到資料庫中");
} catch (IOException e) {
e.printStackTrace();
}
}
}

F. java 備份mysql資料庫的部分數據

假設;你要備份的數據條件是時間,只備份當天的數據。備份區:資料庫。

  1. 匹配當天的數據讀出來。

  2. 在將數據添加到備份區的資料庫。

備份區表的創建問題:1.你可以事先手動建好。

2.也可以通過程序自動建表有兩步;

a).判斷當前備份數據,在備份區是否有對應的表。(有直接添加)

b).如果沒有,拷貝當前備份數據的表結構,在備份區生成。

G. java備份mysql資料庫,求大神解答。

1 public static String comman="C:/Program Files/MySQL/MySQL Server 5.5/bin/mysql.exe -uroot -proot test";
2 public static void back(String mySqlBackupName,String mysqlBackupPath, String command){
3
4 String fPath=mysqlBackupPath+"/"+new Date().getTime()+".sql";
5
6 Runtime rt = Runtime.getRuntime();
7 try {
8 Process child = rt.exec(command);
9 InputStream in = child.getInputStream();

H. 怎麼用java備份mysql資料庫

首先,設置mysql的環境變數(在path中添加%MYSQL_HOME%\bin),重啟電腦。
完整代碼:
備份:

public static void main(String[] args) {

backup();
load();
}

public static void backup() {
try {
Runtime rt = Runtime.getRuntime();

// 調用 mysql 的 cmd:
Process child = rt
.exec("mysqlmp -u root --set-charset=utf8 bjse act_obj");// 設置導出編碼為utf8。這里必須是utf8

// 把進程執行中的控制台輸出信息寫入.sql文件,即生成了備份文件。註:如果不對控制台信息進行讀出,則會導致進程堵塞無法運行
InputStream in = child.getInputStream();// 控制台的輸出信息作為輸入流

InputStreamReader xx = new InputStreamReader(in, "utf8");// 設置輸出流編碼為utf8。這里必須是utf8,否則從流中讀入的是亂碼

String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
// 組合控制台輸出信息字元串
BufferedReader br = new BufferedReader(xx);
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();

// 要用來做導入用的sql目標文件:
FileOutputStream fout = new FileOutputStream(
"e:/mysql-5.0.27-win32/bin/bjse22.sql");
OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");
writer.write(outStr);
// 註:這里如果用緩沖方式寫入文件的話,會導致中文亂碼,用flush()方法則可以避免
writer.flush();

// 別忘記關閉輸入輸出流
in.close();
xx.close();
br.close();
writer.close();
fout.close();

System.out.println("");

} catch (Exception e) {
e.printStackTrace();
}

}

public static void load() {
try {
String fPath = "e:/mysql-5.0.27-win32/bin/bjse22.sql";
Runtime rt = Runtime.getRuntime();

// 調用 mysql 的 cmd:
Process child = rt.exec("mysql -u root bjse ");
OutputStream out = child.getOutputStream();//控制台的輸入信息作為輸出流
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(fPath), "utf8"));
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();

OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
writer.write(outStr);
// 註:這里如果用緩沖方式寫入文件的話,會導致中文亂碼,用flush()方法則可以避免
writer.flush();
// 別忘記關閉輸入輸出流
out.close();
br.close();
writer.close();

System.out.println("");

} catch (Exception e) {
e.printStackTrace();
}

}

備份語句:
mysql> SELECT * INTO OUTFILE "D:\\data\\db_testtemp.txt" fields terminated by ',
' from db_testtemp where std_state='1';
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * INTO OUTFILE "D:\\data\\db_testtemp.txt" fields terminated by ',
' from db_testtemp ;
Query OK, 2 rows affected (0.00 sec)

只生成一個只有數據的.txt:SELECT * INTO OUTFILE "D:\\data\\db_testtemp.txt" fields terminated by ',' lines terminated by '\r\n' from db_testtemp ;

只生成一個只有數據的.txt:mysqlmp -uroot -pncae2010 -w "std_state='1'" -T D:\data --no-create-info --fields-terminated-by=, exam db_testtemp

生成一個創建資料庫語句的.sql,一個只有數據的.txt:mysqlmp -uroot -pncae2010 -w "std_state='1'" -T D:\data --fields-terminated-by=, exam db_testtemp

只生成insert語句:mysqlmp -uroot -pncae2010 -w "std_state='1'" -t exam db_testtemp > D:\data\a.sql

I. java mysql備份資料庫數據問題

想得到所有記錄及表的sql語句?

將框裡面那句刪除了試試

插不上圖

System.out.println(result+"--");

這句刪除掉試試

J. java 備份mysql資料庫

備份MySQL資料庫的方法:
import java.io.File;
import java.io.IOException;

/**
* MySQL資料庫備份
*
* @author GaoHuanjie
*/
public class MySQLDatabaseBackup {

/**
* Java代碼實現MySQL資料庫導出
*
* @author GaoHuanjie
* @param hostIP MySQL資料庫所在伺服器地址IP
* @param userName 進入資料庫所需要的用戶名
* @param password 進入資料庫所需要的密碼
* @param savePath 資料庫導出文件保存路徑
* @param fileName 資料庫導出文件文件名
* @param databaseName 要導出的資料庫名
* @return 返回true表示導出成功,否則返回false。
*/
public static boolean exportDatabaseTool(String hostIP, String userName, String password, String savePath, String fileName, String databaseName) {
File saveFile = new File(savePath);
if (!saveFile.exists()) {// 如果目錄不存在
saveFile.mkdirs();// 創建文件夾
}
if (!savePath.endsWith(File.separator)) {
savePath = savePath + File.separator;
}

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("mysqlmp").append(" --opt").append(" -h").append(hostIP);
stringBuilder.append(" --user=").append(userName) .append(" --password=").append(password).append(" --lock-all-tables=true");
stringBuilder.append(" --result-file=").append(savePath + fileName).append(" --default-character-set=utf8 ").append(databaseName);
try {
Process process = Runtime.getRuntime().exec(stringBuilder.toString());
if (process.waitFor() == 0) {// 0 表示線程正常終止。
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}

public static void main(String[] args) throws InterruptedException {
if (exportDatabaseTool("172.16.0.127", "root", "123456", "D:/backupDatabase", "2014-10-14.sql", "test")) {
System.out.println("資料庫備份成功!!!");
} else {
System.out.println("資料庫備份失敗!!!");
}
}
}

熱點內容
蘋果怎麼對備忘錄加密碼 發布:2025-02-13 18:44:19 瀏覽:72
php房產網 發布:2025-02-13 18:18:06 瀏覽:86
源碼資源吧 發布:2025-02-13 18:14:39 瀏覽:80
java培訓價錢 發布:2025-02-13 17:59:33 瀏覽:975
c語言中變數類型 發布:2025-02-13 17:52:20 瀏覽:259
ftp導出報錯 發布:2025-02-13 17:41:20 瀏覽:998
腳本下載教程 發布:2025-02-13 17:39:06 瀏覽:236
解壓密碼re 發布:2025-02-13 17:39:02 瀏覽:559
linuxdump內存 發布:2025-02-13 17:37:30 瀏覽:58
游戲客戶端源碼 發布:2025-02-13 17:37:19 瀏覽:595