1

您好我有一个 JAVA 项目,我想在其中显示 MySQL 数据库。我写了这段代码:

try {
     String []command={"mysql -u root -pmanager","show databases"};
        Process p= Runtime.getRuntime().exec("mysql -u root -pmanager");
        Process p1= Runtime.getRuntime().exec("show databases");
        if (p.waitFor()==0){System.out.println("backup done...");}
        else{System.out.println("!!!");}
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

但它给了我这个错误:

Cannot run program "show": CreateProcess error=2, The system cannot find the file specified

应该怎么做?谢谢...

4

2 回答 2

4

最好在 exec 方法中传递 String 对象,例如:

Runtime.getRuntime().exec(  new String [] {"mysql", "-u", "root", "-pmanager", "-e", "show databases"} )
于 2013-09-25T13:31:55.603 回答
0

您需要在命令提示符下指定什么是 'mysql'... 示例 - C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql

于 2017-07-04T14:44:32.977 回答