永发信息网

如何用Java实现MySQL数据库的备份和恢复

答案:2  悬赏:10  手机版
解决时间 2021-01-29 19:56
  • 提问者网友:流星是天使的眼泪
  • 2021-01-29 01:37
如何用Java实现MySQL数据库的备份和恢复
最佳答案
  • 五星知识达人网友:拾荒鲤
  • 2021-01-29 03:04
注:要将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 = "mysqldump " + database + " -u " + user + " -p"
+ password + " --result-file=" + filepath;

try {
Runtime.getRuntime().exec(stmt1);
System.out.println("数据已导出到文件" + filepath + "中");
}
catch (IOException e) {
e.printStackTrace();
}
}
}

将数据从磁盘上的文本文件还原到MySql中的数据库
import java.io.*;
import java.lang.*;


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();
}
}
}
全部回答
  • 1楼网友:七十二街
  • 2021-01-29 04:32
MySQL的一些前台工具是有备份恢复功能的,可是如何在我们的应用程序中实现这一功能呢?本文提供了示例代码来说明如何使用Java代码实现MySQL数据库的备份恢复。

本次实现是使用了MySQL数据库本身提供的备份命令mysqldump和恢复命令mysql,在java代码中通过从命令行调用这两条命令来实现备份和恢复。备份和恢复所使用的文件都是sql文件。

本代码是参照网上某网友提供的源码完成的。
[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;

public class DatabaseBackup {

private String mysqlBinPath;

private String username;

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;
}

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();
}
}
}

public void backup(String dest, String dbname) {
try {
OutputStream out = new FileOutputStream(dest);
backup(out, dbname);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

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();
}
}

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中本身就是需要配置数据库的用户名和密码,所以我把MySQL的bin路径也直接配置到了这个文件里面,也不需要创建专门的配置文件,不需要写读取配置文件的接口了。
如果不明白,可以去看hibernate.cfg.xml的说明,里面是可以配置其他的property的
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯