我想通過Java使用osql -L
命令填充網絡上SQLServer的下拉列表。用戶將選擇一個服務器並輸入用戶名和密碼。我還需要使用該服務器上的數據庫填充另一個列表。關於如何使用java實現這一點的任何想法?如果可能的話,請給java代碼。一旦我有了一個SQLServer的名字,我該如何在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();
}
}