Abdul Jabbar
Abdul Jabbar

Reputation: 5971

Error in Creating MySQL DB Backup with Java Command Line with Mysqldump

I am facing a strange problem in Creating MySQL DB Backup in java with MysqlDump. When i run command in Command Prompt of Windows it executes easily without any problem and creates backup but when i execute command from java application i get error. My Command is

c:/xampp/mysql/bin/mysqldump -h localhost –-user root –-password=abc --add-drop-database -B db_WebBe -r db_test.sql

and I get this error of MySQL User

mysqldump: Got error: 1044: Access denied for user ''@'localhost' to database '?-user' when selecting the database

I have tried all the stupid things i could do including

1) allow all the rights to ''@localhost
2) deleted user ''@localhost and then got error with 'jdbc'@localhost
3) Tried to Run Java Application with Administartor Rights (Stupid)
4) Some other codes from StackOverflow guides but all returned same error
5) Changed root password
6) Reinstalled, Updated ODBC and JDBC Drivers
6) and many others don't remember :-/

My Code for understanding is

 public boolean databaseBackup() 
{
    try 
    {
        CreateConnection();
        Process runtimeProcess;
        runtimeProcess = Runtime.getRuntime().exec("c:/xampp/mysql/bin/mysqldump -h localhost –-user root –-password=abc --add-drop-database -B db_poonawala -r db_test.sql");
        BufferedReader stdInput = new BufferedReader(new 
             InputStreamReader(runtimeProcess.getInputStream()));

        BufferedReader stdError = new BufferedReader(new 
             InputStreamReader(runtimeProcess.getErrorStream()));

        String s=null;
        try {
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
            System.out.println("5");

            System.out.println("Backed up");
            return true;
        } else {
            System.out.println("Not Backed up");
        }
        conn.close();
    } 
    catch (Exception ex) 
    {
            ex.printStackTrace();
    }
    return false;
}

Upvotes: 0

Views: 1127

Answers (1)

Abdul Jabbar
Abdul Jabbar

Reputation: 5971

I finally figured out and now my working code is this. Maybe someone in future can be benefited by this. I just executed my code without any connection except during the mysqldump command line. This was may be due to as I was using mysqldump during another connection still open.

try
{
    int processComplete; 
    Process runtimeProcess = Runtime.getRuntime().exec("C:\\xampp\\mysql\\bin\\mysqldump -u root -pabc db_Name -r D:/backup.sql");
    processComplete = runtimeProcess.waitFor();
    if(processComplete==1)
    {

        JOptionPane.showMessageDialog(null, "Backup Failed");
    }
    else if(processComplete==0)
    {
        JOptionPane.showMessageDialog(null,"\n Backup created Successfully.");
    }

}
catch(Exception e)
{
     JOptionPane.showMessageDialog(null,e);
}

Upvotes: 1

Related Questions