aspose file tools*
The moose likes Java in General and the fly likes Mysql database restore through java application...? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Mysql database restore through java application...?" Watch "Mysql database restore through java application...?" New topic
Author

Mysql database restore through java application...?

prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120
Hi Members,

I have to execute mysql command line syntax from java runtime, that should work in both windows and linux machine. I tried using the Runtime.getRuntime().exec("cmd /c mysql -u username -p pass databasename < c:backup.sql");

But this is not restoring my database, instead im getting the mysql version and options information in the console trace.

Please help me to figure out this issue...

Some times it just hangs...

Regards,
Prabhu.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30548
    
152

Prabhu,
Does that work if you run it directly at the command line? The manual shows a database option being passed in and not just the database name.

mysql -u username -p --database=dbname < dump.sql


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120
Jeanne,

Yes this works fine through command line. But i could not process it through java application.
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by prabhu rangan:
But i could not process it through java application.


I would suggest that you create a .bat file for windows and .sh file for Linux, and write the command "mysql -u username -p --database=dbname < dump.sql" in .bat/.sh file.

This way you will also get flexibility of changing command in files like username, password etc. You will not have tochange the java code everytime for such changes.

Shailesh


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30548
    
152

If it works through the command line, it is likely a Runtime.exec problem rather than a database problem. I'll move this to our Java in General forum for you.
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120
Hi Members,

I do not know where im failing, but it works fine through command line and what reason this condition fails in java through Runtime execution.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Trying something at the command line is not a good enough test. The command line is a shell which takes care of redirects (where you put < filename to take the input from a file). But Runtime.exec() does not start a shell, it just attempts to run an executable with parameters.

So, if you want your input to come from a file, you will have to provide that input by passing it to the Process's InputStream. Or OutputStream, whichever it is, you could look it up in the documentation.

Or you could use a .bat file as already suggested. Nobody has yet linked to the article When Runtime.exec() won't which you should really read.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mysql database restore through java application...?