Java实现MySQL数据库备份(一)

        下班了,利用闲暇时间总结一下如何使用Java语言实现MySQL数据库备份:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
 * 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) 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 {
			printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8"));
			Process process = Runtime.getRuntime().exec(" mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " --set-charset=UTF8 " + databaseName);
			InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");
			bufferedReader = new BufferedReader(inputStreamReader);
            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;
	}
	
	public static void main(String[] args){
		try {
			if (exportDatabaseTool("172.16.0.127", "root", "123456", "D:/backupDatabase", "2014-10-14.sql", "test")) {
				System.out.println("数据库成功备份!!!");
			} else {
				System.out.println("数据库备份失败!!!");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

  • 5
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 21
    评论
可以使用以下步骤来实现Java语言备份MySQL数据库: 1. 导入MySQL JDBC驱动程序 Java程序需要使用MySQL JDBC驱动程序来连接和操作MySQL数据库。可以从MySQL官方网站下载并导入驱动程序。 2. 创建连接 使用JDBC驱动程序创建与MySQL数据库的连接。可以使用以下代码来创建连接: ``` String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "root"; String password = "mypassword"; Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, password); ``` 其中,`driver`是JDBC驱动程序的类名,`url`是连接字符串,`user`和`password`是数据库的用户名和密码。 3. 执行备份命令 使用Java程序执行备份命令。可以使用以下代码来执行备份命令: ``` String backupPath = "/path/to/backup.sql"; String command = "mysqldump --opt --user=" + user + " --password=" + password + " --host=localhost mydatabase > " + backupPath; Process process = Runtime.getRuntime().exec(command); int exitStatus = process.waitFor(); if(exitStatus == 0) { System.out.println("Backup successful"); } else { System.out.println("Backup failed"); } ``` 其中,`backupPath`是备份文件的路径,`command`是备份命令。使用`Runtime.getRuntime().exec()`方法执行命令,并使用`process.waitFor()`方法等待命令执行完成。 4. 关闭连接 备份完成后,使用Java程序关闭与MySQL数据库的连接。可以使用以下代码来关闭连接: ``` conn.close(); ``` 这些步骤可以实现Java语言备份MySQL数据库。需要注意的是,备份命令需要正确设置MySQL用户名、密码、主机和数据库名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿老高

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值