简体   繁体   English

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

[英]How can I read mysql console output using Java Process

I am currently writing a Windows shell emulator using Java. 我目前正在使用Java编写Windows Shell模拟器。 Most programs seem to work fine when I use the Process.getOutputStream() method to read and display the program's output. 当我使用Process.getOutputStream()方法读取和显示程序的输出时,大多数程序似乎都能正常工作。 When I run the MySql console from my program, this doesn't seem to apply though. 当我从程序中运行MySql控制台时,这似乎并不适用。 MySql doesn't seem to print to the default system console. MySql似乎无法打印到默认系统控制台。

How can I hook in to the MySql console to display its output and allow input to be provided by a user through my emulator? 如何连接到MySql控制台以显示其输出并允许用户通过仿真器提供输入?

I think there is two problems to manage. 我认为有两个问题需要处理。 The first is rescue the output and you can use this article ( http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.htm ) to understand the way to rescue output. 首先是抢救输出,您可以使用本文( http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.htm )来了解救援输出。 So I use the command line to rescue output from mysql command line software with switch -e : 所以我使用命令行通过switch -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();
            }

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

相关问题 什么是Java用于在控制台中输出,以及如何在ruby中捕获它? - What is Java using to output in console, and how can I capture it in ruby? Java:如何修改控制台 output? - Java: how can I modify console output? Java程序如何完全读取自己的控制台输出? - How can a Java program read its own console output fully? 如何实时读写进程并在 JTextArea 中实时显示输出? (Java 摆动) - How can I read and write a process in real time and display output in JTextArea in real time? (Java Swing) 如何将 Java 控制台输出读入字符串缓冲区 - How could I read Java Console Output into a String buffer 我无法在FOP流程中捕获Java的控制台输出,并且我的所有流程均无法正常运行 - I can't capture the console output from Java in FOP process and all my process it doesn't work 如何为以Java启动的nogui进程创建控制台窗口? - How can i create a Console Window for a nogui process started in java? 我想从控制台模式子进程读取输出 - I want to read output from a console-mode child process 如何使用 Java 中的扫描仪 class 从控制台读取输入? - How can I read input from the console using the Scanner class in Java? 如何从 java 代码输出到 Jenkins 控制台? - How can I output from java code to the Jenkins console?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM