java 备份mysql_Java实现备份MySql数据库

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("127.0.0.1", "root", "123456", "c:/", "blood_bank.sql", "blood_bank")) {

System.out.println("数据库成功备份!!!");

} else {

System.out.println("数据库备份失败!!!");

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下步骤来实现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用户名、密码、主机和数据库名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值