• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get mysql backup using java swing

 
Shashika Thiran
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use mysqldump to get mysql database backup.it's work fine when i try it on dos prompt.
like this..
>mysqldump -u uname -ppassword databaseName>C:/backup.sql

this will create backup.sql file.
then i try this in java

try{
Runtime rt=Runtime.getRuntime();
rt.exec("C:/Programme Files/MySQL/MySQL Server 5.0/bin/mysqldump -u uname -ppassword databaseName>C:/backup.sql");
catch(IoException e){}

It's runs without errors.But nothing happen.No file create.
Can anyone tell me why this is not work...
can anyone help me..
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wouldn't know even if there were any errors. Your catch block is empty! Try putting a print stack trace there and see how it goes.
 
Dmitry Mamonov
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, empty catch block is the bad point to find error.
Also, you are running "C:/Programme.exe"
with arguments
1. Files/MySQL/MySQL
2. Server
3. 5.0/bin/mysqldump
etc.
have you any executables with such name?
 
Shashika Thiran
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have printStacktrace() in catch block and file path is fine...it's soething else i think
 
Dmitry Mamonov
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sasika, would you like to trick us posting code which has no relation to you real problem?

with you posted code we have next:
rt.exec("C:/Programme Files/MySQL/MySQL Server 5.0/bin/mysqldump -u uname -ppassword databaseName>C:/backup.sql");

1. http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String) refers to http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String,%20java.lang.String[],%20java.io.File)
2. and consequentally to http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[],%20java.io.File)
3. the command line you have passed as factual argument of exec() method is stripped into String[] with
http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html

here is example from javadoc

The following is one example of the use of the tokenizer. The code:


prints the following output:


So, as I described above, you are executing "C:/Programme" which will be turned by OS "C:/programme.exe" (or .com or .bat).

To evaluate executable "\"C:/Programme Files/MySQL/MySQL Server 5.0/bin/mysqldump\"" you should enclose it's path with quotes, it may help, but I'm not sure.
Or, better, use method: http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[],%20java.io.File)
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Runtime.exec(String[]) is much better than Runtime.exec(String). It saves you from all the trouble about paths containing spaces and parameters needing to be quoted.

Also be sure to handle the input/error/output stream of the child process. This is described in detail in the article "When Runtime.exec() won't" by Daconta. Nobody should use Runtime.exec without reading that.
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sasika Thiranga wrote:C:/Programme Files/MySQL/MySQL Server 5.0/bin/mysqldump -u uname -ppassword databaseName>C:/backup.sql

1) shouldn't there be a space between -p and password?
2) file redirection like this (using > C:/backup.sql) is handled by the shell (cmd.exe), it is not part of the application. To do the same in Java you would need to get the process' output (through getInputStream()) and copy its contents to a FileOutputStream("C:/backup.sql") manually. This would also solve half of the problems that the article Ulf mentioned is about. (You'll still need to clear getErrorStream().)
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sasika Thiranga wrote:I have printStacktrace() in catch block and file path is fine...it's soething else i think


Thank you for wasting my time by posting code off the cuff.
Please read this: http://faq.javaranch.com/java/PostRealCode
 
Shashika Thiran
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all of u.I got the point now.I'm working on it now.thanks.
 
Shashika Thiran
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the answer of my problem...
It's simple add -r for >
like this..

this is work fine and it create backup.sql file.
now i have problem with restore....

mysqldump -u root -ppword Dname < C:/backup.sql

anyone know the symbol to use instead of "<"
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read my previous post? I told you how you could handle ">" in Java. "<" is similar: call the Process object's getOutputStream(), create an InputStream, and copy the data.
 
Shashika Thiran
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i write code like this..

but it not restore my backup.please help me with this code.
Thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure your program name and parameters are correct? Before you used mysqldump, now it's mysql. Also, try flushing the output stream.

And please http://faq.javaranch.com/java/UseCodeTags
 
Shashika Thiran
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
finally i manage to find the way to restore using this code.
think this gone help others too

 
Tom Kuras
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, String[] is the way to go for restore. A working example on how to use dump/restore commands from JSP code can be found here
http://www.jvmhost.com/articles/mysql-postgresql-dump-restore-java-jsp-code
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic