简体   繁体   English

如何在Java代码中运行mysqldump命令?

[英]How to run mysqldump command in java code?

I want to test to run mysqldump command in my function but I could not create aaadumpdb.sql file. 我想测试在我的函数中运行mysqldump命令,但无法创建aaadumpdb.sql文件。 My code is below: 我的代码如下:

@Test
public void dumpDB() {
    Process p = null;
    try {
        Runtime runtime = Runtime.getRuntime();
        p = runtime
                .exec("mysqldump -u root -padmin --add-drop-database aaa_db "
                        + "D:\\backupdenemeaaa " + "aaadumpdb.sql");
        // 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();
    }

    File f = new File("aaadumpdb.sql");

    assertTrue(f.exists());
}

Can anybody give me some advice about it? 有人可以给我一些建议吗? Thank you. 谢谢。

I did some edit in my code but when I run, my code enter into else structure. 我在代码中做了一些编辑,但是当我运行时,我的代码进入了else结构。 What can be the problem? 可能是什么问题?

My editted code is below: 我编辑的代码如下:

    @Test
public void dumpDB() {
    Process p = null;
    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 aaa_db -r" + "D:\\backupdenemeaaa " + "\\aaadumpdb.sql");
        // 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();
    }

    File f = new File("aaadumpdb.sql");

    assertTrue(f.exists());

How can I solve this problem? 我怎么解决这个问题? Thank you. 谢谢。

Add the below snippet to find what exactly is happening 添加以下代码段以查找实际发生的情况

try
        {
            InputStreamReader isr = new InputStreamReader(process.getErrorStream());
            BufferedReader br = new BufferedReader(isr,4094);
            String line=null;
            while ( (line = br.readLine()) != null)
                System.out.println(type + "> " + line);    
            } catch (IOException ioe)
              {
                ioe.printStackTrace();  
              }
    }

You will be aware of the the exceptions and errors that might occur in that Process execution 您将了解该流程执行中可能发生的异常和错误

The stacktrace would really help to debug it, but I believe you can check if "mysqldump" is in your PATH or the application will simple not find the executable. stacktrace确实可以帮助调试它,但是我相信您可以检查PATH中是否包含“ mysqldump”,否则应用程序将找不到可执行文件。 If you don't want to mess around with your PATH you can hardcode the path to the executable like below: 如果您不想弄乱PATH,可以将可执行文件的路径硬编码为如下所示:

String mysqldumpExecutable = "C:\\apps\\mysql\\mysqldump.exe";
runtime.exec(mysqldumpExecutable + "-u root -padmin --add-drop-database aaa_db (.....));

When you say you can't create the file what exactly does that mean? 当您说无法创建文件时,这到底是什么意思? Are you getting an error? 您遇到错误了吗? Nothing happens? 什么都没发生? Help us help you. 帮助我们帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM