我无法在java上与mysqldump.exe集成

时间:2011-12-09 09:06:03

标签: java mysql backup mysqldump

我试图通过MySQL附带的mysqldump实用程序备份MySQL数据库。我的代码是在JAVA.But它什么也没有返回,所以我无法备份数据库。以下是用于执行此操作的函数:

public String getServerDumpData()
{
    new Database("Database.ini");
    StringBuilder dumpdata = new StringBuilder();
    String execline = "";
    try {
        if(Database.ConnectToDatabase()){
            // Set path. Set location of mysqldump
            //  For example: current user folder and lib subfolder
            if( HelpersToolbox.IsWindows() ){
                execline = System.getProperty("user.dir") + "\\mysql\\mysqldump.exe";
            }else{
                execline = System.getProperty("user.dir") + "\\lib\\mysqldump.exe";
            }
            // Usage: mysqldump [OPTIONS] database [tables]
            // OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
            // OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
            String command[] = new String[]{ execline,
                            "--host=" + Database.DbServer,
                            "--port=" + Database.DbPort,
                            "--user=" + Database.DbUsername,
                            "--password=" + Database.DbPassword,
                            "--compact",
                            "--complete-insert",
                            "--extended-insert",
                            "--skip-comments",
                            "--skip-triggers",
                            Database.DbName };

            // Run mysqldump
            ProcessBuilder pb = new ProcessBuilder(command);
            Process process = pb.start();



            InputStream in = process.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));


            int count;
            char[] cbuf = new char[STREAM_BUFFER];

            // Read datastream
            while ((count = br.read(cbuf, 0, STREAM_BUFFER)) != -1){
                dumpdata.append(cbuf, 0, count);

            }
            // Close
            br.close();
            in.close();
        }

    } catch (Exception ex) {

        ex.printStackTrace();
        return "";
    }
    return dumpdata.toString();
}

最后无法访问循环。那么这里的问题是什么?

1 个答案:

答案 0 :(得分:0)

您有错误流的数据,您必须使用

读取或合并输出流
pb.redirectErrorStream(true);

在开始这个过程之前。