我正在尝试使用Java通过osql -L
命令填充网络上的SQL服务器下拉列表。然后,用户将选择一个服务器并输入用户名和密码。我还需要用该服务器上的数据库填充另一个列表。您有任何使用Java实现此目的的想法吗?如果可能,请给出Java代码。
谢谢。
public class SQL implements ActionListener{
public static void main(String[] args) throws Exception{
String[] str = new String[] {"cmd.exe", "/c", "osql", "-L" };
Runtime rt = Runtime.getRuntime();
try{
Process p = rt.exec(str);
InputStream is =p.getInputStream();
InputStream err = p.getErrorStream();
InputStreamReader in = new InputStreamReader(is);
StringBuffer sb = new StringBuffer();
BufferedReader buff = new BufferedReader(in);
//clearing away white space and "Servers"
buff.readLine();
buff.readLine();
String line = buff.readLine();
JComboBox servers = new JComboBox();
while (line != null){
servers.addItem(line.trim());
line =buff.readLine();
}
SQL sql = new SQL();
servers.addActionListener(sql);
JOptionPane.showMessageDialog(null, servers);
}catch( Exception ex )
{
ex.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String ser = (String)cb.getSelectedItem();
}
}