tags:

views:

27

answers:

1

I am trying to populate a drop down list of SQLServers on a network using the osql -L command through Java. The user will then choose a server and type in a username and password. I also need to populate another list with the databases on that server. Any ideas on how I can implement that using java? If possible, please give java code.

Thank you.

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();


}
}
A: 

This website helped a lot.

http://www.cyberciti.biz/faq/howto-ms-sql-list-tables-and-database/

chama