English 中文(简体)
一旦我拥有SQLServer的名称,如何用Java列出它的数据库?
原标题:Once I have the name of a SQLServer, how do I list its databases in java?

我正在尝试使用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();


}
}
最佳回答

这个网站帮了很多忙。

将此翻译成中文:http://www.cyberciti.biz/faq/howto-ms-sql-list-tables-and-database/ http://www.cyberciti.biz/faq/howto-ms-sql-list-tables-and-database/

问题回答
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:
");
            while ((ss = stdInput.readLine()) != null) {
                System.out.println(ss);
            }

            System.out.println("Here is the standard error of the command (if any):
");
            while ((ss = stdError.readLine()) != null) {
                System.out.println(ss);
            }

        }catch( Exception ex )
        {
            ex.printStackTrace();   
        }
    }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签