我正在尝试用Java编写一个程序,该程序使用osql
生成服务器上数据库的列表。我的代码如下:
public Object[] findDataBases(String server, String user, String passwd){
str = new String[] {"cmd.exe", "/c", "osql", " -S ",
server, " -U", user, "&&", "-P ", passwd,
"&&", "sp_databases","&&", "GO"
};
Runtime rt = Runtime.getRuntime();
try{
Process p = rt.exec(str);
InputStream is = p.getInputStream();
InputStream err = p.getErrorStream();
InputStreamReader in = new InputStreamReader(is);
InputStreamReader er = new InputStreamReader(err);
BufferedReader buff = new BufferedReader(in);
String line = buff.readLine();
ArrayList<String> listDatabases = new ArrayList<String>();
while (line != null){
listDatabases.add(line.trim());
/*for (int i = 0; i<4; i++){
buff.readLine();
}*/
line =buff.readLine();
}
System.out.println("error stream:");
buff = new BufferedReader(er);
while ((line=buff.readLine()) != null){
System.err.println(line);
}
databases = listDatabases.toArray();
return databases;
}
当我运行此命令时,由于某种原因,我会收到一条消息,好像我已经运行了
osql -?
,但显然我还没有这样做。我不确定自己在做什么错,因此可以提供任何帮助。非常感谢!
最佳答案
从参数中删除空格&& off:
str = new String [] {“ cmd.exe”,“ / c”,“ osql”,“-S”,服务器,“-U”,用户,“-P”,passwd,“-Q”,“ sp_databases” “};
关于java - osql -S没有给我预期的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2364408/