使用java转储mysql数据库
我正在使用Ubuntu。我想使用java应用程序转储MySQL数据库使用java转储mysql数据库,java,mysqldump,Java,Mysqldump,我正在使用Ubuntu。我想使用java应用程序转储MySQL数据库以下是我的代码: String userName = "root"; String userPassword = "root"; String oldDatabaseName = "testing"; String executeCommand = ""; executeCommand = "mysqldump -u "+userName+" -p"+userPassword+" --no-data " +Old
以下是我的代码:
String userName = "root";
String userPassword = "root";
String oldDatabaseName = "testing";
String executeCommand = "";
executeCommand = "mysqldump -u "+userName+" -p"+userPassword+" --no-data "
+OldDatabaseName+" > testingbackup.sql";
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if(processComplete == 0){
System.out.println("Backup successful.");
}else{
System.out.println("Backup failed.");
}
但是,当我运行上述程序时,我总是收到“备份失败”消息。我写错代码了吗?我需要在java应用程序中包含哪些库/文件
请提供帮助。这在我的windows机器上运行良好
package org.some.test;
import java.io.File;
public class dateformat {
private static final String dbUserName = "root";
private static final String dbPassword = "manager";
private static final String dbName = "opencms";
public static void main(String[] args){
try {
File path =new File("D:\\dump1.sql");
String[] executeCmd = new String[]{"C:/Program Files/MySQL/MySQL Server 5.6/bin/mysql", "--user=" + dbUserName, "--password=" + dbPassword,""+ dbName,"-e", "source "+path};
Process p = Runtime.getRuntime().exec(executeCmd);
int processComplete = p.waitFor();
if (processComplete == 0)
{
System.out.println("Restore created successfully");
//return true;
}
else
{
System.out.println("Could not restore");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
在我看来,您省略了-p和引号之间的空格:
-p "+userPassword+
^-- insert space here
mysqldump进程的输出是什么?手动调用是否成功?您的用户ID是否可以在工作目录中创建一个文件,文件
testingbackup.sql
是否已创建,如果已创建,其中包含什么,则processComplete
的值是多少(打印)您运行的是什么操作系统?所有MySQL用户程序,包括mysqldump
都以通常的Unix/GNU方式使用带有参数的大多数标志,可以将--long wordy foo
或-x foo
作为两个令牌,也可以将--long wordy=foo
或-xfoo
作为一个令牌。但他们对密码特别重视;只有一个令牌表单有效。我错了。