Reputation: 1422
This is the code I created for exporting the database. The problem is, the file is not exported, and the code shows no error message.
public boolean exportDatabase(String fromServer,
String FileName,
String FilePath,
int ExportOpions) {
try {
String dbName ="NMSAzzist";
String dbUser = "root";
String dbPass ="root";
String dumbPath = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\";
String executeCmd = dumbPath+"mysqldump -u "+dbUser+ "-p"+dbPass+" "+dbName+ "-r "+FilePath+ "";
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 1) { // if values equal 1 process failed
JOptionPane.showMessageDialog(null, "Backup Failed");//display message
} else if (processComplete == 0) {
JOptionPane.showMessageDialog(null, "\n Backup created Successfully..");
// display message
}
return true;
} catch (final Exception ex) {
NmsLogger.writeErrorLog("Database Connection Failed ", ex.toString());
NmsLogger.writeDebugLog(ex);
return false;
}
How can I export the database to a path specified in the variable FilePath
in the name FileName
? How can I solve the issue?
And BTW, can i use the following to import the same??
String[] executeCmd = new String[]{"mysql", databaseName, "-u" + userName, "-p" + password, "-e" + FileName };
Upvotes: 0
Views: 310
Reputation: 5946
You first try your executeCmd can run successfullly in db. In your executeCmd is syntax error. Your code left file name in execution command in
String executeCmd = "/" + dumbPath + "mysqldump -u " + dbUser
+ " -p" + dbPass + " " + dbName + " -r " + FilePath + "\\"
+ FileName;
This works for me.
public class exportDataBase {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
exportDatabase("", "Sma_test.sql", "C:", 0);
}
public static boolean exportDatabase(String fromServer, String FileName,
String FilePath, int ExportOpions) {
try {
String dbName = "dmsdev";
String dbUser = "root";
String dbPass = "root";
String dumbPath = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\";
String executeCmd = "/" + dumbPath + "mysqldump -u " + dbUser
+ " -p" + dbPass + " " + dbName + " -r " + FilePath + "\\"
+ FileName;
System.out.println(executeCmd);
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
System.out.println("processComplete: " + processComplete);
if (processComplete == 1) {// if values equal 1 process failed
System.out.println("Backup failed");
}
else if (processComplete == 0) {
System.out.println("Backup Success");
}
return true;
} catch (final Exception ex) {
System.out.println("Connection failed");
return false;
}
}
}
Upvotes: 1
Reputation: 16158
Make it as :
// you did not give file name.
String executeCmd = "cmd " + dumbPath+"mysqldump -u "+dbUser+ "-p"+dbPass+" "+dbName+ "-r "+FilePath+ "\\" + filename;
// I tried running I am getting error code 13.
I think you should make :
if(processComplete != 0) {
//error with error code
} else {
//success
}
instead of
if (processComplete == 1) {// if values equal 1 process failed
System.out.println("Backup failed");
}
else if (processComplete == 0) {
System.out.println("Backup Success");
}
because error-code returned may be other than 0 and 1.
Suggestion : Use Apache Commons exec API, this is more sophisticated than Runtime.exec
.
Upvotes: 1
Reputation: 990
Didn't you forget the Filename?
String executeCmd =dumbPath+"mysqldump -u "+dbUser+ "-p"+dbPass+" "+dbName+ "-r "+FilePath+"\\"+Filename"";
Upvotes: 1