从 java 获取 mysqldump 错误消息

get mysqldump error message from java

我使用 Java runtime.exec 成功转储了数据。
现在我想处理像 "mysqldump: Got error: 1045: Access denied for user 'errre'@'localhost' (using password: YES) when trying to connect"
这样的异常,当我输入错误的用户名或密码或错误的 where 条件时

但是我从 process.getErrorStream() 得到的是 "Warning: Using a password on the command line interface can be insecure." 而 getInputStream() 是 null

在终端中,输出如下:
mysqldump -uerror -p123456 erp_sys 警告:在命令行界面上使用密码可能不安全。 mysqldump:出现错误:1045:尝试连接时拒绝用户 'error'@'localhost'(使用密码:YES)访问

我做了一些研究,但仍然找不到获取 mysqldump 的方法error.Anyone有这方面的经验吗?

我不知道。它对我有用:

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;

public class MysqlTest {
    public static void main(String[] args) throws Exception {
        Process p = new ProcessBuilder("mysqldump", "-u", "error", "-p123456", "erp_sys").start();
        ByteArrayOutputStream dmp = new ByteArrayOutputStream();
        // Use FileOutputStream dmp = ... in real cases.
        Thread t1 = copyStreamsInBackground(p.getInputStream(), dmp);
        ByteArrayOutputStream err = new ByteArrayOutputStream();
        Thread t2 = copyStreamsInBackground(p.getErrorStream(), err);
        t1.join();
        t2.join();
        int exitCode = p.waitFor();
        if (exitCode != 0) {
            System.err.println("Exit code: " + exitCode);
            String errors = new String(err.toByteArray(), Charset.forName("utf-8"));
            System.err.println(errors);
        } else {
            System.out.println("Exit code: " + exitCode);
            String dumps = new String(dmp.toByteArray(), Charset.forName("utf-8"));
            System.out.println(dumps);
        }
    }

    private static Thread copyStreamsInBackground(final InputStream is, final OutputStream os) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    IOUtils.copy(is, os);
                    os.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    throw new IllegalStateException(ex);
                }
            }
        });
        t.start();
        return t;
    }
}

它打印:

Exit code: 2
mysqldump: Got error: 1045: Access denied for user 'error'@'localhost' (using password: YES) when trying to connect