我正在尝试用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 -?
,我显然没有这样做。
我不确定我做错了什么,所以任何帮助都会受到赞赏。非常感谢!
答案 0 :(得分:1)
从参数中删除空格&amp;&amp;关: str = new String [] {“cmd.exe”,“/ c”,“osql”,“ - S”,server,“ - U”,user,“ - P”,passwd,“ - Q”,“sp_databases “};