java - 如何使用 Java Process 读取 mysql 控制台输出

标签 java mysql windows console

我目前正在使用 Java 编写 Windows shell 模拟器。当我使用 Process.getOutputStream() 方法读取和显示程序的输出时,大多数程序似乎都能正常工作。当我从我的程序运行 MySql 控制台时,这似乎并不适用。 MySql 似乎没有打印到默认系统控制台。

如何连接到 MySql 控制台以显示其输出并允许用户通过我的模拟器提供输入?

最佳答案

我认为有两个问题需要管理。第一个是拯救输出,你可以通过这篇文章(http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.htm)来了解拯救输出的方法。 因此,我使用命令行通过开关 -e 来挽救 mysql 命令行软件的输出:

             try {
                FileOutputStream fos = new FileOutputStream("c:/test.txt");
                Runtime rt = Runtime.getRuntime();
                Process proc = rt.exec("mysql.exe -u root -e \"select * from table_name\" database_name");

                StreamGobbler errorGobbler = new StreamGobbler(
                        proc.getErrorStream(), "ERROR");

                StreamGobbler outputGobbler = new StreamGobbler(  proc.getInputStream(), "OUTPUT", fos);

                errorGobbler.start();
                outputGobbler.start();

                int exitVal = proc.waitFor();
                System.out.println("ExitValue: " + exitVal);
                fos.flush();
                fos.close();
            } catch (Throwable t) {
                t.printStackTrace();
            }

关于java - 如何使用 Java Process 读取 mysql 控制台输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10832936/

相关文章:

java - 鱿鱼:MaximumInheritanceDepth on Android

java - 在 Spring Data 中更新

php - 使用正则表达式查找和替换 SQL 查询

MySQL事件调度程序查询?

c# - 如何创建非窗口绑定(bind)的键盘快捷键

java - 循环线程组 - 请帮我调试

java - Hadoop:使用什么来代替已弃用的接口(interface) Mapper 和Reducer?

mysql - 对于 SELECT * WHERE id ='1blah' 这样的查询,PostgreSQL 和 Oracle 会如何表现?

windows - gpgsm -a --export-secret-key-p12 [keyid] 在windows下显示错误信息 "No secret key"

c - 如何从终端将字符传递给可执行文件?