osql -S not giving me expected results
I'm trying to write a program in Java that uses osql
to generate a list of databases on a server. My code is as follows:
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);
S开发者_开发问答tring 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;
}
When I run this, for some reason I get a message as if I had run osql -?
, which I obviously have not done.
I'm not sure what I'm doing wrong, so any help would be appreciated. Thanks so much!
remove spaces from the arguments take && off: str = new String[] {"cmd.exe", "/c", "osql", "-S", server, "-U", user, "-P", passwd, "-Q", "sp_databases"};
精彩评论