簡體   English   中英

獲得SQLServer的名稱后,如何在Java中列出其數據庫?

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

我正在嘗試通過Java使用osql -L命令在網絡上填充SQLServer的下拉列表。 然后,用戶將選擇服務器並輸入用戶名和密碼。 我還需要用該服務器上的數據庫填充另一個列表。 關於如何使用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();


}
}
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM