我使用以下代码从运行良好的 sql 备份我的数据库,并在 backup.sql 中备份。
import java.io.*;
import java.lang.*;
import java.util.*;
class Backup
{
public static void main(String[] args)
{
String command = "D:\\MySQL\\bin\\mysqldump.exe -u root -proot yatin -r D:/backup.sql";
System.out.println(command);
Process runtimeProcess;
try
{
runtimeProcess = Runtime.getRuntime().exec(command);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0)
{
System.out.println("Backup created successfully");
//return true;
}
else
{
System.out.println("Could not create the backup");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
但是当我执行下面的代码时,它让我处于等待状态。以前我的数据库名称是 yatin,现在我想将它恢复到 mybackup 数据库,所以我创建了名为 mybackup 的数据库。这里出了什么问题
import java.io.*;
import java.lang.*;
import java.util.*;
class Restore
{
public static void main(String[] args)
{
String command = "D:\\MySQL\\bin\\mysql.exe -u root -proot mybackup< D:/backup.sql";
//String [] command = new String[]{"mysql",mybackup,"D:\\MySQL\\bin\\mysql.exe -u root -proot < D:/backup.sql";
//String[] command = new String[]{"mysql", [database], "--user=" + [username], "--password=" + [password], "-e", " source " + [absolute path to the sql file]};
//String[] command = new String[]{"mysql",mybackup, "-u=" +root, "-p="+root, "-e", " source " +D:/backup.sql};
System.out.println(command);
Process runtimeProcess;
try
{
runtimeProcess = Runtime.getRuntime().exec(command);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0)
{
System.out.println("Restore created successfully");
//return true;
}
else
{
System.out.println("Could not restore");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
更新 1
mysql> mysql -u root -proot mybackup < D:/backup.sql;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
-u root -proot mybackup < D:/backup.sql' at line 1