代码之家  ›  专栏  ›  技术社区  ›  chama

一旦我有了一个SQLServer的名字,我该如何在Java中列出它的数据库?

  •  1
  • chama  · 技术社区  · 15 年前

    我正在尝试使用 osql -L 通过Java命令。然后,用户将选择一个服务器并输入用户名和密码。我还需要用该服务器上的数据库填充另一个列表。关于如何使用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();
    
    
    }
    }
    
    2 回复  |  直到 11 年前
        1
  •  0
  •   chama    15 年前
        2
  •  0
  •   Pradeep    11 年前
    public static void main(String[] args) {
            String[] str = new String[] {"cmd.exe", "/c", "sqlcmd -U abc -P abc -q sp_databases"};
    
            Runtime rt = Runtime.getRuntime();
            try{
    
                String ss = null;
                Process p = rt.exec(str);
    
                BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
                BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    
                //obj.exec(ss);
                System.out.println("Here is the standard output of the command:\n");
                while ((ss = stdInput.readLine()) != null) {
                    System.out.println(ss);
                }
    
                System.out.println("Here is the standard error of the command (if any):\n");
                while ((ss = stdError.readLine()) != null) {
                    System.out.println(ss);
                }
    
            }catch( Exception ex )
            {
                ex.printStackTrace();   
            }
        }