Hi!
This article is about creating a java program to backup/restore a MySQL database. The article is in two basic parts, namely Backup & Restore.
1. Backup - Basically, the MYSQL command to backup database looks like follows.
mysqldump -u [username] -p[password] -B [dbname] -r [MYSQL file Path]
The following Code is for the java program.
public class Main{ public void restoreDB(String path){ String executeCmd = "C:/xampp/mysql/bin/mysqldump -u xyz -pxxyyzz -B myDB -r " + path; System.out.println(executeCmd); Process runtimeProcess; try { runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd }); int processComplete = runtimeProcess.waitFor(); System.out.println(processComplete); if(processComplete == 0) { System.out.println("Backup Created Successfully !"); } else { System.out.println("Couldn't Create the backup !"); } } catch(Exception ex) { ex.printStackTrace(); } } public static void main(String[]args){ new Main().restoreDB("C:/File.sql"); } }
2. Restore - Basically, the MYSQL command to restore database looks like follows.
mysql -u [username] -p[password] [databasename]<[MYSQL file Path]
In the query,
-u [username] - is where you should type your username to access databases.
-p[password] - is where you should type your password to the databases.
Then, you should type the name of the database which you need to backup tables to.
mysql -u xyz -pxxyyzz myDatabase<C:/file.sql
Then,Here's a java code to test run this MYSQL command.
public class Main{ public void restoreDB(String path){ String executeCmd = "C:/xampp/mysql/bin/mysql -u xyz -pxxyyzz myDB <" + path; System.out.println(executeCmd); Process runtimeProcess; try { runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd }); int processComplete = runtimeProcess.waitFor(); System.out.println(processComplete); if(processComplete == 0) { System.out.println("Restored the Backup !"); } else { System.out.println("Couldn't Restore the backup !"); } } catch(Exception ex) { ex.printStackTrace(); } } public static void main(String[]args){ new Main().restoreDB("C:/File.sql"); } }Ah, one more thing, You can set path to MYSQL in windows & adjust line 6 as follows.
String executeCmd = "mysql -u xyz -pxxyyzz myDB <" + path;
And, You need to have Process Class invoked to run cmd.exe & execute MYSQL command in line with Command Prompt.
19 comments:
thank you very much. it's really working :D
4 oomsortu
thank you very muuch. it's really work :D
thank you very much. it's work :D
thank you very much. it's work :DFund
A complete example on how to use these commands from JSP code can be found here
http://www.jvmhost.com/articles/mysql-postgresql-dump-restore-java-jsp-code
Thanks Nithin for the info.
thank you so muchh Nithin.. it works
Don't work.
Hey, its not working every time i get Couldn't Create the backup !
Please help
Couldn't Create the backup ! ,I am getting this message every time ,please help
I'm sorry People I was not available for several months. You can simply ask me any problem if you have ??
Thanks Harsha
Backup is working
while restoring the backup it is printing else part i.e. "Couldn't Restore the Backup!"
Thanks Harsha,
The backup is working well.
While restoring the backup it is displaying the else part "Couldn't Restore the Backup!"
Please help
Than
Thanks Harsha
The Backup code is working
while restoring the database the program is printing the else part i.e. "Couldn't Restore the backup !"
this exactly what i looking for, searching ton of tutorials for restore the database, finally find this easy understandable simple code, you're the best sir...thank you very much!
eureka!!! this what i looking for, searching ton of tutorials for restore the database, finally find this easy understandable simple code, you're the best sir! thank you very much!
it gives me this error.
java.io.IOException: Cannot run program "mysqldump": CreateProcess error=2, The system cannot find the file specified
so what to do now please help
Post a Comment