Results 1 to 7 of 7
Thread: MYSQLDUMP from Java
- 04-07-2009, 03:05 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 27
- Rep Power
- 0
MYSQLDUMP from Java
Hi everyone,
I am trying to take a backup of my database using mysqldump from java.
This is the code I use to back up my database but for some reason it's not working:
I also used this code, but still not working:Java Code:[COLOR="Red"]Runtime.getRuntime().exec("mysqldump --user root --password=secret pharmacy > testBackUp.sql");[/COLOR]
Does anyone know what the problem is?Java Code:[COLOR="Red"]Runtime shell = Runtime.getRuntime(); shell.exec("C:/mysql-5.1.30-win32/bin/mysqldump.exe --opt --user root --password=secret pharmacy > C:/Users/Ali/testing.sql");[/COLOR]
- 04-07-2009, 03:08 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Why are you attempting to do a backup from Java? Especially using a Runtime.exec.
There are tools designed for performing backup operations on just about any DB you can imagine, and seeing as mysqldump is a completely self contained command, a System scheduler and a redimentary script would be enough to perform regular backups.
Use the right tool for the job, and Java is, normally, not the right tool for this.
- 04-07-2009, 03:08 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
That having been said, it might help to know exactly what "it did not work" means.
But, while you're thinking about that read When Runtime.exec() won't - JavaWorld thoroughly, carefully, and completely and implement the suggestions contained therein.
- 04-07-2009, 03:35 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 27
- Rep Power
- 0
Thanks for suggestion, thee reason I am using java to back up my database is that I am creating a java application which interact with MYSQL Database and I need to have this functionality(backup) in the system where the user click a button to backup the database.
This is why I need the java code for it.
- 11-18-2009, 08:15 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
Hi
Hi Robert,
I had that code but the problem is while inserting that dump into db. Try this one....
String host="localhost",user="root",pw="dbpwd!";
File test=new File("D:\\aaa\\DB_Backup\\test.sql");
FileWriter fw=null;
try{
fw = new FileWriter(test);
Runtime rt = Runtime.getRuntime();
Process child = rt.exec("C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump -h"+host+" -u"+user+" -p"+pw+" testDB");
InputStream in = child.getInputStream();
InputStreamReader xx = new InputStreamReader(in,"latin1");
char[] chars=new char[1024];
int ibyte=0;
while((ibyte=xx.read(chars))>0)
{
fw.write(chars);
}
fw.close();
}catch (Exception e) {
e.printStackTrace();
}
- 11-18-2009, 11:04 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 88
- Rep Power
- 0
just try it
set the environment path for mysql while installing
then
in java by using
String varname="cmd mysqldump -uroot -ppassword databasename>c:\\backup.sql"
Runtime.getRuntime().exec(varname);
if u want tosave in other dir use filechosser and get the path od destination.
all the bestLast edited by anilkumar_vist; 11-18-2009 at 11:06 AM.
- 11-18-2009, 03:33 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
"cmd" is needed in the command only when the command to be executed is not actually a command, but rather a "shell-builtin". If an exe exists (which there does for mysqldump) "cmd" is not needed, and is possibly counter-productive.
Also, the output redirection there is not going to work properly.
And, he has already been given a link to a document that covers using Runtime.exec in-depth, and he hopefully knows what command he wishes to use.
In short, the above post is almost useless.
Edit: Ditto for the post above that, but a little less so.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks