第一步,准备好MySQL数据库,eclipse工具,把下面的代码复制一个java项目的普通java文件里就可以测试了。
public static void main(String[] args) {
// 数据库导出
String user = "root"; // 数据库帐号
String password = "root"; // 数据库密码
String database = "condiment"; // 需要备份的数据库名
String filepath = "D:\\mysql.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();
}
}