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

发布于 2024-12-20 06:46:33 字数 2218 浏览 0 评论 0原文

我正在尝试通过 MySQL 附带的 mysqldump 实用程序备份 MySQL 数据库。我的代码是在JAVA上。但它没有返回任何内容,所以我无法备份数据库。下面是用于执行此操作的函数:

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();
}

它无法访问末尾的循环。那么这里有什么问题呢?

Am trying to backup MySQL database via mysqldump utility attached with MySQL. My code is on JAVA.But it returns nothing so i cannot backup the database. Here is the function used to do that :

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();
}

It cannot access the loop at the end . So what's the problem here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

予囚 2024-12-27 06:46:33

您还必须读取这些数据或将其与输出流合并。

pb.redirectErrorStream(true);

您拥有错误流上的数据,在开始该过程之前,

You have the data on the error stream which you either also have to read or merge with the output stream by using

pb.redirectErrorStream(true);

before starting the process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文