1
我目前正在使用Java编写Windows shell模拟器。当我使用Process.getOutputStream()方法读取并显示程序的输出时,大多数程序看起来工作正常。当我从程序运行MySql控制台时,这似乎并不适用。 MySql似乎没有打印到默认的系统控制台。如何使用Java进程读取mysql控制台输出
如何挂接到MySql控制台以显示其输出并允许用户通过我的模拟器提供输入?
我目前正在使用Java编写Windows shell模拟器。当我使用Process.getOutputStream()方法读取并显示程序的输出时,大多数程序看起来工作正常。当我从程序运行MySql控制台时,这似乎并不适用。 MySql似乎没有打印到默认的系统控制台。如何使用Java进程读取mysql控制台输出
如何挂接到MySql控制台以显示其输出并允许用户通过我的模拟器提供输入?
我认为有两个问题需要解决。首先是救援输出,你可以使用这篇文章(http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.htm)了解救援输出的方式。 因此,我使用命令行来救援mysql命令行软件的输出 - 开关-e:
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();
}