简体   繁体   English

获得SQLServer的名称后,如何在Java中列出其数据库?

[英]Once I have the name of a SQLServer, how do I list its databases in java?

I am trying to populate a drop down list of SQLServers on a network using the osql -L command through Java. 我正在尝试通过Java使用osql -L命令在网络上填充SQLServer的下拉列表。 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? 关于如何使用Java实施的任何想法? If possible, please give java code. 如果可能,请提供Java代码。

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


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何扫描文件夹,以便可以在文本文件中列出其内容的名称? Java的 - How do I scan a folder so I can list the name of its contents in a text file? Java Java:当我将其名称作为字符串时,如何返回对象? - Java: How to return object when I have its name as a string? 如何在Java目录及其子目录中查找/列出具有特定名称的文件或目录? - How do I find/list a file or directory with specific name inside a directory and its subdirectories in Java? 如何让程序用Java创建自己的变量? - How do I have a program create its own variables with Java? 在 java 中创建对象后,如何使用对象实例变量进行数学运算 - How do i do math with object instance variables once i have created the object in java 如何通过Java使用“显示表​​”列出多个数据库及其表? - How do I list multiple databases with their tables by using “Show Tables” via Java? 数据库的新手,该如何设计? MYSQL和Java - New to databases, how do I design this? MYSQL and Java 加密后如何解密? - How do I decrypt once I have encrypted? 我如何一次加载我的片段并保存其状态 - How do I load my Fragment once and save its state JAVA:我有地址; 如何打印其内容? - JAVA: I have the address; how can I print its contents?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM