Two Laptop Bag*
The moose likes Java in General and the fly likes how to get mysql backup using java swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to get mysql backup using java swing" Watch "how to get mysql backup using java swing" New topic
Author

how to get mysql backup using java swing

Shashika Thiran
Greenhorn

Joined: Jan 22, 2009
Posts: 24
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

Joined: Jul 26, 2007
Posts: 10167
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Dmitry Mamonov
Greenhorn

Joined: Dec 24, 2008
Posts: 28
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

Joined: Jan 22, 2009
Posts: 24
I have printStacktrace() in catch block and file path is fine...it's soething else i think
Dmitry Mamonov
Greenhorn

Joined: Dec 24, 2008
Posts: 28
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
Marshal

Joined: Mar 22, 2005
Posts: 41065
    
  43
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.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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().)


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

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

Joined: Jan 22, 2009
Posts: 24
Thank you all of u.I got the point now.I'm working on it now.thanks.
Shashika Thiran
Greenhorn

Joined: Jan 22, 2009
Posts: 24
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Jan 22, 2009
Posts: 24
now i write code like this..

but it not restore my backup.please help me with this code.
Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Jan 22, 2009
Posts: 24
finally i manage to find the way to restore using this code.
think this gone help others too

Tom Kuras
Greenhorn

Joined: Jul 21, 2011
Posts: 2
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
 
 
subject: how to get mysql backup using java swing
 
Similar Threads
How to restore mysql backup using java swing
need help with mysqldump...
Backup
Backing up database through java
Runtime.exec()