0

我在使用 MysqlDump 在 java 中创建 MySQL 数据库备份时遇到了一个奇怪的问题。当我在 Windows 的命令提示符中运行命令时,它可以轻松执行而没有任何问题并创建备份,但是当我从 java 应用程序执行命令时出现错误。我的命令是

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

我得到这个 MySQL 用户的错误

mysqldump:出现错误:1044:选择数据库时,用户''@'localhost'对数据库'?-user'的访问被拒绝

我已经尝试了所有我能做的愚蠢的事情,包括

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

我的理解代码是

 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;
}
4

1 回答 1

1

我终于想通了,现在我的工作代码是这样的。也许将来有人可以从中受益。mysqldump除了在命令行期间,我只是在没有任何连接的情况下执行了我的代码。这可能是因为我mysqldump在另一个连接仍然打开时使用。

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);
}
于 2013-10-24T17:07:14.843 回答