• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mysql database restore through java application...?

 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne,

Yes this works fine through command line. But i could not process it through java application.
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic