[英]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.