加入收藏 | 设为首页 | 会员中心 | 我要投稿 航空爱好网 (https://www.52kongjun.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

Java代码实现MySQL数据库备份导出及恢复导入

发布时间:2022-12-12 13:33:02 所属栏目:MsSql教程 来源:网络
导读: 优质广告供应商
广告是为了更好地支持作者创作
Java代码实现MySQL数据库备份导出及恢复导入
/**
* Java代码实现MySQL数据库导出
*
* @param hostIP MySQL数据库所在服务器地址IP
* @par

优质广告供应商

广告是为了更好地支持作者创作

Java代码实现MySQL数据库备份导出及恢复导入

/**

* Java代码实现MySQL数据库导出

*

* @param hostIP MySQL数据库所在服务器地址IP

* @param userName 进入数据库所需要的用户名

* @param password 进入数据库所需要的密码

* @param savePath 数据库导出文件保存路径

* @param fileName 数据库导出文件文件名

* @param databaseName 要导出的数据库名

* @return 返回true表示导出成功,否则返回false。

* @author GaoHuanjie

*/

public static boolean exportDatabaseTool(String hostIP, String hostPort, String userName, String password, String savePath, String fileName, String databaseName)throws InterruptedException {

File saveFile =new File(savePath);

if (!saveFile.exists()) {// 如果目录不存在

saveFile.mkdirs();// 创建文件夹

}

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

savePath = savePath + File.separator;

}

PrintWriter printWriter =null;

BufferedReader bufferedReader =null;

try {

Runtime runtime = Runtime.getRuntime();

String path = ResourceUtils.getURL("classpath:static").getPath().replace('/','\\');

path = path.substring(1);

log.info(path);

mssql数据库备份_钛备份备份应用数据_钛备份怎么备份多个数据

//String cmd = "mysqldump -h127.0.0.1 -uroot -P3308 -p123456 archives";

String cmd ="mysqldump -h" + hostIP +" -u" + userName +" -P" + hostPort +" -p" + password +" " + databaseName;

cmd = path +"\\" + cmd;

log.info(cmd);

Process process = runtime.exec(cmd);

InputStreamReader inputStreamReader =new InputStreamReader(process.getInputStream(),"utf8");

bufferedReader =new BufferedReader(inputStreamReader);

printWriter =new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName),"utf8"));

String line;

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

printWriter.println(line);

}

printWriter.flush();

if (process.waitFor() ==0) {//0 表示线程正常终止。

return true;

}

}catch (IOException e) {

e.printStackTrace();

}finally {

try {

if (bufferedReader !=null) {

bufferedReader.close();

}

if (printWriter !=null) {

printWriter.close();

}

}catch (IOException e) {

e.printStackTrace();

}

}

return false;

}

/**

* Java实现MySQL数据库导入

*

* @param hostIP MySQL数据库所在服务器地址IP

* @param userName 数据库用户名

* @param password 进入数据库所需要的密码

* @param importFilePath 数据库文件路径

* @param sqlFileName 数据库文件名

* @param databaseName 要导入的数据库名

* @return 返回true表示导入成功mssql数据库备份,否则返回false。

* @author GaoHuanjie

*/

public static boolean importDatabase(String hostIP, String hostPort, String userName, String password, String importFilePath, String sqlFileName, String databaseName) {

File saveFile =new File(importFilePath);

if (!saveFile.exists()) {// 如果目录不存在

saveFile.mkdirs();// 创建文件夹

}

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

importFilePath = importFilePath + File.separator;

}

StringBuilder stringBuilder =new StringBuilder();

stringBuilder.append("mysql").append(" -h").append(hostIP);

stringBuilder.append(" -u").append(userName).append(" -P").append(hostPort).append(" -p").append(password);

stringBuilder.append(" ").append(databaseName);

stringBuilder.append(" d:\\123456.sql");

}

优质广告供应商

广告是为了更好地支持作者创作

(编辑:航空爱好网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!