2

I'm trying to backup my database from a Java application. Currently, I'm using xampp as a localhost for database. I tried these codes in my program, but when I execute these codes my program goes on waiting state. When I execute the same code from command line it ask a password and after giving a password back up file backup.sql is created successfully. The code I'm using is:

String executeCmd = "C:\\xampp\\mysql\\bin\\mysqldump.exe -u root -p db1 -r D:/mine/backup.sql";

Java code:

    Process runtimeProcess;
    try {


        runtimeProcess = Runtime.getRuntime().exec(executeCmd);

        int processComplete = runtimeProcess.waitFor();

        if (processComplete == 0) {
            System.out.println("Backup created successfully");
            return true;
        } else {
            System.out.println("Could not create the backup");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

1 Answer 1

2

Try using the command with password in it, like

String executeCmd = "C:\\xampp\\mysql\\bin\\mysqldump.exe -u root -pYOUR_DB_PASSWORD db1 -r D:/mine/backup.sql";

Mind the NO SPACE between -p and the DATABASE PASSWORD.


Edit1: (from comment)

To resotre your database you need to execute

mysql -u root -pPASSWORD db1 < /full/qualified/path/to/backup.sql

Edit2: (improvements)

You may also want to try some of the cool options like

mysqldump --user MYSQL_USER --password=MYSQL_PASSWORD --add-locks --flush-privileges \
--add-drop-table --complete-insert --extended-insert --single-transaction \
--database DB_TO_BACKUP > FILENAME_FOR_SQL_STATEMENTS

Refer here.

Sign up to request clarification or add additional context in comments.

8 Comments

what sud i write if the database password is ""(null).
@user1154781 -- it ask a password and after giving a password back up file backup.sql is created successfully. -- I thought you have a password. If there is no password, I think, you should remove -p option from the executeCmd
Hey thanx it worked and what if i had to restore this file as database in my application.
@user1154781 glad it worked. If this answer solved your problem, please mark it as answered by clicking on the up-tick next to this answer. See here how. If you liked the answer, you may also up-vote by clicking on up arrow next to the answer.
I'm just a beginner so voting is forbidden to me..surely i will in near future now can u tell me how can i restore the saved backup.sql file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.