欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java備份恢復代碼 java 文件備份

如何用java代碼實現(xiàn)數(shù)據(jù)備份和恢復?

這個啊,說說大概思路 公司用過 但我沒注意看

創(chuàng)新互聯(lián)自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目網(wǎng)站制作、網(wǎng)站建設網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元鶴壁做網(wǎng)站,已為上家服務,為鶴壁各地企業(yè)和個人服務,聯(lián)系電話:18980820575

package org.lxh.runtimedemo;

public class RuntimeDemo02 {

public static void main(String[] args) throws Exception {

Runtime.getRuntime().exec("notepad.exe");

Runtime.getRuntime().exec("freecell.exe");

}

}

這個程序你 看得懂吧

數(shù)據(jù)備份 和 回復呢

具體備份、恢復 sql語句你上網(wǎng)查 然后寫在.bat 文件里

最后 調(diào)用 Runtime.getRuntime().exec("路徑/*.bat") 文件即可

如何用Java實現(xiàn)MySQL數(shù)據(jù)庫的備份和恢復

MySQL的一些前臺工具是有備份恢復功能的,可是如何在我們的應用程序中實現(xiàn)這一功能呢?本文提供了示例代碼來說明如何使用Java代碼實現(xiàn)MySQL數(shù)據(jù)庫的備份恢復。

本次實現(xiàn)是使用了MySQL數(shù)據(jù)庫本身提供的備份命令mysqldump和恢復命令mysql,在java代碼中通過從命令行調(diào)用這兩條命令來實現(xiàn)備份和恢復。備份和恢復所使用的文件都是sql文件。

本代碼是參照網(wǎng)上某網(wǎng)友提供的源碼完成的。

[java] view plaincopy

package xxx.utils;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.io.UnsupportedEncodingException;

/**

* MySQL數(shù)據(jù)庫的備份與恢復 缺陷:可能會被殺毒軟件攔截

*

* @author xxx

* @version xxx

*/

public class DatabaseBackup {

/** MySQL安裝目錄的Bin目錄的絕對路徑 */

private String mysqlBinPath;

/** 訪問MySQL數(shù)據(jù)庫的用戶名 */

private String username;

/** 訪問MySQL數(shù)據(jù)庫的密碼 */

private String password;

public String getMysqlBinPath() {

return mysqlBinPath;

}

public void setMysqlBinPath(String mysqlBinPath) {

this.mysqlBinPath = mysqlBinPath;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public DatabaseBackup(String mysqlBinPath, String username, String password) {

if (!mysqlBinPath.endsWith(File.separator)) {

mysqlBinPath = mysqlBinPath + File.separator;

}

this.mysqlBinPath = mysqlBinPath;

this.username = username;

this.password = password;

}

/**

* 備份數(shù)據(jù)庫

*

* @param output

* 輸出流

* @param dbname

* 要備份的數(shù)據(jù)庫名

*/

public void backup(OutputStream output, String dbname) {

String command = "cmd /c " + mysqlBinPath + "mysqldump -u" + username

+ " -p" + password + " --set-charset=utf8 " + dbname;

PrintWriter p = null;

BufferedReader reader = null;

try {

p = new PrintWriter(new OutputStreamWriter(output, "utf8"));

Process process = Runtime.getRuntime().exec(command);

InputStreamReader inputStreamReader = new InputStreamReader(process

.getInputStream(), "utf8");

reader = new BufferedReader(inputStreamReader);

String line = null;

while ((line = reader.readLine()) != null) {

p.println(line);

}

p.flush();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (reader != null) {

reader.close();

}

if (p != null) {

p.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 備份數(shù)據(jù)庫,如果指定路徑的文件不存在會自動生成

*

* @param dest

* 備份文件的路徑

* @param dbname

* 要備份的數(shù)據(jù)庫

*/

public void backup(String dest, String dbname) {

try {

OutputStream out = new FileOutputStream(dest);

backup(out, dbname);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

/**

* 恢復數(shù)據(jù)庫

*

* @param input

* 輸入流

* @param dbname

* 數(shù)據(jù)庫名

*/

public void restore(InputStream input, String dbname) {

String command = "cmd /c " + mysqlBinPath + "mysql -u" + username

+ " -p" + password + " " + dbname;

try {

Process process = Runtime.getRuntime().exec(command);

OutputStream out = process.getOutputStream();

String line = null;

String outStr = null;

StringBuffer sb = new StringBuffer("");

BufferedReader br = new BufferedReader(new InputStreamReader(input,

"utf8"));

while ((line = br.readLine()) != null) {

sb.append(line + "/r/n");

}

outStr = sb.toString();

OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");

writer.write(outStr);

writer.flush();

out.close();

br.close();

writer.close();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 恢復數(shù)據(jù)庫

*

* @param dest

* 備份文件的路徑

* @param dbname

* 數(shù)據(jù)庫名

*/

public void restore(String dest, String dbname) {

try {

InputStream input = new FileInputStream(dest);

restore(input, dbname);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

Configuration config = HibernateSessionFactory.getConfiguration();

String binPath = config.getProperty("mysql.binpath");

String userName = config.getProperty("connection.username");

String pwd = config.getProperty("connection.password");

DatabaseBackup bak = new DatabaseBackup(binPath, userName, pwd);

bak.backup("c:/ttt.sql", "ttt");

bak.restore("c:/ttt.sql", "ttt");

}

}

最后的main方法只是一個簡單的使用方法的示例代碼。

本人所做的項目是使用了hibernate的,而這里需要提供MySQL的bin路徑和用戶名、密碼,而hibernate.cfg.xml中本身就是需要配置數(shù)據(jù)庫的用戶名和密碼,所以我把MySQL的bin路徑也直接配置到了這個文件里面,也不需要創(chuàng)建專門的配置文件,不需要寫讀取配置文件的接口了。

如果不明白,可以去看hibernate.cfg.xml的說明,里面是可以配置其他的property的

用java代碼實現(xiàn)Oracle數(shù)據(jù)庫的備份與恢復(保存為.dmp或.sql)

備份:發(fā)送sql給mssqlserver:

backup database your database name to disk='備份文件名' with init

注意: 1.備份文件名必須為絕對路徑,

2.備份文件只能是mssqlserver所在的機器上的路徑, mssql支持備份到網(wǎng)絡位置。

恢復:

restore database your database name from disk='備份文件名' with replace

要注意的是執(zhí)行restore database時,要恢復的數(shù)據(jù)庫必須沒有任何客戶端連接,包括自身(發(fā)起restore database命令的連接)。發(fā)使用restore,可以連接到master庫,然后再發(fā)送restore命令。

否則,一定失敗。

在jsp中如何用呢

-------------------------------------------------------------------------------------- 你用這個了!

%

先要連接上Connection對象!

就是要先和數(shù)據(jù)庫建立起連接

然后在jsp頁面中直接用我這樣的語句就可以了

try{

String sql="backup database xncsims to disk='d:\\xncback.dat'";

st=con.createStatement();

rs=st.executeQuery(sql);

}

catch(SQLException e){ System.out.println(e.toString());}

catch(Exception e){ System.out.println(e.toString());}

%

rs=st.executeQuery(sql);

這里就是把你的SQL語句發(fā)到數(shù)據(jù)庫執(zhí)行

另有一篇論文供參考

sqlite用JAVA 備份還原

stms.executeUpdate("backup to '" + db_path + "'");// 備份

stms.executeUpdate("restore from '" + db_path + "'");//恢復

分享題目:java備份恢復代碼 java 文件備份
當前網(wǎng)址:http://chinadenli.net/article44/dodidhe.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機、微信公眾號電子商務、微信小程序App設計、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司