Results 1 to 7 of 7
  1. #1
    Robert_85 is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default 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:

    Java Code:
    [COLOR="Red"]Runtime.getRuntime().exec("mysqldump --user root --password=secret pharmacy > testBackUp.sql");[/COLOR]
    I also used this code, but still not working:

    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]
    Does anyone know what the problem is?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    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.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    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.

  4. #4
    Robert_85 is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default

    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.

  5. #5
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default 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();
    }

  6. #6
    Join Date
    Oct 2009
    Posts
    88
    Rep Power
    0

    Default 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 best
    Last edited by anilkumar_vist; 11-18-2009 at 12:06 PM.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    "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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •