jeudi 8 octobre 2015

How to find mysqldump.exe path automatically in code?

I want to test my mysqldump command. I call path of executable mysqldump.exe by hardcoded but I want to find path of mysqldump.exe automatically in code. Can anybody give me some advice about it? Thank you.

My code:

public String dumpDB() {
    Process p = null;
    String dumpDirectory = "D:/backupdenemex/xdumpdb.sql";
    try {

        Runtime runtime = Runtime.getRuntime();
        String mysqldumpExecutable = "C:/Program Files/MySQL/MySQL Server 5.6/bin/mysqldump.exe";
        p = runtime.exec(mysqldumpExecutable
                + " -uroot -padmin --add-drop-database -B x_db -r "
                + dumpDirectory);
        // change the dbpass and dbname with your dbpass and dbname
        int processComplete = p.waitFor();

        if (processComplete == 0) {

            System.out.println("Backup created successfully!");

        } else {
            JOptionPane.showMessageDialog(new JDialog(),
                    "Could not create the backup");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return dumpDirectory;
}

Aucun commentaire:

Enregistrer un commentaire